Skip to content

Replace rewind() with fseek() for error handling#380

Open
manoj23 wants to merge 1 commit into
dvidelabs:masterfrom
manoj23:upstream/fix-rewind
Open

Replace rewind() with fseek() for error handling#380
manoj23 wants to merge 1 commit into
dvidelabs:masterfrom
manoj23:upstream/fix-rewind

Conversation

@manoj23

@manoj23 manoj23 commented May 22, 2026

Copy link
Copy Markdown

PR: Replace rewind() with fseek() for error handling

Summary

rewind() is equivalent to (void)fseek(fp, 0L, SEEK_SET) and silently discards any seek error. This replaces it with fseek() and checks the return value in readfile.h and fileio.c so that a failed seek propagates to the caller via the existing fail path.

Changes

  • include/flatcc/support/readfile.h: replace rewind(fp) with if (fseek(fp, 0L, SEEK_SET)) goto fail;
  • src/compiler/fileio.c: same change

Motivation

Static analysis (CWE-676) flags rewind() as a potentially dangerous function because it provides no way to detect errors. Both call sites already have a fail label with proper cleanup, so wiring in the error check is straightforward.

Testing

Built and tested with CMake on Linux (gcc/clang). No behavioral change for the success path; the fail path now correctly triggers on seek errors.

rewind() is equivalent to (void)fseek(fp, 0L, SEEK_SET) and silently
discards any seek error. Replace with fseek() and check the return
value in readfile.h and fileio.c so that a failed seek propagates to
the caller via the existing fail path.

Signed-off-by: Georges Savoundararadj <savoundg@amazon.com>
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