-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
Stream::parseFloat() fails if it reads too many digits #129
Comments
I see two options for fixing this issue:
Any thoughts? Opinions? Should this be fixed? Which strategy brings the right (in the Arduino context) balance between cost and benefit? Would the maintainers consider a pull-request? |
Hi @edgar-bonet 👋 Given the fact that we now got unit tests automatically built and executed upon each push to the repository - could you create a PR extending test_parseFloat.cpp to include a failing test for this specific issue? |
Hello @aentinger, and thank you for your response. I am not familiar with this form of test-driven development, but it looks to me like a very interesting approach! I managed to make the test-suite run on my computer, and wrote the pull request for this issue: #133 |
Fixed by pull request #133 (merged). Thanks @aentinger! |
Stream::parseFloat()
builds along
integer out of the digits it reads, skipping the decimal point, and then it scales it by the appropriate power of 0.1. For example,"3.14"
is interpreted as 314 × 0.12.If the user provides too many digits after the decimal point, the integer overflows, which triggers undefined behavior and makes the function return garbage. For example,
"3.141592654"
is read as 3141592654 × 0.19, but the integer overflows to −1153374642, andStream::parseFloat()
returns −1.1534.It could be argued that it is the user's fault, and that they should just not provide more digits than needed by the resolution of the
float
data type. The users, however, are not always in control of the format of the data they want to process. I have found an unsuspecting user bitten by this issue.Test sketch: This uses a class that makes an input
Stream
out of a string, in order to not be dependent on the serial monitor.Output:
The text was updated successfully, but these errors were encountered: