gh-71810: fix corner case (length==0) for int.to_bytes()#138739
gh-71810: fix corner case (length==0) for int.to_bytes()#138739vstinner merged 6 commits intopython:mainfrom
Conversation
```pycon
>>> (0).to_bytes(0, 'big', signed=True)
b''
>>> (-1).to_bytes(0, 'big', signed=True) # was b''
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
(-1).to_bytes(0, 'big', signed=True)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert
```
This comment was marked as resolved.
This comment was marked as resolved.
|
@serhiy-storchaka: I would suggest to backport this change to Python 3.13 and 3.14. What do you think? |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
It seems there are no enough tests for signed=True. Please add also tests for (128).to_bytes(0, 'big', signed=True) and (-129).to_bytes(0, 'big', signed=True).
|
Disclaimer: I was thinking about |
|
Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2. |
Done. There are such tests, but rather for "automatic" value for length parameter (determined by result): Line 1262 in 1ce0553
It seems to be a big refactoring of tests. Maybe it does make sense as a separate pr? |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
I would add much more tests. Anyway, LGTM. 👍
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
|
Thanks @skirpichev for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
…nGH-138739) ```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…nGH-138739) ```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
|
GH-138782 is a backport of this pull request to the 3.14 branch. |
|
GH-138783 is a backport of this pull request to the 3.13 branch. |
|
Merged, thank you. |
…38739) (#138783) gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-138739) ```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
| p += pincr; | ||
| } | ||
| else if (j == n && n > 0 && is_signed) { | ||
| else if (j == n && is_signed) { |
There was a problem hiding this comment.
This change introduced an undefined behavior: unsigned char msb = *(p - pincr); read is out of the bounds if n == 0. I wrote #138873 to fix it.
Uh oh!
There was an error while loading. Please reload this page.