Skip to content

gh-134411: assert PyLong_FromLong(x) != NULL when x is known to be small #134415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 21, 2025

Conversation

smurav
Copy link
Contributor

@smurav smurav commented May 21, 2025

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

PyLong_FromLong sets an exception when it fails, so the call to PyObject_SetAttrString is unsafe. We should return NULL instead of switching to Py_XDECREF.

But, as @sergey-miryanov mentioned, this can't fail for integers <255. I think it's also reasonable to add an assertion here instead of adding error handling.

…case and can't return NULL. Added assert for extra confidence.

python#134411 (comment)
@ZeroIntensity ZeroIntensity added skip news needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 21, 2025
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

LGTM, with one little note.

@@ -2558,18 +2558,22 @@ PyObject *_Py_CreateMonitoringObject(void)
err = PyObject_SetAttrString(events, "NO_EVENTS", _PyLong_GetZero());
if (err) goto error;
PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID);
assert(val != NULL);
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be a good idea to add a comment explaining why it can't ever fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add it tomorrow.

@picnixz picnixz changed the title gh-134411: Fix dangerous reference count decrement gh-134411: assert PyLong_FromLong(x) != NULL when x is known to be small May 26, 2025
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks!

@sobolevn sobolevn merged commit cf19b64 into python:main Jul 21, 2025
42 checks passed
@miss-islington-app
Copy link

Thanks @smurav for the PR, and @sobolevn 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 Jul 21, 2025
…n to be small (pythonGH-134415)

Since `PyLong_From Long(PY_MONITORING_DEBUGGER_ID)` falls to `small_int` case and can't return `NULL`. Added `assert`s for extra confidence.
python#134411 (comment)
(cherry picked from commit cf19b64)

Co-authored-by: Sergey Muraviov <smurav@mail.ru>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 21, 2025
…n to be small (pythonGH-134415)

Since `PyLong_From Long(PY_MONITORING_DEBUGGER_ID)` falls to `small_int` case and can't return `NULL`. Added `assert`s for extra confidence.
python#134411 (comment)
(cherry picked from commit cf19b64)

Co-authored-by: Sergey Muraviov <smurav@mail.ru>
@bedevere-app
Copy link

bedevere-app bot commented Jul 21, 2025

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

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jul 21, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jul 21, 2025

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

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 21, 2025
sobolevn pushed a commit that referenced this pull request Jul 21, 2025
…wn to be small (GH-134415) (#136911)

gh-134411: assert `PyLong_FromLong(x) != NULL` when `x` is known to be small (GH-134415)

Since `PyLong_From Long(PY_MONITORING_DEBUGGER_ID)` falls to `small_int` case and can't return `NULL`. Added `assert`s for extra confidence.
#134411 (comment)
(cherry picked from commit cf19b64)

Co-authored-by: Sergey Muraviov <smurav@mail.ru>
sobolevn pushed a commit that referenced this pull request Jul 21, 2025
…wn to be small (GH-134415) (#136910)

gh-134411: assert `PyLong_FromLong(x) != NULL` when `x` is known to be small (GH-134415)

Since `PyLong_From Long(PY_MONITORING_DEBUGGER_ID)` falls to `small_int` case and can't return `NULL`. Added `assert`s for extra confidence.
#134411 (comment)
(cherry picked from commit cf19b64)

Co-authored-by: Sergey Muraviov <smurav@mail.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants