FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-137273: Fix debug assertion failure in locale.setlocale() on Windows by serhiy-storchaka · Pull Request #137300 · python/cpython · GitHub

Repository navigation

gh-137273: Fix debug assertion failure in locale.setlocale() on Windows - #137300

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:locale-long-encoding
Aug 1, 2025
Merged

serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:locale-long-encoding

Conversation

serhiy-storchaka commented Aug 1, 2025 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

It happened when there were at least 16 characters after dot in the locale name.

… Windows

It happened when there were at least 16 characters after dot in the
locale name.

malemburg left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This fix should probably be limited to just a few Windows releases, unless Win11 also shows the same behavior.

Comment thread Modules/_localemodule.c
size_t len = end ? (size_t)(end - locale) : strlen(locale);
const char *dot = memchr(locale, '.', len);
if (dot && locale + len - dot > 16) {
return -1;

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Where did you get the "16" from ?
Please include some comments to explain where you got it from and why this check is necessary. Thanks.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

BTW: It's better to make this limit configurable via a constant.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Comes from https://github.com/huangqinjin/ucrt/blob/d6e817a4cc90f6f1fe54f8a0aa4af4fff0bb647d/inc/corecrt_internal.h#L401, though I don't think that constant is available for us at compile time (the internal headers for the CRT are, well, internal).

A constant at least gives it a name though, and using MAX_CP_LEN may help someone find an updated definition in the future.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good now.

bedevere-app Bot commented Aug 1, 2025

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Copy link
Copy Markdown
Member Author

How to check the Windows version? I do not have Windows 11, can you please test this PR on Windows 11?

zooba commented Aug 1, 2025

Copy link
Copy Markdown
Member

How to check the Windows version?

I wouldn't bother, this limit has been around forever. The bigger challenge would be how to handle @<extras> if/when support is added, because right now there's no support for them in UCRT at all.

Copy link
Copy Markdown
Member

How to check the Windows version? I do not have Windows 11, can you please test this PR on Windows 11?

Sorry, but I don't have a Windows 11 build system available at the moment (not even a working Win10 system, after the VM crashed some time ago).

Let's not worry about this now. We can disable things again selectively, once the Windows SDK provides better support for handling locale names (perhaps in a few years).

Copy link
Copy Markdown
Member Author

The bigger challenge would be how to handle @<extras> if/when support is added, because right now there's no support for them in UCRT at all.

I am planning to handle this in Python code. Translate "zh_HK.UTF-8@hans" to "zh-Hans-HK.utf-8", etc.

Copy link
Copy Markdown
Member

I am planning to handle this in Python code. Translate "zh_HK.UTF-8@hans" to "zh-Hans-HK.utf-8", etc.

Is that how Windows handles modifiers ?

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again.

bedevere-app Bot commented Aug 1, 2025

Copy link
Copy Markdown

Thanks for making the requested changes!

@malemburg: please review the changes made to this pull request.

bedevere-app Bot requested a review from malemburg August 1, 2025 13:34
Comment thread Modules/_localemodule.c
size_t len = end ? (size_t)(end - locale) : strlen(locale);
const char *dot = memchr(locale, '.', len);
if (dot && locale + len - dot > 16) {
return -1;

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good now.

zooba commented Aug 1, 2025

Copy link
Copy Markdown
Member

Is that how Windows handles modifiers ?

No - Windows UCRT treats them as part of the code page name and overruns its own buffers, leading to a crash 😉

I don't think there's any support for modifiers in locales at all. The OS itself has always used a different system for those AFAICT, it's just the POSIX emulation in the CRT that's trying to fudge over it.

Copy link
Copy Markdown
Member

I don't think there's any support for modifiers in locales at all.

In that case, it's probably best to simply strip from them locale strings passed to setlocale() on Windows. Esp. since the meaning of those modifiers is not standardized anywhere, AFAIK.

Copy link
Copy Markdown
Member Author

Is that how Windows handles modifiers ?

Yes. It supports format language[-script][-region][.codepage] for BCP-47 locale names.

I seen also sr_Latn_RS instead of sr_RS@latin on some of Posix platforms, so we should support this format not only on Windows. setlocale() should try several variants.

serhiy-storchaka merged commit 718e0c8 into python:main Aug 1, 2025

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 1, 2025
… Windows (pythonGH-137300)

It happened when there were at least 16 characters after dot in the
locale name.
(cherry picked from commit 718e0c8)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka deleted the locale-long-encoding branch August 1, 2025 14:44
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 1, 2025
… Windows (pythonGH-137300)

It happened when there were at least 16 characters after dot in the
locale name.
(cherry picked from commit 718e0c8)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

bedevere-app Bot commented Aug 1, 2025

Copy link
Copy Markdown

GH-137305 is a backport of this pull request to the 3.14 branch.

bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 1, 2025

bedevere-app Bot commented Aug 1, 2025

Copy link
Copy Markdown

GH-137306 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 only security fixes label Aug 1, 2025
serhiy-storchaka added a commit that referenced this pull request Aug 1, 2025
…n Windows (GH-137300) (GH-137306)

It happened when there were at least 16 characters after dot in the
locale name.
(cherry picked from commit 718e0c8)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

Copy link
Copy Markdown
Member Author

Thank you for your review, @malemburg and @zooba.

Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
… Windows (pythonGH-137300)

It happened when there were at least 16 characters after dot in the
locale name.
vstinner pushed a commit that referenced this pull request Oct 7, 2025
…n Windows (GH-137300) (#137305)

gh-137273: Fix debug assertion failure in locale.setlocale() on Windows (GH-137300)

It happened when there were at least 16 characters after dot in the
locale name.
(cherry picked from commit 718e0c8)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL