-
Notifications
You must be signed in to change notification settings - Fork 342
[All] Security: unsafe string operations #5902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[All] Security: unsafe string operations #5902
Conversation
…handle invalid input and overflow conditions in string-to-number conversions
…t prevent buffer overflows
0c9de53 to
8d9a23b
Compare
bakpaul
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are going to hate me...
| strncpy( dataFile, value, sizeof(dataFile) - 1 ); | ||
| dataFile[sizeof(dataFile) - 1] = '\0'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, thank's Claude
| { | ||
| dataFile[lenWithoutExt] = '\0'; | ||
| strncat( dataFile, "raw", sizeof(dataFile) - lenWithoutExt - 1 ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many if without fallback. Maybe an error message somewhere ?
| r.name1.clear(); | ||
| } | ||
| else | ||
| r.group1 = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no clear ?
| r.name2.clear(); | ||
| } | ||
| else | ||
| r.group2 = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no clear ?
| errno = 0; | ||
| double val = std::strtod(c.c_str(), &endptr); | ||
| if (errno == 0 && endptr != c.c_str()) | ||
| values.push_back((Real)val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if, no fall back and no message...
| namespace sofa::helper::deque_detail | ||
| { | ||
| inline bool safeStrToInt(const std::string& s, int& result) | ||
| { | ||
| char* endptr = nullptr; | ||
| errno = 0; | ||
| long val = std::strtol(s.c_str(), &endptr, 10); | ||
| if (errno != 0 || endptr == s.c_str() || val < INT_MIN || val > INT_MAX) | ||
| return false; | ||
| result = static_cast<int>(val); | ||
| return true; | ||
| } | ||
|
|
||
| inline bool safeStrToUInt(const std::string& s, unsigned int& result) | ||
| { | ||
| char* endptr = nullptr; | ||
| errno = 0; | ||
| unsigned long val = std::strtoul(s.c_str(), &endptr, 10); | ||
| if (errno != 0 || endptr == s.c_str() || val > UINT_MAX) | ||
| return false; | ||
| result = static_cast<unsigned int>(val); | ||
| return true; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this could have eased all of the previous boiling plate code that I reviewed. Could you kindly ask Claude to put that in a util file and use it everywhere instead of the crappy boiling plate code everywhere
| namespace | ||
| { | ||
| bool safeStrToInt(const std::string& s, int& result) | ||
| { | ||
| char* endptr = nullptr; | ||
| errno = 0; | ||
| long val = std::strtol(s.c_str(), &endptr, 10); | ||
| if (errno != 0 || endptr == s.c_str() || val < INT_MIN || val > INT_MAX) | ||
| return false; | ||
| result = static_cast<int>(val); | ||
| return true; | ||
| } | ||
|
|
||
| bool safeStrToUInt(const std::string& s, unsigned int& result) | ||
| { | ||
| char* endptr = nullptr; | ||
| errno = 0; | ||
| unsigned long val = std::strtoul(s.c_str(), &endptr, 10); | ||
| if (errno != 0 || endptr == s.c_str() || val > UINT_MAX) | ||
| return false; | ||
| result = static_cast<unsigned int>(val); | ||
| return true; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude, c'mon man
| } | ||
| catch (const std::exception&) | ||
| { | ||
| // Skip invalid entries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message
| errno = 0; | ||
| long parsed = std::strtol(str, &endptr, 10); | ||
| if (errno != 0 || endptr == str) | ||
| continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message
| errno = 0; | ||
| long val1 = std::strtol(params[0].c_str(), &endptr, 10); | ||
| if (errno != 0 || endptr == params[0].c_str() || val1 < INT_MIN || val1 > INT_MAX) | ||
| val1 = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message
unhandled exception from different string calls and replace unsafe calls of string functions
[with-all-tests]
By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).
Reviewers will merge this pull-request only if