This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Assert on varint encoding
ClosedPublic

Authored by kadircet on Nov 13 2020, 1:14 AM.

Details

Summary

5th byte of a varint can't be bigger than 0x0f, fix a test and add an
assertion.

Diff Detail

Event Timeline

kadircet created this revision.Nov 13 2020, 1:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 13 2020, 1:14 AM
kadircet requested review of this revision.Nov 13 2020, 1:14 AM
kadircet updated this revision to Diff 305055.Nov 13 2020, 1:28 AM
  • Also fix the high byte
sammccall added inline comments.Nov 13 2020, 3:28 AM
clang-tools-extra/clangd/index/Serialization.cpp
94

I'm fine with an assert, but this still leaves us with the suspected UB in NDEBUG mode, which doesn't seem OK.
(As noted on D91258 I'm not sure this is actual UB under C++ rules)

The issue here is integral promotion rules say that uint_8 << anything uses *signed* arithmetic. And same for a bunch of the other operations.

I think the clearest thing to do is explicitly type B as uint32_t, so no expression it is in can ever be promoted to (signed) int.
(Unless int is bigger than 32 bits, in which case we've always got enough bits to shift into)

clang-tools-extra/clangd/unittests/SerializationTests.cpp
371

/shamecube

In my defense, I grew up on a 68k mac... little endian is weird

kadircet updated this revision to Diff 305094.Nov 13 2020, 4:04 AM
kadircet marked an inline comment as done.
  • Increase width of B to prevent integer promotion.
sammccall accepted this revision.Nov 13 2020, 4:11 AM
This revision is now accepted and ready to land.Nov 13 2020, 4:11 AM
sammccall added inline comments.Nov 13 2020, 4:12 AM
clang-tools-extra/clangd/index/Serialization.cpp
94

hmm, actually it's also invalid to have the More bit set on the last byte...

kadircet updated this revision to Diff 305108.Nov 13 2020, 5:19 AM
  • Only assert on the 5th byte, prior bytes can have any value.
This revision was landed with ongoing or failed builds.Nov 13 2020, 8:01 AM
This revision was automatically updated to reflect the committed changes.