[clang-tools-extra] 6e7dd1e - [clangd] Assert on varint encoding
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 08:01:16 PST 2020
Author: Kadir Cetinkaya
Date: 2020-11-13T17:01:07+01:00
New Revision: 6e7dd1e3e1170080b76b5dcc5716bdd974343233
URL: https://github.com/llvm/llvm-project/commit/6e7dd1e3e1170080b76b5dcc5716bdd974343233
DIFF: https://github.com/llvm/llvm-project/commit/6e7dd1e3e1170080b76b5dcc5716bdd974343233.diff
LOG: [clangd] Assert on varint encoding
5th byte of a varint can't be bigger than 0x0f, fix a test and add an
assertion.
Differential Revision: https://reviews.llvm.org/D91405
Added:
Modified:
clang-tools-extra/clangd/index/Serialization.cpp
clang-tools-extra/clangd/unittests/SerializationTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/index/Serialization.cpp b/clang-tools-extra/clangd/index/Serialization.cpp
index 0b82224fa715..8b0ae3925a2f 100644
--- a/clang-tools-extra/clangd/index/Serialization.cpp
+++ b/clang-tools-extra/clangd/index/Serialization.cpp
@@ -21,6 +21,7 @@
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
+#include <cstdint>
#include <vector>
namespace clang {
@@ -81,12 +82,17 @@ class Reader {
uint32_t consumeVar() {
constexpr static uint8_t More = 1 << 7;
- uint8_t B = consume8();
+
+ // Use a 32 bit unsigned here to prevent promotion to signed int (unless int
+ // is wider than 32 bits).
+ uint32_t B = consume8();
if (LLVM_LIKELY(!(B & More)))
return B;
uint32_t Val = B & ~More;
for (int Shift = 7; B & More && Shift < 32; Shift += 7) {
B = consume8();
+ // 5th byte of a varint can only have lowest 4 bits set.
+ assert((Shift != 28 || B == (B & 0x0f)) && "Invalid varint encoding");
Val |= (B & ~More) << Shift;
}
return Val;
diff --git a/clang-tools-extra/clangd/unittests/SerializationTests.cpp b/clang-tools-extra/clangd/unittests/SerializationTests.cpp
index 94db6c9127a8..ca7d3ba7e3e1 100644
--- a/clang-tools-extra/clangd/unittests/SerializationTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SerializationTests.cpp
@@ -366,9 +366,9 @@ TEST(SerializationTest, NoCrashOnBadArraySize) {
Pos += FileDigest.size();
// Varints are little-endian base-128 numbers, where the top-bit of each byte
- // indicates whether there are more. 8fffffff7f -> 0xffffffff.
+ // indicates whether there are more. ffffffff0f -> 0xffffffff.
std::string CorruptSrcs =
- (Srcs->Data.take_front(Pos) + llvm::fromHex("8fffffff7f") +
+ (Srcs->Data.take_front(Pos) + llvm::fromHex("ffffffff0f") +
"some_random_garbage")
.str();
Srcs->Data = CorruptSrcs;
More information about the cfe-commits
mailing list