Skip to content

fix: validate integer fields in SamParser to prevent silent atoi() corruption#51

Open
ZaGrayWolf wants to merge 3 commits intocompiler-research:developfrom
ZaGrayWolf:fix/samparser-integer-validation
Open

fix: validate integer fields in SamParser to prevent silent atoi() corruption#51
ZaGrayWolf wants to merge 3 commits intocompiler-research:developfrom
ZaGrayWolf:fix/samparser-integer-validation

Conversation

@ZaGrayWolf
Copy link
Copy Markdown

Fixes #22, closes #26

Problem

SamParser::ParseLine() uses atoi() for five integer fields: flag (field 1),
pos (field 3), mapq (field 4), pnext (field 7), tlen (field 8). atoi() returns
0 silently on non-numeric input — malformed SAM records are written as
zero-valued integers with no warning, corrupting the output RNTuple.

Fix

  • Added ParseInt() helper using strtol() for proper validation
  • Logs a warning to std::cerr identifying the field name and line number
  • Returns false to skip the malformed record entirely
  • Replaced all five atoi() calls in ParseLine()

Tests

Existing test suite passes (3/3). Regression tests for malformed input
to be added in a follow-up commit.

Status

Draft — opening for early review and CI validation.

Comment thread src/ramcore/SamParser.cxx
bool SamParser::ParseLine(char* line, SamRecord& record) {
int field_num = 0;
char* token = strtok(line, "\t");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo all whitespace changes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Removed trailing whitespace from blank lines in the amended commit. Added 6 regression tests via ParseFile() (since ParseLine is private), all 16 tests pass including the 6 new ones.

- Add ParseInt() helper using strtol(): validates numeric input, logs
  warning to std::cerr with field name and line number, returns false
  to skip malformed record
- Replace atoi() calls for flag (field 1), pos (field 3), mapq (field 4),
  pnext (field 7), tlen (field 8)
- Malformed records are now skipped with a diagnostic message instead of
  being silently written as zero-valued integers
- Update .gitignore to exclude reference genome files and large data files

Fixes compiler-research#22, closes compiler-research#26
@ZaGrayWolf ZaGrayWolf force-pushed the fix/samparser-integer-validation branch from 3c938b1 to 0a14b5e Compare April 11, 2026 14:08
ZaGrayWolf and others added 2 commits April 11, 2026 20:04
Six tests via ParseFile() covering all five integer fields (flag, pos,
mapq, pnext, tlen) for malformed input and one valid record check.
Also fix CMakeLists.txt keyword mixing and ROOT 6.38 namespace in
SamToNTuple.cxx (pre-existing build breaks, not related to PR logic).

Closes compiler-research#26
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.

Improve testing coverage Bug: SamParser silently corrupts data when encountering non-numeric values in integer fields

2 participants