Skip to content

Serialize ints below the 4-byte range through the long path - #443

Open
abhishekcpp007 wants to merge 1 commit into
pytest-dev:mainfrom
abhishekcpp007:fix/serialize-negative-big-ints
Open

abhishekcpp007 wants to merge 1 commit into
pytest-dev:mainfrom
abhishekcpp007:fix/serialize-negative-big-ints

Conversation

@abhishekcpp007

Copy link
Copy Markdown

_save_integral only checked the upper bound, so any int below -2**31 went down the 4-byte INT/LONG opcode path and struct.pack("!i", i) raised a bare struct.error. Positive values above 2**31 - 1 were already routed through LONGINT/LONGLONG, so 2**40 round-tripped and -2**40 did not.

>>> import execnet
>>> execnet.loads(execnet.dumps(2**40))
1099511627776
>>> execnet.loads(execnet.dumps(-2**40))
struct.error: 'i' format requires -2147483648 <= number <= 2147483647

Over a gateway the same thing shows up as a RemoteError ending in _write_int4 ... struct.error from channel.send(-2**40), while channel.send(2**40) arrives fine. A negative nanosecond timestamp or a hash value gets there easily.

The fix adds FOUR_BYTE_INT_MIN and checks both bounds in _save_integral, so anything outside the 4-byte range takes the string-backed long path. _write_int4 now rejects values below the minimum with DumpError, the library's own exception, instead of leaking struct.error. The wire format is unchanged: load_longint already does int(s) and handles the sign, so an old receiver reads the new output.

Tests: 2**31, -(2**31) - 1, 10**30 and -(10**30) join the TestSerializeAPI matrix in testing/test_basics.py (dumps/loads, mmap and BytesIO), and test_negative_long in testing/test_serializer.py mirrors test_long for the cross-interpreter path.

Verified in a python:3.12 container (pip install pytest -e ., gevent/eventlet deselected):

_save_integral only checked the upper bound, so anything below -2**31 reached struct.pack('!i') and raised a bare struct.error while the same magnitude positive value took the LONGINT path. Check both bounds, and have _write_int4 raise DumpError for either side.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant