llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang] Incorrect overflow check and evaluation for MS integer literal suffixes (i8/i16/i32/i64)

Open TPPPP72 opened this issue 4 months ago • 8 comments

Inspired by #212504

I discovered other issues while trying to fix this assertion.

For code : static_assert(-255i8, ""); Surprisingly, the current mainline Clang doesn't produce any errors or warnings at all.

The current Clang mainline produces neither errors nor warnings for this, yet the value is actually truncated; consequently, it fails the assertion static_assert(-255i8 == -255, "-255 != -255 ?").

https://godbolt.org/z/K88b1sxz9

This drew my attention to the fact that the logic handling Microsoft-style integer literal extensions appears quite fragile. I plan to submit a patch later, though some edge cases may still require further discussion.

For example, how should -128i8 be handled?

TPPPP72 avatar Jul 29 '26 10:07 TPPPP72

@llvm/issue-subscribers-clang-frontend

Author: Shengxin Pei (TPPPP72)

Inspired by #212504

I discovered other issues while trying to fix this assertion.

For code : static_assert(-255i8, ""); Surprisingly, the current mainline Clang doesn't produce any errors or warnings at all.

The current Clang mainline produces neither errors nor warnings for this, yet the value is actually truncated; consequently, it fails the assertion static_assert(-255i8 == -255, "-255 != -255 ?").

https://godbolt.org/z/K88b1sxz9

This drew my attention to the fact that the logic handling Microsoft-style integer literal extensions appears quite fragile. I plan to submit a patch later, though some edge cases may still require further discussion.

For example, how should -128i8 be handled?

Clang behaves same as MSVC for these. Godbolt link.

Although it's highly confusing to accept 128i8..255i8. Maybe we should also suggest MSVC to reject these.


Also, the i8 suffix produces char values and the signedness of char can be affected by Clang's -funsigned-char and MSVC's /J.

If char is set to unsigned, then 128i8..255i8 work as expected, but the i8 suffix becomes confusing instead.

frederick-vs-ja avatar Jul 29 '26 12:07 frederick-vs-ja

In my view, when char is unsigned, the i8 literal does not serve its intended purpose and therefore should generate a warning. Additionally, it could be suggested that MSVC do the same.

YexuanXiao avatar Jul 29 '26 13:07 YexuanXiao

@AaronBallman CC

TPPPP72 avatar Jul 29 '26 13:07 TPPPP72

Alternatively, when char is unsigned, the type of the i8 literal could be changed to signed char, but this would need to be coordinated with MSVC.

YexuanXiao avatar Jul 29 '26 13:07 YexuanXiao

If we are mirroring MSVC behavior in -fms-extensions then that seems like it is working as intended and someone would have to open a ticket with them to see if this is intended behavior or not.

shafik avatar Aug 17 '26 18:08 shafik

If we are mirroring MSVC behavior in -fms-extensions then that seems like it is working as intended and someone would have to open a ticket with them to see if this is intended behavior or not.

As it stands, the assertion failure definitely needs to be fixed. Whether a warning or even an error should be issued depends on Microsoft's feedback.

TPPPP72 avatar Aug 18 '26 02:08 TPPPP72

If we are mirroring MSVC behavior in -fms-extensions then that seems like it is working as intended and someone would have to open a ticket with them to see if this is intended behavior or not.

@shafik I've opened https://developercommunity.visualstudio.com/t/11127972 for this. In any case, we should fix the related crash first (see #212743).

frederick-vs-ja avatar Aug 18 '26 05:08 frederick-vs-ja