-
Notifications
You must be signed in to change notification settings - Fork 835
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
BUG: Integer underflow causes HUGE space() in sendGeneric() #381
Conversation
A mesgtime of zero (or less than the elapsed time to send a message) tickled a bug in `sendGeneric()` due to the subtraction of two unsigned values resulting in a negative number. This bug isn't catchable with our current unit test setup. This was discovered by "on hardware" testing.
e7fc2c6
to
688a6c9
Compare
I'm submitting this as is, as it is rather a show-stopping bug. I don't want to leave |
if (elapsed >= mesgtime) | ||
space(gap); | ||
else | ||
space(std::max(gap, mesgtime - elapsed)); |
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.
you still avoid duplicating the rest of the function so i think its worth it.
if you don't like the if statement another way is using one line if statement like this:
uint32_t space_gap = elapsed > mesgtime ? gap : std::max(gap, mesgtime - elapsed);
space(space_gap);
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.
Agreed. No doubt the de-duplication of the procedure was worth it. Just the road to heck is/was paved with good intentions. ;-)
I don't think changing the code to the one-liner actually saves any compiled code/space. The compiler should do that the optimal anyway. If it does save space, I'll change it. However I think the code is more readable to others with a if
statement verses the conditional assignment.
It doesn't have space I think. I didn't look at it that way just thought
about reading code wise as I thought you didnt like the if-else statement.
Another option is doing max first between the substraction and 0 so you
won't get negative number.
Leaving as is is of course fine :-)
Thanks for the hard work!
…On Tue, Jan 2, 2018, 11:12 David Conran ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/IRsend.cpp
<#381 (comment)>
:
> @@ -340,7 +340,12 @@ void IRsend::sendGeneric(const uint16_t headermark, const uint32_t headerspace,
// Footer
if (footermark) mark(footermark);
- space(std::max(gap, mesgtime - usecs.elapsed()));
+ uint32_t elapsed = usecs.elapsed();
+ // Avoid potential unsigned integer underflow. e.g. when mesgtime is 0.
+ if (elapsed >= mesgtime)
+ space(gap);
+ else
+ space(std::max(gap, mesgtime - elapsed));
Agreed. No doubt the de-duplication of the procedure was worth it. Just
the road to heck is/was paved with good intentions. ;-)
I don't think changing the code to the one-liner actually saves any
compiled code/space. The compiler should do that the optimal anyway. If it
does save space, I'll change it. However I think the code is more readable
to others with a if statement verses the conditional assignment.
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub
<#381 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAoSnvv7PtPvN4MVlOChjJHNBbioaGqOks5tGfMOgaJpZM4RIJex>
.
|
Since `sendJVC()` was updated to use `sendGeneric()` it has produced a double minimum gap at the end of each message, instead of a single one. This wasn't picked up by our unit-testing. This tickled a situation where an integer underflow happened when calculating the required `space()` length, which would cause a delay of approx 1h20m. Refactored code to eliminate this problem, and a potential similar case in `sendWhynter()` too. This is a derivative of the bug/issue #381 This instance was reported in #400, which this patch should fix.
Since `sendJVC()` was updated to use `sendGeneric()` it has produced a double minimum gap at the end of each message, instead of a single one. This wasn't picked up by our unit-testing. This tickled a situation where an integer underflow happened when calculating the required `space()` length, which would cause a delay of approx 1h20m. Refactored code to eliminate this problem, and a potential similar case in `sendWhynter()` too. This is a derivative of the bug/issue #381 This instance was reported in #400, which this patch should fix.
A mesgtime of zero (or less than the elapsed time to send a message) tickled
a bug in sendGeneric() due to the subtraction of two unsigned values.
This bug isn't catchable with our current unit test setup.
This was discovered by "on hardware" testing.
FYI @roidayan That elegant solution wasn't so elegant after all.