- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.3k
          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
Conversation
There was a problem hiding this 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)
There was a problem hiding this 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.
        
          
                Python/instrumentation.c
              
                Outdated
          
        
      | err = PyObject_SetAttrString(events, "NO_EVENTS", _PyLong_GetZero()); | ||
| if (err) goto error; | ||
| PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID); | ||
| assert(val != NULL); | 
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
PyLong_FromLong(x) != NULL when x is known to be small
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
…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 <[email protected]>
…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 <[email protected]>
| GH-136910 is a backport of this pull request to the 3.14 branch. | 
| GH-136911 is a backport of this pull request to the 3.13 branch. | 
…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 <[email protected]>
…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 <[email protected]>
…n to be small (python#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)
…n to be small (python#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)
…is known to be small (pythonGH-134415) (python#136910) pythongh-134411: assert `PyLong_FromLong(x) != NULL` when `x` is known 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 <[email protected]>
Python/instrumentation.c: ensure non-NULLPyLong_FromLongresults when possible #134411