Skip to content

gh-151284: Fix test_capi on UBSan#151286

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:test_capi
Jun 11, 2026
Merged

gh-151284: Fix test_capi on UBSan#151286
vstinner merged 2 commits into
python:mainfrom
vstinner:test_capi

Conversation

@vstinner

@vstinner vstinner commented Jun 10, 2026

Copy link
Copy Markdown
Member

Remove two checks relying on undefined behavior in test_fromwidechar() of test_capi.

Enable test_capi in GitHub Action "Reusable Sanitizer".

Remove two checks relying on undefined behavior in
test_fromwidechar() of test_capi.

Enable test_capi in GitHub Action "Reusable Sanitizer".
@vstinner vstinner added skip news needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 10, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jun 10, 2026
@vstinner

Copy link
Copy Markdown
Member Author
PyObject *
PyUnicode_FromWideChar(const wchar_t *u, Py_ssize_t size)
{
    ...

    if (find_maxchar_surrogates(u, u + size,
                                &maxchar, &num_surrogates) == -1)
        return NULL;

    ...
}

u + size with size=PY_SSIZE_T_MAX triggers an undefined behavior.

@vstinner

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: Would you mind to review this change?

@serhiy-storchaka 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.

I think we should left commented out calls with an explanatory comment, like for crashes. It indicates that we did not miss these cases, so they will not be reintroduced.

@vstinner

Copy link
Copy Markdown
Member Author

I think we should left commented out calls with an explanatory comment, like for crashes. It indicates that we did not miss these cases, so they will not be reintroduced.

I added this code at the end of the function. Is it what you mean?

        # The following tests are skipped since they rely on undefined behavior
        #self.assertRaises(MemoryError, fromwidechar, b'', PY_SSIZE_T_MAX)
        #self.assertRaises(SystemError, fromwidechar, b'\0'*SIZEOF_WCHAR_T, PY_SSIZE_T_MIN)

@serhiy-storchaka 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.

LGTM. 👍

@vstinner vstinner enabled auto-merge (squash) June 11, 2026 10:36
@vstinner vstinner merged commit d87d772 into python:main Jun 11, 2026
52 checks passed
@vstinner vstinner deleted the test_capi branch June 11, 2026 10:55
@miss-islington-app

Copy link
Copy Markdown

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

@bedevere-app

bedevere-app Bot commented Jun 11, 2026

Copy link
Copy Markdown

GH-151323 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 11, 2026
@bedevere-app

bedevere-app Bot commented Jun 11, 2026

Copy link
Copy Markdown

GH-151324 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 Jun 11, 2026
@bedevere-app

bedevere-app Bot commented Jun 11, 2026

Copy link
Copy Markdown

GH-151325 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 Jun 11, 2026
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for the review @serhiy-storchaka.

vstinner added a commit that referenced this pull request Jun 11, 2026
gh-151284: Fix test_capi on UBSan (GH-151286)

Comment two checks relying on undefined behavior in
test_fromwidechar() of test_capi.

Enable test_capi in GitHub Action "Reusable Sanitizer".
(cherry picked from commit d87d772)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this pull request Jun 11, 2026
gh-151284: Fix test_capi on UBSan (GH-151286)

Comment two checks relying on undefined behavior in
test_fromwidechar() of test_capi.

Enable test_capi in GitHub Action "Reusable Sanitizer".
(cherry picked from commit d87d772)

Co-authored-by: Victor Stinner <vstinner@python.org>
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.

2 participants