Skip to content

Conversion check: fix off-by-one error for float-to-unsigned #8157

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions regression/cbmc/overflow/float_conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdint.h>

int main()
{
uint32_t u = 0xffffffffu;

// since the double type seems to be implemented with a IEEE 754 binary64
// format in CBMC, which has 53 bits of mantissa, double has enough bits to
// represent the exact value of u, use, e.g., http://weitz.de/ieee/ to check;
// therefore, C17, section 6.3.1.4, paragraph 2 says that this is
// defined behavior and d should store the exact value that u stores
double d = u;

// defined behavior behavior as well, by C17 section 6.3.1.4, paragraph 1,
// because the 'unsigned' type can represent the value; however,
// cbmc --conversion-check used to complain
u = (uint32_t)d;

return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc/overflow/float_conversion.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
float_conversion.c

^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
2 changes: 1 addition & 1 deletion src/ansi-c/goto_check_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ void goto_check_ct::conversion_check(const exprt &expr, const guardt &guard)
{
// Note that the fractional part is truncated!
ieee_floatt upper(to_floatbv_type(old_type));
upper.from_integer(power(2, new_width) - 1);
upper.from_integer(power(2, new_width));
const binary_relation_exprt no_overflow_upper(
op, ID_lt, upper.to_expr());

Expand Down