Stop reporting an allocation failure as a malformed record

RecordObjectUnpack returned -1 both for a bad record and for a failed
allocation inside itself, and only the first case set *err. list() read
that as "no field identified, so the line is unparsable" and printed a
parse error for a perfectly valid snapshot, exiting 1 instead of
reporting a system error. The errno == EINVAL protocol that already told
the two apart inside unescape_path stopped at the function boundary.

With calloc made to fail on a valid two-record snapshot:

  before:  good.fsnap:1: unparsable line
  after:   list failed: Cannot allocate memory

Give every malformed-record exit errno EINVAL through a small malformed()
helper, set ENOMEM explicitly where calloc fails (the C standard does not
require calloc to set it, and RecordObjectNew returned NULL without it),
and have both callers branch on errno. Document the contract in types.h.

Verified by injecting calloc failures at every allocation site in list
and diff: all of them now surface as ENOMEM, and the parse diagnostics
for the SPEC section 15 cases are unchanged.

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:20:20 +02:00
parent 594dcfe62c
commit 14f6d8aca8
4 changed files with 74 additions and 68 deletions

View File

@@ -21,12 +21,16 @@ const char *RecordObjectFieldName(enum Fields f);
struct RecordObject;
/* Allocate a new RecordObject. Returns NULL on allocation failure. */
/* Allocate a new RecordObject. Returns NULL on allocation failure,
with errno set to ENOMEM. */
struct RecordObject *RecordObjectNew(void);
/* Unpack a pipe-delimited record string into self.
Returns 0 on success, -1 on failure. If err is non-NULL, sets *err to
the field that failed to parse. */
Returns 0 on success, -1 on failure. On failure errno is EINVAL when the
record is malformed and something else, ENOMEM in practice, when the
failure is the parser's own; only in the first case is *err meaningful.
If err is non-NULL and the offending field could be identified, sets *err
to that field. */
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err);
/* Populate self by stat-ing the file at path and writing to out.

View File

@@ -46,8 +46,16 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
}
enum Fields err = NONE;
errno = 0;
int ret = RecordObjectUnpack(objp, buf, &err);
if (ret == -1) {
/* A failed allocation inside the parser is not a malformed record and
must not be reported as one; errno tells them apart. */
if (errno != EINVAL) {
RecordObjectRelease(objp);
goto cleanup;
}
/* 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. */

View File

@@ -29,6 +29,7 @@ int list(const char *snapshot) {
size_t size = 0;
records = calloc((size_t)arr_size, sizeof(*records));
if (records == NULL) {
errno = ENOMEM;
rc = -1;
goto out;
}
@@ -39,19 +40,24 @@ int list(const char *snapshot) {
goto out;
}
enum Fields err = NONE;
errno = 0;
int ret = RecordObjectUnpack(objp, buf, &err);
if (err != NONE) {
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1,
RecordObjectFieldName(err));
rc = 128;
RecordObjectRelease(objp);
break;
}
if (ret == -1) {
fprintf(stderr, "%s:%d: unparsable line\n", snapshot, i + 1);
RecordObjectRelease(objp);
/* A failed allocation inside the parser is not a malformed record and
must not be reported as one; errno tells them apart. */
if (errno != EINVAL) {
rc = -1;
RecordObjectRelease(objp);
goto out;
}
if (err != NONE)
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1,
RecordObjectFieldName(err));
else
fprintf(stderr, "%s:%d: unparsable line\n", snapshot, i + 1);
rc = 128;
RecordObjectRelease(objp);
break;
}

View File

@@ -56,8 +56,10 @@ static char *unescape_path(const char *s, char delim) {
const size_t len = strlen(s);
char *out = calloc(len + 1, sizeof(char));
if (out == NULL)
if (out == NULL) {
errno = ENOMEM;
return NULL;
}
size_t j = 0;
for (size_t i = 0; i < len; i++) {
@@ -262,87 +264,73 @@ struct RecordObject {
struct RecordObject *RecordObjectNew(void) {
struct RecordObject *ret = calloc(1, sizeof(*ret));
if (ret == NULL)
if (ret == NULL) {
/* calloc is not required to set errno, and callers report failure
through it. */
errno = ENOMEM;
return NULL;
ret->path = NULL;
ret->target = NULL;
}
return ret;
}
/* Two kinds of failure share the -1 return: a malformed record, which is the
input's fault, and an allocation failure, which is not. They are told apart
by errno, EINVAL for the former, so callers can report a parse error without
swallowing an out-of-memory condition. */
static int malformed(enum Fields *err, enum Fields field) {
if (err)
*err = field;
errno = EINVAL;
return -1;
}
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err) {
char *fields[NFIELDS];
if (split_record(s, fields, '|') == -1)
return -1;
if (parse_file_type(fields[0], &self->file_type) == -1) {
if (err)
*err = FILE_TYPE;
if (split_record(s, fields, '|') == -1) {
errno = EINVAL;
return -1;
}
if (parse_perm(fields[1], &self->perm) == -1) {
if (err)
*err = PERMISSIONS;
return -1;
}
if (parse_file_type(fields[0], &self->file_type) == -1)
return malformed(err, FILE_TYPE);
if (parse_uintmax(fields[2], &self->uid) == -1) {
if (err)
*err = UID;
return -1;
}
if (parse_perm(fields[1], &self->perm) == -1)
return malformed(err, PERMISSIONS);
if (parse_uintmax(fields[3], &self->gid) == -1) {
if (err)
*err = GID;
return -1;
}
if (parse_uintmax(fields[2], &self->uid) == -1)
return malformed(err, UID);
if (parse_intmax_nonnegative(fields[4], &self->size) == -1) {
if (err)
*err = SIZE;
return -1;
}
if (parse_uintmax(fields[3], &self->gid) == -1)
return malformed(err, GID);
if (parse_intmax_nonnegative(fields[5], &self->time) == -1) {
if (err)
*err = TIME;
return -1;
}
if (parse_intmax_nonnegative(fields[4], &self->size) == -1)
return malformed(err, SIZE);
if (parse_intmax_nonnegative(fields[5], &self->time) == -1)
return malformed(err, TIME);
errno = 0;
char *target = unescape_path(fields[6], '|');
if (target == NULL) {
if (errno == EINVAL) {
if (err)
*err = TARGET;
return -1;
}
/* unescape_path already distinguished the two cases through errno. */
if (errno == EINVAL)
return malformed(err, TARGET);
return -1;
}
self->target = target;
if (parse_uintmax(fields[7], &self->fingerprint) == -1) {
if (err)
*err = FINGERPRINT;
return -1;
}
if (parse_uintmax(fields[7], &self->fingerprint) == -1)
return malformed(err, FINGERPRINT);
if (fields[8][0] == '\0') {
if (err)
*err = PATH;
return -1;
}
if (fields[8][0] == '\0')
return malformed(err, PATH);
errno = 0;
char *path = unescape_path(fields[8], '|');
if (path == NULL) {
if (errno == EINVAL) {
if (err)
*err = PATH;
return -1;
}
if (errno == EINVAL)
return malformed(err, PATH);
return -1;
}