Skip to content

Commit d72bfa3

Browse files
authored
gguf : reject empty metadata keys (ggml-org#24917)
1 parent 3cec3bc commit d72bfa3

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

ggml/src/gguf.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ static struct gguf_context * gguf_init_from_reader(const struct gguf_reader & gr
557557
GGML_LOG_ERROR("%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i);
558558
ok = false;
559559
}
560+
if (ok && key.empty()) {
561+
GGML_LOG_ERROR("%s: key %" PRIi64 " is empty\n", __func__, i);
562+
ok = false;
563+
}
560564
for (size_t j = 0; ok && j < ctx->kv.size(); ++j) {
561565
if (key == ctx->kv[j].key) {
562566
GGML_LOG_ERROR("%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n", __func__, key.c_str(), j, i);

tests/test-gguf.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum handcrafted_file_type {
2626
HANDCRAFTED_HEADER_EMPTY = 800,
2727

2828
HANDCRAFTED_KV_BAD_KEY_SIZE = 10 + offset_has_kv,
29+
HANDCRAFTED_KV_EMPTY_KEY = 15 + offset_has_kv,
2930
HANDCRAFTED_KV_BAD_TYPE = 20 + offset_has_kv,
3031
// HANDCRAFTED_KV_BAD_VALUE_SIZE = 30 + offset_has_kv, // removed because it can result in allocations > 1 TB (default sanitizer limit)
3132
HANDCRAFTED_KV_DUPLICATE_KEY = 40 + offset_has_kv,
@@ -64,6 +65,7 @@ static std::string handcrafted_file_type_name(const enum handcrafted_file_type h
6465
case HANDCRAFTED_HEADER_EMPTY: return "HEADER_EMPTY";
6566

6667
case HANDCRAFTED_KV_BAD_KEY_SIZE: return "KV_BAD_KEY_SIZE";
68+
case HANDCRAFTED_KV_EMPTY_KEY: return "KV_EMPTY_KEY";
6769
case HANDCRAFTED_KV_BAD_TYPE: return "KV_BAD_TYPE";
6870
case HANDCRAFTED_KV_DUPLICATE_KEY: return "KV_DUPLICATE_KEY";
6971
case HANDCRAFTED_KV_BAD_ALIGN: return "KV_BAD_ALIGN";
@@ -284,7 +286,9 @@ static FILE * get_handcrafted_file(const unsigned int seed, const enum handcraft
284286
const enum gguf_type type = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].first);
285287
const enum gguf_type type_arr = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].second);
286288

287-
const std::string key = "my_key_" + std::to_string((hft == HANDCRAFTED_KV_DUPLICATE_KEY ? i/2 : i));
289+
const std::string key = hft == HANDCRAFTED_KV_EMPTY_KEY
290+
? ""
291+
: "my_key_" + std::to_string((hft == HANDCRAFTED_KV_DUPLICATE_KEY ? i/2 : i));
288292

289293
if (hft == HANDCRAFTED_KV_BAD_KEY_SIZE) {
290294
const uint64_t n = -1;
@@ -732,6 +736,7 @@ static std::pair<int, int> test_handcrafted_file(const unsigned int seed) {
732736
HANDCRAFTED_HEADER_EMPTY,
733737

734738
HANDCRAFTED_KV_BAD_KEY_SIZE,
739+
HANDCRAFTED_KV_EMPTY_KEY,
735740
HANDCRAFTED_KV_BAD_TYPE,
736741
HANDCRAFTED_KV_DUPLICATE_KEY,
737742
HANDCRAFTED_KV_BAD_ALIGN,

0 commit comments

Comments
 (0)