-
Notifications
You must be signed in to change notification settings - Fork 277
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
Signed integer overflow in mqtt_error_str()
#176
Comments
The issue isn't noticed normally likely because the compiler reproduces the entire IF statement pretty much as is. Whenever But when optimization is enabled, the compiler does away with the whole subtraction and IF block, because
In other words, in C, the function has been transformed into:
... which should be functionally equivalent to the original ... so long as nobody was depending on certain signed overflows. |
When I modify the function like so:
... everything's fine now, because we force the compiler to acknowledge the possibility that the So it preserves the IF statement, more or less as is (though I didn't check). As a result, we also don't perform any subtraction unless we're certain there won't be an overflow. |
The issue is reproduced as follows:
-Os
) in the compiler (GCC-ARM used here on STM32), before compilingmqtt_error_str(MQTT_OK)
anytime"MQTT_OK"
, the string that is returned is"MQTT_ERROR_NULLPTR"
everytime.It happens because a signed overflow occurs during this subtraction:
MQTT_OK
== 1,MQTT_ERROR_UNKNOWN
==INT_MIN
.1 - INT_MIN
is undefined, for the current 32-bit-int representation. In fact, that goes for any number greater than or equal to 0, as explained here.The text was updated successfully, but these errors were encountered: