Out of range bugfix - #167
Conversation
Fixed the std::out_of_range exception that was happening in the unit tests for the functions: - bin_to_dec - oct_to_dec
himanshigaba22
left a comment
There was a problem hiding this comment.
Good catch and a clean fix - switching to stoul before narrowing to unsigned int correctly avoids the out_of_range thrown by stoi on values between INT_MAX and UINT_MAX. Two small things:
Since stoul only throws for values exceeding unsigned long, an input that overflows unsigned int but fits in unsigned long will now silently truncate instead of throwing -probably fine, but worth a quick comment noting that's accepted, or a range check if malformed input is a concern here.
Would it make sense to add a regression test covering the specific inputs that used to throw, so this doesn't regress silently in the future?
Also using std::stoul fully-qualified where the surrounding code calls stoi unqualified - minor, just flagging for consistency.
Added checks in the `bin_to_dec' and 'oct_to_dec' functions for when the input string length is larger than the maximum value a unsigned int can hold. If it's above a 32-bit value it will just return the max value of an unsigned int. Added tests to verify that the max value of unsigned int is returned when above inputs are given
Added a check to return the max value of an unsigned int if the input string would be greater than it
This was the only call to a string to integer type that was not fully qualified in this file to add consistency
|
Appreciate the feedback. So I've added some checks to the functions that make sure that if inputs are going over the UINT boundary it will just return the UINT max value. I've added try blocks around the return calls in the case of an unexpected error and will just return 0. As for the regression test I can check about that but I added some tests in to make sure the output values were correct for the checks that I added. I also made the call to stoi underneath one of the functions fully qualified as all other calls to similar functions in the file that were fully qualified |
rory-cd
left a comment
There was a problem hiding this comment.
General Information
Type of Change:
- Bug fix
Nice work! Truncating large values seems sensible. I tested on WSL and it seems to resolve the issue. Just a couple of minor things:
Requested changes:
- As @himanshigaba22 noted, the truncation is silent, so the function description/comment in basics.h should probably be updated so users know what to expect.
- The code formatting needs to be fixed up a little, e.g.
if(should beif (and some of the indenting is inconsistent (like the return statements).
Optional:
One other consideration is the digit-counting approach. Using if(bin_str.length() > 32) works, but may be a bit brittle (for example with leading zeroes). I'd suggest another method of clamping, like perhaps parsing to a long long and then clamping if it exceeds numeric_limits<unsigned int>::max(). If it can't be parsed into long long then it could be caught and handled (out of range exception).
Code Quality
- Repository: Thoth-Tech NOT SplashKit
- Readability: Is the code easy to read and follow? If not are there comments to help understand the code?
- Maintainability: Can this code be easily maintained or extended in the future?
Functionality
- Correctness: Does the code meet the requirements of the task?
- Impact on Existing Functionality: Has the impact on existing functionality been considered and tested?
Testing
- Test Coverage: Are unit tests provided for new or modified code?
- Test Results: Have all tests passed?
Documentation
- Documentation: Are both inline and applicable external documentation updated and clear?
Pull Request Details
- PR Description: Is the problem being solved clearly described?
- Checklist Completion: Have all relevant checklist items been reviewed and completed?
Added extra documentation to the basics.h to reflect changes to oct_to_dec and bin_to_dec
Changed the way that clamping worked in the bin_to_dec function to convert the value first then check if the value is larger than unsigned int. Changed unit tests to reflect the changes
Corrected the formatting inconsistency in bin_to_dec and oct_to_dec
Changed the way that oct_to_dec clamps the value to work like bin_to_dec. Adjusted the unit test to reflect these changes
|
Apologies for not adjusting the comment in the header file. I have adjusted them with the changes requested. I'm not sure why there is a problem with the return statements being incorrectly formatted, I will look into it more as it might have to do with my tab settings |
cf2d25a to
2d7cf8d
Compare
jankiluitel
left a comment
There was a problem hiding this comment.
Thanks for working on this bug fix. Replacing std::stoi with std::stoul and adding handling for values larger than unsigned int is a good improvement, and the additional boundary tests help verify the intended behaviour.
I did notice one inconsistency in the implementation:
bin_to_dec() and oct_to_dec() now catch exceptions and clamp values that exceed unsigned int::max(), but hex_to_dec() still directly calls std::stoi() without similar overflow handling. This means hexadecimal values outside the supported range could still throw an exception or behave differently from the binary and octal implementations.
For consistency across the conversion functions, I'd recommend updating hex_to_dec() to use the same pattern (std::stoul() with range checking and exception handling) unless there's a specific reason for the different behaviour.
Other than that, the changes look well structured and the new tests are a welcome addition.
Added to hex_to_dec documentation that there will be truncation if result is greater than the max value of unsigned int to the max of unsigned int. If the input is out of range of an unsigned long then 0 will be returned
Changed the hex_to_dec function to behave the same way as the other _to_dec functions in basics
Added integration tests for hex_to_dec to validate outputs and correctness
Corrected formatting problems
|
Thanks for the feedback. I updated the I also added tests for the |
jankiluitel
left a comment
There was a problem hiding this comment.
Thanks for addressing the review feedback. The conversion functions are now consistent in their overflow handling, and the additional tests provide good coverage for boundary and invalid input cases. The implementation looks good to me. Approved.
Description
Fixed the std::out_of_range exception that was happening in the unit tests for the functions:
bin_to_decoct_to_decThis problem that was causing the exception to be thrown was the call to
std::stoiin the functions above which were returning anunsigned int. This was resolved by changingstd::stoitostatic_cast<unsigned int>(std::stoul(...))to have the correct return type and avoid a-Werror-conversionflag.Type of change
How Has This Been Tested?
Both sktest and skunit_tests were ran and no exceptions were thrown and no tests were failed for the two functions that were changed.
Testing Checklist
Checklist