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.