Repository navigation
datetime.strftime strings can be terminated by "\x00" literals #124531
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Sep 25, 2024 I guess this makes sense if we create a C string and pass this to the C strftime function. Perhaps we should raise an error in our code if we encounter a null character in the format string.
I guess this makes sense if we create a C string and pass this to the C strftime function. Perhaps we should raise an error in our code if we encounter a null character in the format string.
I think we can just escape them and then un-escape them before returning, no?
We handle this just fine in
datetime.fromisoformat:>>> datetime.now().isoformat(sep="\x00") '2024-09-25\x0013:22:39.621972' >>> datetime.fromisoformat(datetime.now().isoformat(sep="\x00")) datetime.datetime(2024, 9, 25, 13, 22, 52, 309562)
I also think it shouldn't raise error in this case. My expectation is the strftime shouldn't care about anything that isn't datetime format specifier, so it should ignore the
\x00instead of treating it as end of string.Reacted by Jan Kaliszewski- added3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes
on Oct 5, 2024 This approach did not work on platforms without
wstrftime()becausePyUnicode_EncodeLocale()does not support embedded null characters. So I was forced to write more complex and generic patch that fixes also other issues. All that remains is to write the tests, I will do it tomorrow.- linked a pull request that will close this issuegh-52551: Fix encoding issues in strftime() #125193
on Oct 9, 2024 - added 3 commits that reference this issue
on Oct 17, 2024
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
Apparently the
strftimeparser treats\x00as "end of string" in the format code, and the remainder of the string is ignored:I would have expected:
Discovered this when adding some hypothesis tests for
strptime/strftime. I suspect again that if you include a null character in your datetime format string you should expect something to act weird as hell about it, but we should probably fix this anyway if it's not too costly.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs