Commit 1c73890
fix saturating float-to-int cast (#4573)
The issue is that Kani computes int_max_as_float by casting u64::MAX to
f64. But u64::MAX (2^64 - 1) isn't exactly representable in f64, so it
rounds *up* to 2^64. The old code used a greater-than operation, which
would mean a float value equal to exactly 2^64 fails this check, falls
through the raw truncation path, and produces a wrong result instead of
saturating to u64::MAX.
Instead we use greater-than-or-equal so that the boundary value 2^64
correctly saturates. This is safe for types where the max IS exactly
representable.
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
Co-authored-by: Felipe R. Monteiro <felisous@amazon.com>1 parent 1f83c79 commit 1c73890
2 files changed
Lines changed: 19 additions & 3 deletions
File tree
- kani-compiler/src/codegen_cprover_gotoc/codegen
- tests/kani/Cast
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1198 | 1198 | | |
1199 | 1199 | | |
1200 | 1200 | | |
1201 | | - | |
| 1201 | + | |
1202 | 1202 | | |
1203 | 1203 | | |
1204 | 1204 | | |
| |||
1208 | 1208 | | |
1209 | 1209 | | |
1210 | 1210 | | |
1211 | | - | |
| 1211 | + | |
1212 | 1212 | | |
1213 | 1213 | | |
1214 | 1214 | | |
1215 | | - | |
| 1215 | + | |
1216 | 1216 | | |
1217 | 1217 | | |
1218 | 1218 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
0 commit comments