Skip to content

Commit

Permalink
[unittest][Support] Fix bad negation of signed integer in LEB128Test.…
Browse files Browse the repository at this point in the history
…SLEB128Size (llvm#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.
  • Loading branch information
wdunicornpro authored Nov 27, 2023
1 parent c2ba2b2 commit e3f16de
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/unittests/Support/LEB128Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit e3f16de

Please sign in to comment.