Skip to content

[unittest][Support] Fix bad negation of signed integer in LEB128Test.SLEB128Size #72700

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

Conversation

wdunicornpro
Copy link
Contributor

I came across an undefined behavior when running Support unit tests with UBSAN on Windows.

[ RUN      ] LEB128Test.SLEB128Size
C:\llvm\unittests\Support\LEB128Test.cpp:382:3: runtime error: negation of -9223372036854775808 cannot be represented in type 'long long'; cast to an unsigned type to negate this value to itself
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\llvm\unittests\Support\LEB128Test.cpp:382:3 in 

UBSAN did not report the same error on macOS, but the negation still seemed invalid (0x8000000000000000LL == -0x8000000000000000LL evaluated to true).
I can confirm that -0x7fffffffffffffffLL - 1 results in the expected value on both platforms.

@llvmbot
Copy link
Member

llvmbot commented Nov 17, 2023

@llvm/pr-subscribers-llvm-support

Author: Duo Wang (wdunicornpro)

Changes

I came across an undefined behavior when running Support unit tests with UBSAN on Windows.

[ RUN      ] LEB128Test.SLEB128Size
C:\llvm\unittests\Support\LEB128Test.cpp:382:3: runtime error: negation of -9223372036854775808 cannot be represented in type 'long long'; cast to an unsigned type to negate this value to itself
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\llvm\unittests\Support\LEB128Test.cpp:382:3 in 

UBSAN did not report the same error on macOS, but the negation still seemed invalid (0x8000000000000000LL == -0x8000000000000000LL evaluated to true).
I can confirm that -0x7fffffffffffffffLL - 1 results in the expected value on both platforms.


Full diff: https://github.com/llvm/llvm-project/pull/72700.diff

1 Files Affected:

  • (modified) llvm/unittests/Support/LEB128Test.cpp (+1-1)
diff --git a/llvm/unittests/Support/LEB128Test.cpp b/llvm/unittests/Support/LEB128Test.cpp
index 986d29358458d26..21523e5f7a08c73 100644
--- a/llvm/unittests/Support/LEB128Test.cpp
+++ b/llvm/unittests/Support/LEB128Test.cpp
@@ -379,7 +379,7 @@ TEST(LEB128Test, SLEB128Size) {
   EXPECT_EQ(10u, getSLEB128Size(-0x4100000000000000LL));
 
   EXPECT_EQ(10u, getSLEB128Size(-0x7fffffffffffffffLL));
-  EXPECT_EQ(10u, getSLEB128Size(-0x8000000000000000LL));
+  EXPECT_EQ(10u, getSLEB128Size(-0x7fffffffffffffffLL - 1));
   EXPECT_EQ(10u, getSLEB128Size(INT64_MIN));
 }
 

Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

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

Sounds good

@wdunicornpro
Copy link
Contributor Author

@dwblaikie Thanks for the approval!

Could someone with write access help me merge this PR?

@dwblaikie dwblaikie merged commit e3f16de into llvm:main Nov 27, 2023
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