fix: replace atoi() with strict integer parsing in SamParser (#22)#37
fix: replace atoi() with strict integer parsing in SamParser (#22)#37lukedev45 wants to merge 2 commits intocompiler-research:developfrom
Conversation
|
Thanks for this, Can you explore if this will be better (https://en.cppreference.com/w/cpp/string/byte/strtol.html). |
|
No problem I will take a look and see |
|
The short answer is yes. I'll work on implementing it now. |
| @@ -1,9 +1,33 @@ | |||
| #include "ramcore/SamParser.h" | |||
There was a problem hiding this comment.
warning: 'ramcore/SamParser.h' file not found [clang-diagnostic-error]
#include "ramcore/SamParser.h"
^There was a problem hiding this comment.
@lukedev45 please take a look here and fix the conflicts.
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (71.87%) is below the target coverage (85.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## develop #37 +/- ##
===========================================
+ Coverage 55.85% 56.06% +0.20%
===========================================
Files 16 16
Lines 1425 1452 +27
Branches 752 771 +19
===========================================
+ Hits 796 814 +18
- Misses 521 524 +3
- Partials 108 114 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
This needs rebasing and tests. |
…r-research#22) atoi() silently returns 0 for non-numeric strings, causing corrupted SAM records to be written with zeroed-out integer fields. Replace all five atoi() calls with a StrictAtoi wrapper using std::stoi that validates input and rejects records with non-numeric FLAG, POS, MAPQ, PNEXT, or TLEN values.
strtol works directly with the char* tokens from strtok, avoiding the heap allocation from std::string construction and exception overhead on every call. This is more efficient for a hot parsing loop and fits the C-style pattern already used throughout SamParser.
d8c0701 to
1993695
Compare
Summary
atoi()calls inSamParser::ParseLinewith aStrictAtoihelper that usesstd::stoiwith full validationFLAG,POS,MAPQ,PNEXT,TLEN) are now rejected instead of silently written with zeroed-out valuesSamParserRejectsNonNumericFields) verifying that invalid records are skipped while valid records in the same file are still processedProblem
atoi()returns0for non-numeric input without signaling an error. This caused silent data corruption: malformed SAM records were written to ROOT files with integer fields set to0, with no warning to the user.Solution
A static
StrictAtoifunction wrapsstd::stoiand:"123abc")std::invalid_argumentfor non-numeric stringsstd::out_of_rangefor values exceedingintlimitsstderridentifying the field and invalid valuefalsesoParseLineskips the recordTest plan
cmake --build build && cd build && ctest --output-on-failurepasses all testsCloses #22