Skip to content

GH-131296: fix clang-cl warning in tracemalloc.c #131374

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

Closed
wants to merge 1 commit into from

Conversation

chris-eibl
Copy link
Member

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

This one is really interesting. Only clang-cl issues it - not clang or GCC on Linux: https://godbolt.org/z/57oaaTcbT

Interestingly enough, clang-cl even emits this warning without any options given (i.e. the default warning level), on the not taken branch in Py_MIN(x, y) (((x) > (y)) ? (y) : (x)).

I had to use C++ on godbolt, because clang-cl is not available as C compiler, but I get the same error for that file when compiling the C file tracemalloc.c locally.

The fix is easy, although seeming very unneeded :(

@@ -53,7 +53,8 @@ typedef struct tracemalloc_traceback traceback_t;
/* The maximum number of frames is either:
- The maximum number of frames we can store in `traceback_t.nframe`
- The maximum memory size_t we can allocate */
static const unsigned long MAX_NFRAME = Py_MIN(UINT16_MAX, ((SIZE_MAX - sizeof(traceback_t)) / sizeof(frame_t) + 1));
static const unsigned long MAX_NFRAME = (unsigned long)Py_MIN(
Copy link
Member Author

Choose a reason for hiding this comment

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

Fix warning implicit conversion from 'unsigned long long' to 'const unsigned long' changes value from 1537228672809129300 to 1431655764 [-Wconstant-conversion]

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure that this change is needed. It should return UINT16_MAX which fits into unsigned long. IMO this code is too complicated, it's not worth it. I wrote a simpler but different fix: #131514 (note: I'm the author of this code ;-)).

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, much simpler. Didn't dare to do it, since there might be some weird targets where unsigned long is only 16 bit, but those are most probably not existing these days anymore :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Closing in favor of #131514, because

  • simple is better than complex
  • I can't imagine there are platforms out there where SIZE_MAX (maximum value of size_t) is so small that we'd get something smaller than UINT16_MAX

Copy link
Member

Choose a reason for hiding this comment

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

Right, I merged my PR. Thanks anyway!

@chris-eibl chris-eibl closed this Mar 22, 2025
@chris-eibl chris-eibl deleted the fix_clangcl_tracemalloc branch March 22, 2025 09:41
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