Skip to content

GH-131296: fix clang-cl warning on Windows in getpath.c #131594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025

Conversation

chris-eibl
Copy link
Member

@chris-eibl chris-eibl commented Mar 22, 2025

with expression of type 'const char' is always true
[-Wtautological-constant-out-of-range-compare]
@@ -690,7 +690,6 @@ env_to_dict(PyObject *dict, const char *key, int and_clear)
// Quick convert to wchar_t, since we know key is ASCII
wchar_t *wp = wkey;
for (const char *p = &key[4]; *p; ++p) {
assert(*p < 128);
Copy link
Member Author

@chris-eibl chris-eibl Mar 22, 2025

Choose a reason for hiding this comment

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

fix warning : result of comparison of constant 128 with expression of type 'const char' is always true [-Wtautological-constant-out-of-range-compare]

We are here in an #ifdef MS_WINDOWS where a char will always be signed, so this assert can safely be deleted.

Copy link
Member

Choose a reason for hiding this comment

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

If you compile with /J char is unsigned. (ref https://learn.microsoft.com/en-us/cpp/cpp/char-wchar-t-char16-t-char32-t?view=msvc-170)

I don't know if that is supported in CPython or used by anyone though, I expect it would be quite rare. Perhaps that should be a compile error?

Copy link
Member

Choose a reason for hiding this comment

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

I would imagine setting /J would break the ABI, so I would imagine that is a good reason to make it a compile error if char is unsigned.

Copy link
Member Author

Choose a reason for hiding this comment

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

Boah, yeah, would have never imagined to think of making char unsigned on Windows, because that would presumably result in a lot of trouble. But since you've mentioned it - if we'd like this to be asserted here (and not a more prominent place like pyport.h using an explicit static_assert, etc to fail at compile time), then we could keep it. It just won't do its job well, since presumably this will not fire reliably here.

But then we'd have to silence the warning: maybe with a pragma diagnostic?

Im still +0 for just removing this assert.

@zooba WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, to be clear I think we probably should make an assertion about the signedness of char earlier on, and remove this assertion.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah - ok, but this will need its own issue and PR.

Copy link
Member

Choose a reason for hiding this comment

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

We still need the assertion if it's signed, it just has to check that it's not negative.

Perhaps asserting !(*p & 0x80) is the better fix? It's unlikely that someone is going to start providing Unicode characters elsewhere in the source file, but that is the point of the check.

@chris-eibl
Copy link
Member Author

@FFY00 friendly ping. IMHO this is a very simple one :)

@zooba zooba merged commit f663b2c into python:main Apr 22, 2025
41 checks passed
@chris-eibl chris-eibl deleted the fix_clangcl_getpath branch April 22, 2025 14:15
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.

4 participants