Title: bytes.fromhex() reads uninitialized memory · Issue #158583 · python/cpython · GitHub
Open Graph Title: bytes.fromhex() reads uninitialized memory · Issue #158583 · python/cpython
X Title: bytes.fromhex() reads uninitialized memory · Issue #158583 · python/cpython
Description: Example: import array res = bytes.fromhex(array.array('B', b' 1A 2B 30 ')) try: bytes.fromhex(array.array('B', b' 1A 2B 3')) except ValueError: pass Valgrind reports two "Use of uninitialised value of size 8" errors: $ PYTHONMALLOC=mallo...
Open Graph Description: Example: import array res = bytes.fromhex(array.array('B', b' 1A 2B 30 ')) try: bytes.fromhex(array.array('B', b' 1A 2B 3')) except ValueError: pass Valgrind reports two "Use of uninitialised value...
X Description: Example: import array res = bytes.fromhex(array.array('B', b' 1A 2B 30 ')) try: bytes.fromhex(array.array('B', b' 1A 2B 3')) except ValueError: pass Valgrind reports...
Opengraph URL: https://github.com/python/cpython/issues/158583
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bytes.fromhex() reads uninitialized memory","articleBody":"Example:\n\n```py\nimport array\nres = bytes.fromhex(array.array('B', b' 1A 2B 30 '))\ntry:\n bytes.fromhex(array.array('B', b' 1A 2B 3'))\nexcept ValueError:\n pass\n```\n\nValgrind reports two \"Use of uninitialised value of size 8\" errors:\n\n```\n$ PYTHONMALLOC=malloc valgrind ./python x.py \n==863634== Memcheck, a memory error detector\n==863634== Copyright (C) 2002-2026, and GNU GPL'd, by Julian Seward et al.\n==863634== Using Valgrind-3.27.1 and LibVEX; rerun with -h for copyright info\n==863634== Command: ./python x.py\n==863634== \n==863634== Use of uninitialised value of size 8\n==863634== at 0x497C07: _PyBytes_FromHex (bytesobject.c:2688)\n==863634== by 0x497CC2: bytes_fromhex_impl (bytesobject.c:2618)\n==863634== by 0x497D07: bytes_fromhex (bytesobject.c.h:1261)\n==863634== by 0x4F491A: cfunction_vectorcall_O (methodobject.c:535)\n==863634== by 0x49BDAF: _PyObject_VectorcallTstate (pycore_call.h:149)\n==863634== by 0x49BE77: PyObject_Vectorcall (call.c:327)\n==863634== by 0x5B0C90: _Py_VectorCallInstrumentation_StackRefSteal (ceval.c:770)\n==863634== by 0x5B7E1C: _PyEval_EvalFrameDefault (generated_cases.c.h:1906)\n==863634== by 0x5CFACD: _PyEval_EvalFrame (pycore_ceval.h:122)\n==863634== by 0x5CFC98: _PyEval_Vector (ceval.c:2176)\n==863634== by 0x5CFD7C: PyEval_EvalCode (ceval.c:681)\n==863634== by 0x649C6B: run_eval_code_obj (pythonrun.c:1406)\n==863634== \n==863634== Use of uninitialised value of size 8\n==863634== at 0x497BC1: _PyBytes_FromHex (bytesobject.c:2700)\n==863634== by 0x497CC2: bytes_fromhex_impl (bytesobject.c:2618)\n==863634== by 0x497D07: bytes_fromhex (bytesobject.c.h:1261)\n==863634== by 0x4F491A: cfunction_vectorcall_O (methodobject.c:535)\n==863634== by 0x49BDAF: _PyObject_VectorcallTstate (pycore_call.h:149)\n==863634== by 0x49BE77: PyObject_Vectorcall (call.c:327)\n==863634== by 0x5B0C90: _Py_VectorCallInstrumentation_StackRefSteal (ceval.c:770)\n==863634== by 0x5B7E1C: _PyEval_EvalFrameDefault (generated_cases.c.h:1906)\n==863634== by 0x5CFACD: _PyEval_EvalFrame (pycore_ceval.h:122)\n==863634== by 0x5CFC98: _PyEval_Vector (ceval.c:2176)\n==863634== by 0x5CFD7C: PyEval_EvalCode (ceval.c:681)\n==863634== by 0x649C6B: run_eval_code_obj (pythonrun.c:1406)\n==863634== \n==863634== \n==863634== HEAP SUMMARY:\n==863634== in use at exit: 1,618 bytes in 4 blocks\n==863634== total heap usage: 36,249 allocs, 36,245 frees, 9,219,652 bytes allocated\n==863634== \n==863634== LEAK SUMMARY:\n==863634== definitely lost: 0 bytes in 0 blocks\n==863634== indirectly lost: 0 bytes in 0 blocks\n==863634== possibly lost: 0 bytes in 0 blocks\n==863634== still reachable: 1,618 bytes in 4 blocks\n==863634== suppressed: 0 bytes in 0 blocks\n==863634== Rerun with --leak-check=full to see details of leaked memory\n==863634== \n==863634== Use --track-origins=yes to see where uninitialised values come from\n==863634== For lists of detected and suppressed errors, rerun with: -s\n==863634== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)\n```\n\nThe problem is in `_PyBytes_FromHex()`: `str++` doesn't check if we reached `end`.\n\nFor example, for a buffer of 10 bytes, it can read the 11th: outside the buffer!\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-158584\n* gh-158691\n* gh-158692\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-10-01T22:31:01Z","url":"https://github.com/python/cpython/issues/158583"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:45ec56c3-9287-f9d0-d6eb-662b3d474d2d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CDAE:2A7C1C:1AC1105:2354259:6B021B31 |
| html-safe-nonce | 84cb9cb7361653da5a83a67cc072e22d39274db5b190ae97387bf0ff1292a5d7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDREFFOjJBN0MxQzoxQUMxMTA1OjIzNTQyNTk6NkIwMjFCMzEiLCJ2aXNpdG9yX2lkIjoiMjY2MDE4OTA5NTgwODA4MDY4OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | adae4f309eccb24ad66f1271ef892c3bcac14c272a7488661997cccd27b4a7a6 |
| hovercard-subject-tag | issue:5670109685 |
| 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/158583/issue_layout |
| twitter:image | https://opengraph.githubassets.com/600cb5ef5d3b5f3b3eff8cb4b0116770b9db4bc40877a9502397183dda78ed6e/python/cpython/issues/158583 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/600cb5ef5d3b5f3b3eff8cb4b0116770b9db4bc40877a9502397183dda78ed6e/python/cpython/issues/158583 |
| og:image:alt | Example: import array res = bytes.fromhex(array.array('B', b' 1A 2B 30 ')) try: bytes.fromhex(array.array('B', b' 1A 2B 3')) except ValueError: pass Valgrind reports two "Use of uninitialised value... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width