FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-71810: fix corner case (length==0) for int.to_bytes() by skirpichev · Pull Request #138739 · python/cpython · GitHub

Repository navigation

gh-71810: fix corner case (length==0) for int.to_bytes() - #138739

Merged
vstinner merged 6 commits into
python:mainfrom
skirpichev:int-bytes/71810
Sep 11, 2025
Merged

vstinner merged 6 commits into
python:mainfrom
skirpichev:int-bytes/71810

Conversation

skirpichev commented Sep 10, 2025 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member
>>> (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

```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
```

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

This comment was marked as resolved.

Copy link
Copy Markdown
Member

@serhiy-storchaka: I would suggest to backport this change to Python 3.13 and 3.14. What do you think?

skirpichev requested a review from vstinner September 10, 2025 11:55

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

serhiy-storchaka added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Sep 10, 2025

Copy link
Copy Markdown
Member Author

Disclaimer:
This pr implements "the minimal change necessary to remove ambiguity" (c) @mdickinson

I was thinking about (0).to_bytes(0, 'big', signed=True) too, but that means rejecting also int.from_bytes(b"", ..., signed=True). That case looks for me as a special case, that aren't special enough to warrant compatibility break.

Copy link
Copy Markdown
Member

Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2.

Copy link
Copy Markdown
Member Author

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).

Done.

There are such tests, but rather for "automatic" value for length parameter (determined by result):

def test_to_bytes(self):

Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2.

It seems to be a big refactoring of tests. Maybe it does make sense as a separate pr?

Comment thread Lib/test/test_long.py Outdated
Comment thread Lib/test/test_long.py Outdated

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I would add much more tests. Anyway, LGTM. 👍

Comment thread Lib/test/test_long.py Outdated
Comment thread Lib/test/test_long.py Outdated
Comment thread Lib/test/test_long.py
skirpichev and others added 2 commits September 11, 2025 10:04
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
vstinner merged commit 011179a into python:main Sep 11, 2025
45 checks passed

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 11, 2025
…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>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 11, 2025
…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>

bedevere-app Bot commented Sep 11, 2025

Copy link
Copy Markdown

GH-138782 is a backport of this pull request to the 3.14 branch.

bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 11, 2025

bedevere-app Bot commented Sep 11, 2025

Copy link
Copy Markdown

GH-138783 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 only security fixes label Sep 11, 2025

Copy link
Copy Markdown
Member

Merged, thank you.

skirpichev deleted the int-bytes/71810 branch September 11, 2025 10:46
vstinner pushed a commit that referenced this pull request Sep 11, 2025
…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>
hugovk pushed a commit that referenced this pull request Sep 12, 2025
…38739) (#138782)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Comment thread Objects/longobject.c
p += pincr;
}
else if (j == n && n > 0 && is_signed) {
else if (j == n && is_signed) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Oh, sorry. Thanks!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

3 participants


Back | FazBrowse Home | New Git URL