Title: `_csv`: `_set_str()` missing NULL check after `PyUnicode_DecodeASCII()` · Issue #145978 · python/cpython · GitHub
Open Graph Title: `_csv`: `_set_str()` missing NULL check after `PyUnicode_DecodeASCII()` · Issue #145978 · python/cpython
X Title: `_csv`: `_set_str()` missing NULL check after `PyUnicode_DecodeASCII()` · Issue #145978 · python/cpython
Description: Bug report Bug description: In Modules/_csv.c, _set_str() does not check the return value of PyUnicode_DecodeASCII() when using the default value. If the allocation fails (OOM), *target is set to NULL and the function returns 0 (success)...
Open Graph Description: Bug report Bug description: In Modules/_csv.c, _set_str() does not check the return value of PyUnicode_DecodeASCII() when using the default value. If the allocation fails (OOM), *target is set to N...
X Description: Bug report Bug description: In Modules/_csv.c, _set_str() does not check the return value of PyUnicode_DecodeASCII() when using the default value. If the allocation fails (OOM), *target is set to N...
Opengraph URL: https://github.com/python/cpython/issues/145978
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`_csv`: `_set_str()` missing NULL check after `PyUnicode_DecodeASCII()`","articleBody":"# Bug report\n\n### Bug description:\n\nIn `Modules/_csv.c`, `_set_str()` does not check the return value of `PyUnicode_DecodeASCII()` when using the default value. If the allocation fails (OOM), `*target` is set to `NULL` and the function returns `0` (success).\n\nhttps://github.com/python/cpython/blob/main/Modules/_csv.c#L315-L329\n\n```c\nstatic int\n_set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)\n{\n if (src == NULL)\n *target = PyUnicode_DecodeASCII(dflt, strlen(dflt), NULL);\n // ^^^ no NULL check \n else {\n ...\n }\n return 0; // Returns 0 even if allocation failed\n}\n```\n\nThis function is called for `lineterminator`:\n```c\nDIASET(_set_str, \"lineterminator\", \u0026self-\u003elineterminator, lineterminator, \"\\r\\n\");\n```\n\nIf allocation fails, `self-\u003elineterminator` is `NULL`. Subsequent calls to `PyUnicode_GET_LENGTH(dialect-\u003elineterminator)` (e.g. in `join_append_data` at line 1183 or `join_append_lineterminator` at line 1299) will dereference `NULL`.\n\n## Fix\n\n```c\nstatic int\n_set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)\n{\n if (src == NULL) {\n *target = PyUnicode_DecodeASCII(dflt, strlen(dflt), NULL);\n if (*target == NULL)\n return -1;\n }\n else {\n if (!PyUnicode_Check(src)) {\n PyErr_Format(PyExc_TypeError,\n \"\\\"%s\\\" must be a string, not %T\", name, src);\n return -1;\n }\n Py_XSETREF(*target, Py_NewRef(src));\n }\n return 0;\n}\n```\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux","author":{"url":"https://github.com/raminfp","@type":"Person","name":"raminfp"},"datePublished":"2026-03-15T12:24:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/145978/cpython/issues/145978"}
| 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:459967e0-4716-ff20-55aa-f5fdce256550 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C306:FB8AB:6C1A89:939CBC:6A514FF1 |
| html-safe-nonce | d02edc28b6dffa9db464978f48e06dc5b993b93c91aabbf08a0d54f982262a35 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzA2OkZCOEFCOjZDMUE4OTo5MzlDQkM6NkE1MTRGRjEiLCJ2aXNpdG9yX2lkIjoiMTAzNjU5MjM2ODcxODAwODMwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5ef3a3c2e8fb1d5cf4c567ef406079d2dac3624952c92c4f578a32f84a802cb7 |
| hovercard-subject-tag | issue:4078277865 |
| 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/145978/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d623558288a51d6f6c1ae4ba961a386ee46a36c8081dd6dfff78589ff457fcf8/python/cpython/issues/145978 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d623558288a51d6f6c1ae4ba961a386ee46a36c8081dd6dfff78589ff457fcf8/python/cpython/issues/145978 |
| og:image:alt | Bug report Bug description: In Modules/_csv.c, _set_str() does not check the return value of PyUnicode_DecodeASCII() when using the default value. If the allocation fails (OOM), *target is set to N... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | raminfp |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6f17634ed2af1d116850bcea6c63a70a4d72970f33ade1dd3eef3764b7e16994 |
| 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 | 511bff6ea06e144ed085ff736aea4c6404987b00 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width