Accept an empty snapshot in diff and report malformed records properly

Three problems in load_in_memory, all visible from the command line:

An empty snapshot was treated as an error, though create() produces one
for an empty directory and list() accepts it. Diffing against it should
report every record of the other side as added or deleted.

A malformed record jumped to cleanup without setting errno and without
naming the offending line, so main() printed strerror() of whatever was
left over, normally 0:

  $ fsnap diff old.fsnap malformed.fsnap
  diff failed: Success

The enum Fields it passed to RecordObjectUnpack was uninitialised and
then discarded, even though list() uses the same out-parameter to say
which field failed.

Report parse failures the way list() does, with snapshot:line: field, and
give them the same distinct 128 status so main() does not append a second,
meaningless message. Guard qsort against the now-reachable NULL array, and
check fflush(stdout) as scan() already does.

  $ fsnap diff old.fsnap malformed.fsnap
  malformed.fsnap:3: unparsable line

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 15:12:14 +02:00
parent 7f0c18df76
commit 4aaaa8dd9c
2 changed files with 36 additions and 11 deletions

View File

@@ -2,7 +2,7 @@
#define DIFF_H
/* Compare two snapshot files and print added/deleted/modified entries.
Returns 0 on success, -1 on failure. */
Returns 0 on success, -1 on system/IO error, 128 on parse error. */
int diff(const char *file1, const char *file2);
#endif

View File

@@ -7,8 +7,9 @@
#include <stdlib.h>
#include <string.h>
/* Returns 0 on success, -1 on a system error, 128 on a malformed record. */
static int load_in_memory(const char *file, struct RecordObject ***records,
int *count) {
int *count) {
int rc = -1;
FILE *stream = NULL;
char *buf = NULL;
@@ -17,11 +18,18 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
goto cleanup;
*count = count_lines(file);
if (*count == 0)
goto cleanup;
if (*count < 0)
goto cleanup;
/* An empty snapshot is a valid one: create() produces it for an empty
directory and list() accepts it. It simply carries no records, so every
record in the other snapshot is an addition or a deletion. calloc(0, ..)
may legitimately return NULL, hence the early return. */
if (*count == 0) {
rc = 0;
goto cleanup;
}
*records = calloc((size_t)*count, sizeof(**records));
if (*records == NULL) {
errno = ENOMEM;
@@ -37,10 +45,19 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
goto cleanup;
}
enum Fields err;
enum Fields err = NONE;
int ret = RecordObjectUnpack(objp, buf, &err);
if (ret == -1) {
/* Same diagnostic shape as list(): identify the snapshot, the line and
the offending field. A malformed record is not a system error, so it
gets its own status and main() leaves errno alone. */
if (err != NONE)
fprintf(stderr, "%s:%d: invalid %s\n", file, i + 1,
RecordObjectFieldName(err));
else
fprintf(stderr, "%s:%d: unparsable line\n", file, i + 1);
RecordObjectRelease(objp);
rc = 128;
goto cleanup;
}
@@ -57,8 +74,7 @@ cleanup:;
const int saved_errno = errno;
if (stream != NULL)
fclose(stream);
if (buf != NULL)
free(buf);
free(buf);
errno = saved_errno;
return rc;
}
@@ -83,15 +99,17 @@ int diff(const char *file1, const char *file2) {
struct RecordObject **records2 = NULL;
int len1 = 0;
if (load_in_memory(file1, &records1, &len1) == -1)
if ((rc = load_in_memory(file1, &records1, &len1)) != 0)
goto cleanup;
int len2 = 0;
if (load_in_memory(file2, &records2, &len2) == -1)
if ((rc = load_in_memory(file2, &records2, &len2)) != 0)
goto cleanup;
qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path);
qsort(records2, (size_t)len2, sizeof(records2[0]), order_by_path);
if (records1 != NULL)
qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path);
if (records2 != NULL)
qsort(records2, (size_t)len2, sizeof(records2[0]), order_by_path);
int m = 0, n = 0;
while (m < len1 && n < len2) {
@@ -149,6 +167,13 @@ int diff(const char *file1, const char *file2) {
n++;
}
/* stdout is block-buffered when redirected, so a write error can surface
only here. */
if (fflush(stdout) == EOF) {
rc = -1;
goto cleanup;
}
rc = 0;
cleanup: