pacman82 / atoi-rs

Parse integers directly from `[u8]` slices in safe code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrapping and panics in debug mode on overflow (round 2)

niklasf opened this issue · comments

Hi, sorry to resurrect this issue (#1). I changed my mind on it: The wrapping (and panicking in debug mode) behavior seems to be a bad default in practise and probably even better documentation still has a good chance to be overlooked.

All published dependent crates of atoi seem to be incorrectly using it in network protocols.

https://github.com/carllerche/tower-web/blob/2fb07a75272a120f0bffc165961b8d5d9c8c5fb1/src/extract/num.rs#L32

https://github.com/oezgurakkurt/http-codec/blob/e8b6176d6e912af6aab465127d6cebd6e9e2455e/src/server.rs#L88

https://github.com/blackbeam/rust_mysql_common/blob/63d46d35ea33094942aea2b44812385a7ddb991b/src/value/convert.rs#L168

It would be much better to opt in to wrapping arithmetic and let checked arithmetic be the default. A while ago I made a fork as a proof of concept.

Alternatively we could also just close this and report these issues downstream.

Hi @niklasf , thanks for your research and feedback. I apologize for taking so long to answer. What do you think about adding an extra trait FromRadix10Checked? I would like to keep the old unchecked trait as it works fine with types which can not overflow like BigInt.
The atoi convinience function would use the new trait, by default though.

As an implementation note: I think we can keep performance impact to a minimum, because we know the maximum number of digits at compile time and only the last digit is at risk of overflowing (and for more digits we know that we would always overflow).

However, I do not know when I will get around to do this.

Sounds excellent. That should solve this once and for all.

Version 0.3.0 now performs overflow checking in a performant manner. I am closing this issue.