Skip to content

Commit 7ff86d0

Browse files
authored
Merge pull request #1985 from janhq/fix/gguf_parser
Fix: Prevent Out-of-Bounds Reads in GGUF Parser
2 parents 58c071c + 6ffed73 commit 7ff86d0

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

engine/config/gguf_parser.cc

+7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ std::pair<std::size_t, std::string> GGUFHandler::ReadString(
104104
uint64_t length;
105105
std::memcpy(&length, data_ + offset, sizeof(uint64_t));
106106

107+
if (offset + 8 + length > file_size_) {
108+
throw std::runtime_error("GGUF metadata string length exceeds file size.\n");
109+
}
110+
107111
std::string value(reinterpret_cast<const char*>(data_ + offset + 8), length);
108112
return {8 + static_cast<std::size_t>(length), value};
109113
}
@@ -274,6 +278,9 @@ size_t GGUFHandler::ReadArray(std::size_t offset, const std::string& key) {
274278
}
275279

276280
array_offset += length;
281+
if (offset + array_offset > file_size_) {
282+
throw std::runtime_error("GGUF Parser Array exceeded file size.\n");
283+
}
277284
}
278285
if (array_values_string.size() > 0)
279286
metadata_array_string_[key] = array_values_string;

0 commit comments

Comments
 (0)