Skip to content

gh-155742: Use PyMem_Malloc() in decode_unicode_with_escapes() - #157587

Merged
vstinner merged 7 commits into
python:mainfrom
vstinner:decode_unicode
Sep 17, 2026
Merged

vstinner merged 7 commits into
python:mainfrom
vstinner:decode_unicode

Conversation

@vstinner

@vstinner vstinner commented Sep 15, 2026

Copy link
Copy Markdown
Member

Replace soft deprecated PyBytes_FromStringAndSize(NULL, size) with a simple PyMem_Malloc().

Avoid also "s = buf" to make the code easier to follow (and easier to debug).

Add non-ASCII tests to test_string_literals.

PyWriterWriter_Finish() is not called, the writer is always
discarded. It's just used as a temporary buffer.

Add non-ASCII tests to test_string_literals.
@vstinner

Copy link
Copy Markdown
Member Author

@lysnikolaou @pablogsal: I was surprised that test_string_literals has no test on non-ASCII string. Is it on purpose?

The test docstring says:

The tricky thing is what should happen when non-ASCII bytes are used inside literals.

@pablogsal

Copy link
Copy Markdown
Member

@lysnikolaou @pablogsal: I was surprised that test_string_literals has no test on non-ASCII string. Is it on purpose?

The test docstring says:

The tricky thing is what should happen when non-ASCII bytes are used inside literals.

Hummmm I don't think this is on purpose :(

@vstinner

Copy link
Copy Markdown
Member Author

Hummmm I don't think this is on purpose :(

Oh ok. Well, my PR adds some tests on Unicode characters :)

Comment thread Parser/string_parser.c Outdated
return NULL;
}
p = buf = PyBytes_AsString(u);
p = buf = PyBytesWriter_GetData(writer);

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.

How about just PyMem_Malloc()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good idea, there is no need for PyBytesWriter since there is no resize and no bytes object is ever needed. I pushed a change to just use PyMem_Malloc(). In debug mode, PyMem_Malloc() detects buffer overflow so I'm fine with it :-)

Replace soft deprecated PyBytes_FromStringAndSize(NULL, size) with a
simple PyMem_Malloc().

Avoid also "s = buf" to make the code easier to follow (and easier to
debug).
@vstinner

Copy link
Copy Markdown
Member Author

I removed the "Add +1 to allow writing a trailing null byte (for strcpy/sprintf)" and replaced it with an assertion to make sure that the buffer is large enough to write a null byte. I ran the test suite without this "+1" and no buffer overflow has been detected.

@vstinner
vstinner enabled auto-merge (squash) September 17, 2026 11:23
@vstinner vstinner changed the title gh-155742: Use PyBytesWriter in decode_unicode_with_escapes() gh-155742: Use PyMem_Malloc() in decode_unicode_with_escapes() Sep 17, 2026
@vstinner
vstinner merged commit 1e03154 into python:main Sep 17, 2026
51 checks passed
@vstinner
vstinner deleted the decode_unicode branch September 17, 2026 11:54
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.

3 participants