-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Remove undefined behavior and add stricter checks in console arg parsing #3613
Conversation
I'll remove the use of |
f6739e7
to
5078619
Compare
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.
Besides my really minor comment, LGTM. Up to you do decide to address it or not. Feel free to merge after.
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 just realized the CI is red :')
Hi @kunaltyagi The cast of max to long int gives -1: I tried to change it to long unsigned int - but that didn't work well with the -314 case :) |
That's a nice find. I should have seen it coming. Dang it Windows. At least it tells me I need to change my SFINAE. Thanks for the hint @larshg |
What does it return linux/unix side ? Max of singed int? ie. 2,147,483,647 or? |
On my system, |
Ahh, yeah its equal here. |
Oh well, now I can spend atleast 1 PR strengthening the |
@larshg Since you have a Windows PC, could you check the latest push? It compiles for me without issues. |
👍 yes, it compiles and unit tests passes. |
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.
At first sight this feels slightly over engineered :') Thank you for chiming in @larshg
Yeah, that's true. Net result: ~300 lines added. Positive side: 200 lines are tests. The original implementation had several silent failures (on edge cases) such as
Step 1: Use
|
I just copied your explanation to the OP. Future people will appreciate it. :) |
Started with a wish to reduce duplication, but grew into a monster than has better semantics, stricter bound checking and fewer UB along with less duplicated code.
The original implementation had several silent failures (on edge cases) such as
atoi
andatol
unsigned int
(negative numbers became large positive numbers)sizeof (int) == sizeof (long int)
forunsigned int
Step 1: Use
strtoX
Usage of
strtoX
allowed error handling, but needed to be separated in a template-function to prevent duplication of logic. 3 sub-cases were needed for minimal overhead:double
,float
,long int
were simple enoughint
,unsigned int
) needed bounds checkedStep 2: Check bounds for integral types
It was still possible to have just 2 simple functions, but
Step 3: Now do it in cross-platform manner
This worked for LP64, but failed on LLP64. Current solution should work with all 64-bit memory models.
std::conditional
all over, which I didn't like).if constexpr
wasn't an option in C++14Final implementation
100 lines which implement:
errorno
checkingAnd @larshg was nice enough to debug the platform dependent issue. Wouldn't have caught it for a few more days without him