Skip to content

fix(tfile): read_tfile() returns 0 on error instead of 1 (F-23, #99)#158

Merged
adequatelimited merged 1 commit intoaudit-fixesfrom
audit/F-23-read-tfile-error-return
Apr 13, 2026
Merged

fix(tfile): read_tfile() returns 0 on error instead of 1 (F-23, #99)#158
adequatelimited merged 1 commit intoaudit-fixesfrom
audit/F-23-read-tfile-error-return

Conversation

@adequatelimited
Copy link
Copy Markdown
Collaborator

Summary

Closes #99 (F-23).

`read_tfile()` returns `size_t` but previously returned `VERROR` (==1) on `fopen()` failure. `1` is also a valid count for "one trailer read successfully." Callers that check `!= 1` or `<= 0` silently treated the error as success and proceeded with uninitialized buffer data on consensus-critical paths (`ng_val`, `contention`, `send_found`).

This change returns `0` on error. All existing callers already check `!= 1`, `<= 0`, or `!= NTFTX`, which correctly detect `0`.

Also wires up the pre-existing TODO in `send_found()` to check `count != NTFTX` and exit the forked child on incomplete read.

Test plan

  • Unit test `src/test/tfile-read-error.c` covers three cases: missing file (returns 0), partial read (returns actual count), seek past EOF (returns 0). Validated to fail with pre-fix code and pass with post-fix.
  • Full build clean under `-Wall -Werror -Wextra -Wpedantic`.
  • Live mainnet sync from genesis through 600+ blocks with trace instrumentation to confirm no regression on happy path (`read_tfile` callers exercised via `refresh_ipl`, `send_found`, and OP_GET_TFILE service).

…1-record read

read_tfile() returns size_t but previously returned VERROR (==1) on
fopen() failure, which is indistinguishable from a successful read of
one trailer. Callers that check `!= 1` or `<= 0` silently accepted the
error as success and continued with uninitialized buffer data on the
consensus-critical paths (ng_val, contention, send_found).

Return 0 on error instead. All existing callers already check `!= 1`,
`<= 0`, or `!= NTFTX` which correctly detect 0.

Also wire up the pre-existing TODO in send_found() to check count !=
NTFTX and exit the forked child on incomplete read.

Unit test in src/test/tfile-read-error.c covers the three cases:
missing file, partial read, and seek-past-EOF.
@adequatelimited adequatelimited merged commit 24a08d9 into audit-fixes Apr 13, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant