Skip to content
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

PyLong_AsLongAndOverflow does not guarantee overflow is -1 when value is -1 #130824

Closed
yijan4845 opened this issue Mar 4, 2025 · 3 comments
Closed
Labels
tests Tests in the Lib/test dir topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@yijan4845
Copy link

yijan4845 commented Mar 4, 2025

Bug report

Bug description:

In the function pylong_aslongandoverflow

static PyObject *
pylong_aslongandoverflow(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
int overflow = UNINITIALIZED_INT;
long value = PyLong_AsLongAndOverflow(arg, &overflow);
if (value == -1 && PyErr_Occurred()) {
assert(overflow == -1);
return NULL;
}
return Py_BuildValue("li", value, overflow);
}

there is an assertion overflow == -1 when value == -1. But this is not always true, like if arg is NULL.

Reproduce:

from test.support import import_helper

_testlimitedcapi = import_helper.import_module('_testlimitedcapi')

aslonglongandoverflow = _testlimitedcapi.pylong_aslonglongandoverflow
aslonglongandoverflow(None)

Result:

python: ../Modules/_testlimitedcapi/long.c:674: pylong_aslonglongandoverflow: Assertion `overflow == -1' failed.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

@yijan4845 yijan4845 added the type-bug An unexpected behavior, bug, or error label Mar 4, 2025
@ZeroIntensity ZeroIntensity added the tests Tests in the Lib/test dir label Mar 4, 2025
@ZeroIntensity
Copy link
Member

That would be because passing None to pylong_aslongandoverflow changes the argument to NULL, so a SystemError is raised by PyLong_AsLongAndOverflow, which the test function wasn't expecting.

This is documented:

If any other exception occurs set *overflow to 0 and return -1 as usual.

overflow is indeed getting set to 0 here, not -1, so the assertion is wrong. It's fine to fix this specific case since we're already here, but I'm assuming you're doing some fuzzing. That's fine--we're happy to fix edge cases--but please try to keep the fuzzing in the standard library, not our test suite (so no more fuzzing on the test module, please!)

@yijan4845
Copy link
Author

That would be because passing None to pylong_aslongandoverflow changes the argument to NULL, so a SystemError is raised by PyLong_AsLongAndOverflow, which the test function wasn't expecting.

This is documented:

If any other exception occurs set *overflow to 0 and return -1 as usual.

overflow is indeed getting set to 0 here, not -1, so the assertion is wrong. It's fine to fix this specific case since we're already here, but I'm assuming you're doing some fuzzing. That's fine--we're happy to fix edge cases--but please try to keep the fuzzing in the standard library, not our test suite (so no more fuzzing on the test module, please!)

I understand. I'll make sure to focus my fuzzing efforts on the standard library as much as possible. Thanks for the clarification!

@encukou encukou closed this as not planned Won't fix, can't repro, duplicate, stale Mar 5, 2025
@encukou encukou reopened this Mar 5, 2025
encukou pushed a commit that referenced this issue Mar 5, 2025
…H-130828)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 5, 2025
…ons (pythonGH-130828)

(cherry picked from commit 9013080)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 5, 2025
@ZeroIntensity
Copy link
Member

I'll deal with backporting both PRs to 3.12.

encukou pushed a commit to encukou/cpython that referenced this issue Mar 5, 2025
… functions (pythonGH-130828)

(cherry picked from commit 9013080)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
encukou pushed a commit to encukou/cpython that referenced this issue Mar 5, 2025
ZeroIntensity added a commit to ZeroIntensity/cpython that referenced this issue Mar 5, 2025
… functions (pythonGH-130828)

(cherry picked from commit 9013080)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
encukou pushed a commit to miss-islington/cpython that referenced this issue Mar 5, 2025
serhiy-storchaka pushed a commit that referenced this issue Mar 5, 2025
…ions (GH-130828) (GH-130869)

(cherry picked from commit 9013080)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
serhiy-storchaka pushed a commit that referenced this issue Mar 5, 2025
…GH-130828) (GH-130876)

(cherry picked from commit 9013080)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants