Title: Undefined Behavior in _PyUnicodeWriter_WriteASCIIString: NULL pointer passed to memcpy when len is 0 · Issue #146196 · python/cpython · GitHub
Open Graph Title: Undefined Behavior in _PyUnicodeWriter_WriteASCIIString: NULL pointer passed to memcpy when len is 0 · Issue #146196 · python/cpython
X Title: Undefined Behavior in _PyUnicodeWriter_WriteASCIIString: NULL pointer passed to memcpy when len is 0 · Issue #146196 · python/cpython
Description: Bug report Bug description: The function _PyUnicodeWriter_WriteASCIIString in Objects/unicodeobject.c (or Objects/unicode_writer.c in some versions) contains a potential Undefined Behavior. When len is 0 and ascii is NULL, it calls memcp...
Open Graph Description: Bug report Bug description: The function _PyUnicodeWriter_WriteASCIIString in Objects/unicodeobject.c (or Objects/unicode_writer.c in some versions) contains a potential Undefined Behavior. When le...
X Description: Bug report Bug description: The function _PyUnicodeWriter_WriteASCIIString in Objects/unicodeobject.c (or Objects/unicode_writer.c in some versions) contains a potential Undefined Behavior. When le...
Opengraph URL: https://github.com/python/cpython/issues/146196
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Undefined Behavior in _PyUnicodeWriter_WriteASCIIString: NULL pointer passed to memcpy when len is 0","articleBody":"# Bug report\n\n### Bug description:\n\nThe function `_PyUnicodeWriter_WriteASCIIString` in `Objects/unicodeobject.c` (or `Objects/unicode_writer.c` in some versions) contains a potential Undefined Behavior. When `len` is 0 and `ascii` is `NULL`, it calls `memcpy` with a `NULL` source pointer.\n\nAccording to the C standard, passing `NULL` to `memcpy` is undefined even if the count is zero.\n\n### Proof of Concept\nClang's UndefinedBehaviorSanitizer (UBSan) reports:\n`../Objects/unicode_writer.c:494:36: runtime error: null pointer passed as argument 2, which is declared to never be null`\n\nThis happens in the following block:\n```c\n case PyUnicode_1BYTE_KIND:\n {\n const Py_UCS1 *str = (const Py_UCS1 *)ascii;\n Py_UCS1 *data = writer-\u003edata;\n memcpy(data + writer-\u003epos, str, len); // \u003c--- UB if str is NULL and len is 0\n break;\n }\n```\n\n### Mitigation\nThe function should return early if `len == 0`. This is a common pattern in CPython to avoid unnecessary work and prevent UB with memory functions.\n\n```c\n if (len == -1)\n len = strlen(ascii);\n if (len == 0)\n return 0;\n```\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-146201\n* gh-146220\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ashm-dev","@type":"Person","name":"ashm-dev"},"datePublished":"2026-03-20T09:29:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/146196/cpython/issues/146196"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:aaffef03-716a-8f13-ce42-5121615558e7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DDF2:35AC14:104622B:15F20B1:6A507491 |
| html-safe-nonce | e7738f6097055dc371402377fa27dd7db1b09678bcc3092dbe94350ffbce6016 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEREYyOjM1QUMxNDoxMDQ2MjJCOjE1RjIwQjE6NkE1MDc0OTEiLCJ2aXNpdG9yX2lkIjoiNTczMjQxODQzMjg1NjUxOTgyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eefd3b73fa7edbab498465480f4b3805bce8beb2b903f569f71e1ca7ea118a90 |
| hovercard-subject-tag | issue:4106820851 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/146196/issue_layout |
| twitter:image | https://opengraph.githubassets.com/310e834cbb09d2f2f6d100d20c1a23181d403b583078d9722998e3461b80769f/python/cpython/issues/146196 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/310e834cbb09d2f2f6d100d20c1a23181d403b583078d9722998e3461b80769f/python/cpython/issues/146196 |
| og:image:alt | Bug report Bug description: The function _PyUnicodeWriter_WriteASCIIString in Objects/unicodeobject.c (or Objects/unicode_writer.c in some versions) contains a potential Undefined Behavior. When le... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ashm-dev |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7ac0ad2f2c7e4b9056617355fd9e33e22b0c8df9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width