[Support] Assert that reported key+data lenghts match reality
authorReid Kleckner <rnk@google.com>
Mon, 2 Nov 2015 20:49:29 +0000 (20:49 +0000)
committerReid Kleckner <rnk@google.com>
Mon, 2 Nov 2015 20:49:29 +0000 (20:49 +0000)
This found a bug in Clang's PTH implementation.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251829 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/OnDiskHashTable.h

index 48ec40ffbaf4feb01a34901104067298d9d47a88..c47134f46c8d5e798863f7e04ca782d84c877e2c 100644 (file)
@@ -171,8 +171,22 @@ public:
         LE.write<typename Info::hash_value_type>(I->Hash);
         const std::pair<offset_type, offset_type> &Len =
             InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
+#ifdef NDEBUG
         InfoObj.EmitKey(Out, I->Key, Len.first);
         InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
+#else
+        // In asserts mode, check that the users length matches the data they
+        // wrote.
+        uint64_t KeyStart = Out.tell();
+        InfoObj.EmitKey(Out, I->Key, Len.first);
+        uint64_t DataStart = Out.tell();
+        InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
+        uint64_t End = Out.tell();
+        assert(offset_type(DataStart - KeyStart) == Len.first &&
+               "key length does not match bytes written");
+        assert(offset_type(End - DataStart) == Len.second &&
+               "data length does not match bytes written");
+#endif
       }
     }