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

Duo Wang via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 13:10:08 PST 2023


https://github.com/wdunicornpro created https://github.com/llvm/llvm-project/pull/72700

I came across an undefined behavior when running Support unit tests with UBSAN on Windows. 
```bash
[ 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.

>From b6820c829ec0e232a460582f43d89dd8ade5cdb3 Mon Sep 17 00:00:00 2001
From: Duo Wang <Duo.Wang at sony.com>
Date: Fri, 17 Nov 2023 12:40:22 -0800
Subject: [PATCH] [Support] fix bad negation of signed integer in
 LEB128Test.SLEB128Size

---
 llvm/unittests/Support/LEB128Test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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));
 }
 



More information about the llvm-commits mailing list