Repository navigation
Commit 258fd81
suoyi
fix: reject values at or above 2^63 in int parameter parsing
toInt's upper bound compares against math.MaxInt, which is
2^(strconv.IntSize-1) - 1 and cannot be represented as a float64: on 64-bit
targets it rounds up to exactly 2^63, so a value that parses to 2^63 is not
greater than the bound and passed the check. It then reached int(f), and
converting an out-of-range float64 to int is implementation-defined - MinInt on
amd64 - so the parameter was accepted and a large negative number was sent to
the API instead of failing the range check.
That covered every caller of RequiredInt, OptionalIntParam,
OptionalIntParamWithDefault and OptionalPaginationParams, including the string
"9223372036854775807", which ParseFloat rounds to 2^63.
Compare against math.Ldexp(1, strconv.IntSize-1) instead. It is the exact power
of two on both 32- and 64-bit targets, so the bound holds everywhere and the
conversion is never reached with an out-of-range value - unlike inspecting the
result of the conversion, which only catches the overflow on targets that do not
saturate.1 parent c59e8c1 commit 258fd81
2 files changed
Lines changed: 36 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
87 | 94 | | |
88 | 95 | | |
89 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
379 | 407 | | |
380 | 408 | | |
381 | 409 | | |
| |||
0 commit comments