fix: validate integer fields in SamParser to prevent silent atoi() corruption#51
Open
ZaGrayWolf wants to merge 3 commits intocompiler-research:developfrom
Open
Conversation
vgvassilev
reviewed
Mar 28, 2026
| bool SamParser::ParseLine(char* line, SamRecord& record) { | ||
| int field_num = 0; | ||
| char* token = strtok(line, "\t"); | ||
There was a problem hiding this comment.
Please undo all whitespace changes.
Author
There was a problem hiding this comment.
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
3c938b1 to
0a14b5e
Compare
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22, closes #26
Problem
SamParser::ParseLine()usesatoi()for five integer fields: flag (field 1),pos (field 3), mapq (field 4), pnext (field 7), tlen (field 8).
atoi()returns0 silently on non-numeric input — malformed SAM records are written as
zero-valued integers with no warning, corrupting the output RNTuple.
Fix
ParseInt()helper usingstrtol()for proper validationstd::cerridentifying the field name and line numberfalseto skip the malformed record entirelyatoi()calls inParseLine()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.