9
9
#include " llvm/CAS/OnDiskHashMappedTrie.h"
10
10
#include " HashMappedTrieIndexGenerator.h"
11
11
#include " llvm/ADT/ScopeExit.h"
12
+ #include " llvm/ADT/StringExtras.h"
12
13
#include " llvm/ADT/StringMap.h"
13
14
#include " llvm/CAS/MappedFileRegionBumpPtr.h"
14
15
#include " llvm/Support/Compiler.h"
@@ -1168,6 +1169,11 @@ class TriePrinter : public TrieVisitor {
1168
1169
};
1169
1170
} // namespace
1170
1171
1172
+ static Error createInvalidTrieError (uint64_t Offset, const Twine &Msg) {
1173
+ return createStringError (make_error_code (std::errc::protocol_error),
1174
+ " invalid trie at 0x" + toHex (Offset) + " : " + Msg);
1175
+ }
1176
+
1171
1177
Error TrieVisitor::visit (HashMappedTrieHandle Trie) {
1172
1178
auto Root = Trie.getRoot ();
1173
1179
if (!Root)
@@ -1179,6 +1185,11 @@ Error TrieVisitor::visit(HashMappedTrieHandle Trie) {
1179
1185
}
1180
1186
1181
1187
Error TrieVisitor::traverseTrieNode (SubtrieHandle Node, StringRef Prefix) {
1188
+ char *Addr = reinterpret_cast <char *>(&Node.getHeader ());
1189
+ if (Addr + Node.getSize () >=
1190
+ Trie.getRegion ().data () + Trie.getRegion ().size ())
1191
+ return createInvalidTrieError ((uint64_t )Addr, " subtrie node out of bound" );
1192
+
1182
1193
if (auto Err = visitSubTrie (Prefix, Node))
1183
1194
return Err;
1184
1195
@@ -1189,6 +1200,9 @@ Error TrieVisitor::traverseTrieNode(SubtrieHandle Node, StringRef Prefix) {
1189
1200
SubtrieSlotValue Slot = Node.load (I);
1190
1201
if (!Slot)
1191
1202
continue ;
1203
+ uint64_t Offset = Slot.isSubtrie () ? Slot.asSubtrie () : Slot.asData ();
1204
+ if (Offset >= (uint64_t )Trie.getRegion ().size ())
1205
+ return createInvalidTrieError (Offset, " slot points out of bound" );
1192
1206
std::string SubtriePrefix = Prefix.str ();
1193
1207
appendIndexBits (SubtriePrefix, I, NumSlots);
1194
1208
if (Slot.isSubtrie ()) {
0 commit comments