René's URL Explorer Experiment


Title: Segfault/UB from expat when re-entering the XML Parser · Issue #146169 · python/cpython · GitHub

Open Graph Title: Segfault/UB from expat when re-entering the XML Parser · Issue #146169 · python/cpython

X Title: Segfault/UB from expat when re-entering the XML Parser · Issue #146169 · python/cpython

Description: Crash report What happened? Semi-reliable(Interstingly, python in macos doesn't segfault for me, but docker/linux aarch64 does reliably) crash with this code: from xml.parsers import expat p = expat.ParserCreate(encoding="utf-16") def st...

Open Graph Description: Crash report What happened? Semi-reliable(Interstingly, python in macos doesn't segfault for me, but docker/linux aarch64 does reliably) crash with this code: from xml.parsers import expat p = expa...

X Description: Crash report What happened? Semi-reliable(Interstingly, python in macos doesn't segfault for me, but docker/linux aarch64 does reliably) crash with this code: from xml.parsers import expat p = ...

Opengraph URL: https://github.com/python/cpython/issues/146169

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault/UB from expat when re-entering the XML Parser","articleBody":"# Crash report\n\n### What happened?\n\nSemi-reliable(Interstingly, python in macos doesn't segfault for me, but docker/linux aarch64 does reliably) crash with this code:\n\n```python\nfrom xml.parsers import expat\n\np = expat.ParserCreate(encoding=\"utf-16\")\n\ndef start(name, attrs):\n    p.CharacterDataHandler = lambda data: p.Parse(data, 0)\n\np.StartElementHandler = start\n\ndata = b\"\\xff\\xfe\u003c\\x00a\\x00\u003e\\x00x\\x00\"\nfor i in range(len(data)):\n    try:\n        p.Parse(data[i:i+1], i == len(data) - 1)\n    except Exception:\n        pass\n```\n\nThis code /is/ doing some pretty naughty stuff, but the main problem seems to be that the handler is being set to re-enter the parser.  The expat docs do say:\n\n\u003e To state the obvious: the three parsing functions [XML_Parse](https://libexpat.github.io/doc/api/latest/#XML_Parse), [XML_ParseBuffer](https://libexpat.github.io/doc/api/latest/#XML_ParseBuffer) and [XML_GetBuffer](https://libexpat.github.io/doc/api/latest/#XML_GetBuffer) must not be called from within a handler unless they operate on a separate parser instance, that is, one that did not call the handler. For example, it is OK to call the parsing functions from within an XML_ExternalEntityRefHandler, if they apply to the parser created by [XML_ExternalEntityParserCreate](https://libexpat.github.io/doc/api/latest/#XML_ExternalEntityParserCreate).\n\nand I see that the python expat parser code tracks in_callback:\nhttps://github.com/python/cpython/blob/52c01864c4778a351e5aa3584e86ed6fd212a5a4/Modules/pyexpat.c#L91\n\nSo I wonder if we can avoid the segfault by preventing Parse calls when in_callback==true?\n\nThere's also a secondary issue in play here, that Parse() seems to call \n\nXML_SetEncoding\nhttps://github.com/python/cpython/blob/52c01864c4778a351e5aa3584e86ed6fd212a5a4/Modules/pyexpat.c#L872\n\nWithout the check outlined in the expat docs:\n\n\u003e Set the encoding to be used by the parser. It is equivalent to passing a non-NULL encoding argument to the parser creation functions. It must not be called after [XML_Parse](https://libexpat.github.io/doc/api/latest/#XML_Parse) or [XML_ParseBuffer](https://libexpat.github.io/doc/api/latest/#XML_ParseBuffer) have been called on the given parser. Returns XML_STATUS_OK on success or XML_STATUS_ERROR on error.\n\nThis is almost definitely not going to cause issues unless the encoding is actually changing, (not that common) at which point the UB will rear its head as the internal state of the parser becomes inconsistent.\n\n\u003cdetails\u003e\n\u003csummary\u003eDockerfile reproducer\u003c/summary\u003e\n\n```\nARG REPO=https://github.com/python/cpython.git\nARG BRANCH=main\n\nFROM ubuntu:24.04\n\nARG REPO\nARG BRANCH\nENV DEBIAN_FRONTEND=noninteractive\n\nRUN apt-get update \u0026\u0026 apt-get install -y \\\n    build-essential git pkg-config \\\n    libssl-dev libbz2-dev libreadline-dev libsqlite3-dev \\\n    liblzma-dev libffi-dev zlib1g-dev uuid-dev \\\n    \u0026\u0026 rm -rf /var/lib/apt/lists/*\n\nRUN git clone --branch ${BRANCH} --depth 1 \\\n    ${REPO} /cpython\n\nRUN cd /cpython \u0026\u0026 \\\n    ./configure --prefix=/python --without-ensurepip \u0026\u0026 \\\n    make -j$(nproc) \u0026\u0026 \\\n    make install\n\n# ── TEST SCRIPT ──────────────────────────────────────────────────\nRUN cat \u003e /test.py \u003c\u003c 'EOF'\nfrom xml.parsers import expat\n\np = expat.ParserCreate(encoding=\"utf-16\")\n\ndef start(name, attrs):\n    p.CharacterDataHandler = lambda data: p.Parse(data, 0)\n\np.StartElementHandler = start\n\ndata = b\"\\xff\\xfe\u003c\\x00a\\x00\u003e\\x00x\\x00\"\nfor i in range(len(data)):\n    try:\n        p.Parse(data[i:i+1], i == len(data) - 1)\n    except Exception:\n        pass\nEOF\n# ──────────────────────────────────────────────────────────────────────────────\n\nCMD [\"/bin/sh\", \"-c\", \"uname -m \u0026\u0026 /python/bin/python3 -VV \u0026\u0026 /python/bin/python3 /test.py\"]\n```\n\n\u003c/details\u003e\n\nGives on my pc:\n```\ndocker run --rm -it expattest\naarch64\nPython 3.15.0a7+ (heads/main:52c0186, Mar 19 2026, 13:06:19) [GCC 13.3.0]\n52c01864c4778a351e5aa3584e86ed6fd212a5a4\nSegmentation fault (core dumped)\n```\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a7+ (heads/main:52c0186, Mar 19 2026, 13:06:19) [GCC 13.3.0]\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-146179\n* gh-146566\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/stestagg","@type":"Person","name":"stestagg"},"datePublished":"2026-03-19T13:28:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/146169/cpython/issues/146169"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:53bba78c-9957-5386-ada4-669ad60ae900
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9366:13DB1B:3450B2:45C58A:6A517C04
html-safe-nonce2320e601aa04fd3d4ccba6d76616cc286ece01eff038a4c5edd7d50921a197e3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzY2OjEzREIxQjozNDUwQjI6NDVDNThBOjZBNTE3QzA0IiwidmlzaXRvcl9pZCI6IjU1Mjc4MDMzNTkzNjUzNTU1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac30686a7922b899345947a734d64dfbca863d1e0b32e229187f2e2e5fca4d4564
hovercard-subject-tagissue:4101689400
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/146169/issue_layout
twitter:imagehttps://opengraph.githubassets.com/13110444cf3708fd9f0fa5c4be2921bd5ad0fbf06550d0d7dbca0fbfbfa5840e/python/cpython/issues/146169
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/13110444cf3708fd9f0fa5c4be2921bd5ad0fbf06550d0d7dbca0fbfbfa5840e/python/cpython/issues/146169
og:image:altCrash report What happened? Semi-reliable(Interstingly, python in macos doesn't segfault for me, but docker/linux aarch64 does reliably) crash with this code: from xml.parsers import expat p = expa...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamestestagg
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasee2c3fb1e56564ee0a4805542460de1eb8bd6bd77
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/146169#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F146169
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F146169
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/146169
Reloadhttps://github.com/python/cpython/issues/146169
Reloadhttps://github.com/python/cpython/issues/146169
Please reload this pagehttps://github.com/python/cpython/issues/146169
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/146169
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.7k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Segfault/UB from expat when re-entering the XML Parserhttps://github.com/python/cpython/issues/146169#top
https://github.com/picnixz
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-XMLhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-XML%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com/stestagg
stestagghttps://github.com/stestagg
on Mar 19, 2026https://github.com/python/cpython/issues/146169#issue-4101689400
XML_Parsehttps://libexpat.github.io/doc/api/latest/#XML_Parse
XML_ParseBufferhttps://libexpat.github.io/doc/api/latest/#XML_ParseBuffer
XML_GetBufferhttps://libexpat.github.io/doc/api/latest/#XML_GetBuffer
XML_ExternalEntityParserCreatehttps://libexpat.github.io/doc/api/latest/#XML_ExternalEntityParserCreate
cpython/Modules/pyexpat.chttps://github.com/python/cpython/blob/52c01864c4778a351e5aa3584e86ed6fd212a5a4/Modules/pyexpat.c#L91
52c0186https://github.com/python/cpython/commit/52c01864c4778a351e5aa3584e86ed6fd212a5a4
cpython/Modules/pyexpat.chttps://github.com/python/cpython/blob/52c01864c4778a351e5aa3584e86ed6fd212a5a4/Modules/pyexpat.c#L872
52c0186https://github.com/python/cpython/commit/52c01864c4778a351e5aa3584e86ed6fd212a5a4
XML_Parsehttps://libexpat.github.io/doc/api/latest/#XML_Parse
XML_ParseBufferhttps://libexpat.github.io/doc/api/latest/#XML_ParseBuffer
gh-146169: Prevent re-entrant Parse() calls in pyexpat #146179https://github.com/python/cpython/pull/146179
gh-146169: correctly handle re-entrant parsing calls in Expat handlers #146566https://github.com/python/cpython/pull/146566
picnixzhttps://github.com/picnixz
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-XMLhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-XML%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.