Repository navigation
Commit dded1b5
authored
uucore: bound hex float exponent to avoid BigDecimal scale overflow (#14845)
* uucore: bound hex float exponent to avoid BigDecimal scale overflow
A hexadecimal float whose binary exponent fits in an i64 but is very large
(e.g. 0x1p-9223372036854775808) made BigDecimal::powi overflow its i64 scale,
which panics in debug builds and silently wraps in release builds.
Treat such exponents as overflow/underflow, like the decimal branch does for
scales outside the i32 range: since 2**4 > 10, any |exponent| > 4 * i32::MAX
gives a decimal exponent that cannot fit in an i32 either.
Fixes #14835
* uucore: tighten hex float exponent bound to i32::MAX * log2(10)
The previous bound of 4 * i32::MAX was loose (log2(10) is ~3.32), so
exponents such as 0x1p8000000000 still went through powi. Use
floor(i32::MAX * log2(10)) instead, which matches the i32 decimal
exponent limit applied to decimal inputs, and test right at the limit.
* Keep the hex exponent bound explanation in one place
The comment where the bound is applied repeated the MAX_HEX_EXPONENT doc. Move the whole reasoning into the constant's doc and point to it from the call site.
* Put the hex exponent limit value in backticks for doc_markdown
---------
Co-authored-by: Amarnath10i <amarnath10i@users.noreply.github.com>1 parent f085992 commit dded1b5
2 files changed
Lines changed: 69 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
405 | 412 | | |
406 | 413 | | |
407 | 414 | | |
| |||
453 | 460 | | |
454 | 461 | | |
455 | 462 | | |
456 | | - | |
457 | | - | |
| 463 | + | |
458 | 464 | | |
459 | 465 | | |
| 466 | + | |
460 | 467 | | |
461 | 468 | | |
462 | 469 | | |
463 | 470 | | |
464 | | - | |
465 | | - | |
466 | 471 | | |
467 | 472 | | |
468 | 473 | | |
| |||
591 | 596 | | |
592 | 597 | | |
593 | 598 | | |
594 | | - | |
| 599 | + | |
595 | 600 | | |
596 | 601 | | |
597 | 602 | | |
| |||
1035 | 1040 | | |
1036 | 1041 | | |
1037 | 1042 | | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
1038 | 1090 | | |
1039 | 1091 | | |
1040 | 1092 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
499 | 499 | | |
500 | 500 | | |
501 | 501 | | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
502 | 514 | | |
503 | 515 | | |
504 | 516 | | |
| |||
0 commit comments