Commit e87f50e
Fix Python VarIntCoder OverflowError on uint64 values (#39047)
* [BEAM] Fix Python VarIntCoder OverflowError on uint64 values
The Cython write_var_int64/get_varint_size stream methods take a signed
int64_t parameter. A Python int in the unsigned 64-bit range [2**63,
2**64) -- a uint64 -- is converted to that signed parameter at the call
boundary and rejected with an OverflowError before the method body runs,
even though the body already operates on the unsigned bit pattern and the
VarInt wire encoding is well-defined.
Fold such values to the signed int64 with the identical bit pattern (and
thus identical VarInt encoding) before handing them to the stream. This
matches Java's signed VarIntCoder on the wire; decoding remains signed.
Values past 64 bits are left unchanged and still overflow downstream,
preserving the coder's documented 64-bit limit.
Adds test_varint_coder_uint64 covering no-overflow encoding, wire
equivalence to the signed twin, size estimation, signed decode, and the
still-raising out-of-range case (guarded on the compiled implementation).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Enforce 64-bit range in _as_signed_int64 for path parity
Address review feedback: raise OverflowError in _as_signed_int64 for
values outside the 64-bit range (reusing fits_in_64_bits) so the
pure-Python path matches the Cython int64_t overflow instead of silently
encoding out-of-range values. Both encode_to_stream and estimate_size
already wrap OverflowError, so the user-facing message is unchanged.
Drop the is_compiled guard in test_varint_coder_uint64 so the
out-of-range case runs on both paths, add the negative lower-bound case
(-(2**63) - 1), and remove the now-unused coder_impl import.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Fix uint64 overflow in VarIntCoderImpl.encode fast path
The compiled encode() cast value to a C int64_t (ivalue, typed via
coder_impl.pxd) for the small-ints fast path, which overflowed on uint64
values before reaching encode_to_stream's fold -- the cause of the
test_varint_coder_uint64 CI failure on the Python PreCommit suites.
Do the small-ints check with a plain Python-object comparison so it can't
overflow; non-small values (including uint64 and out-of-range ints) fall
through to StreamCoderImpl.encode -> encode_to_stream, where
_as_signed_int64 folds them and raises the wrapped OverflowError for
genuinely out-of-range values. Drop the now-unused ivalue int64_t local
hint from coder_impl.pxd.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent f08c259 commit e87f50e
3 files changed
Lines changed: 44 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
138 | | - | |
139 | 138 | | |
140 | 139 | | |
141 | 140 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
93 | 107 | | |
94 | 108 | | |
95 | 109 | | |
| |||
1044 | 1058 | | |
1045 | 1059 | | |
1046 | 1060 | | |
1047 | | - | |
| 1061 | + | |
| 1062 | + | |
1048 | 1063 | | |
1049 | 1064 | | |
1050 | 1065 | | |
1051 | | - | |
1052 | | - | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
1053 | 1069 | | |
1054 | 1070 | | |
1055 | 1071 | | |
1056 | 1072 | | |
1057 | 1073 | | |
1058 | 1074 | | |
1059 | 1075 | | |
1060 | | - | |
1061 | | - | |
1062 | | - | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
1063 | 1081 | | |
1064 | 1082 | | |
1065 | 1083 | | |
| |||
1073 | 1091 | | |
1074 | 1092 | | |
1075 | 1093 | | |
1076 | | - | |
| 1094 | + | |
1077 | 1095 | | |
1078 | 1096 | | |
1079 | 1097 | | |
1080 | | - | |
| 1098 | + | |
1081 | 1099 | | |
1082 | 1100 | | |
1083 | 1101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
437 | 437 | | |
438 | 438 | | |
439 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
440 | 458 | | |
441 | 459 | | |
442 | 460 | | |
| |||
0 commit comments