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

@@ -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;
}