Commit 6d0716cff for llama.cpp
commit 6d0716cff4966cb587c758b1c1baec13ee950fcd
Author: Pascal <admin@serveurperso.com>
Date: Tue Dec 1 15:43:55 2026 +0100
gguf: document and test the NUL byte rule for keys and tensor names (#33443)
State in the format description at the top of gguf.h that keys and
tensor names must not contain null bytes while string values can.
Add handcrafted test cases for both: a key with an embedded NUL placed
before the key it would shadow, and tensor names that only differ after
a NUL. Both files are now expected to be rejected.
diff --git a/ggml/include/gguf.h b/ggml/include/gguf.h
index b3a1e1230..7423d3fcc 100644
--- a/ggml/include/gguf.h
+++ b/ggml/include/gguf.h
@@ -24,6 +24,7 @@
// 7. The tensor data binary blob (optional, aligned).
//
// Strings are serialized as the string length (uint64_t) followed by the C string without the null terminator.
+// Keys and tensor names must not contain null bytes, string values can.
// All enums are stored as int32_t.
// All bool values are stored as int8_t.
// If the special key "general.alignment" (uint32_t) is defined it is used for alignment,
diff --git a/tests/test-gguf.cpp b/tests/test-gguf.cpp
index 6c63ecaeb..f01171076 100644
--- a/tests/test-gguf.cpp
+++ b/tests/test-gguf.cpp
@@ -30,6 +30,7 @@ enum handcrafted_file_type {
HANDCRAFTED_KV_BAD_TYPE = 20 + offset_has_kv,
// HANDCRAFTED_KV_BAD_VALUE_SIZE = 30 + offset_has_kv, // removed because it can result in allocations > 1 TB (default sanitizer limit)
HANDCRAFTED_KV_DUPLICATE_KEY = 40 + offset_has_kv,
+ HANDCRAFTED_KV_NUL_KEY = 45 + offset_has_kv,
HANDCRAFTED_KV_BAD_ALIGN = 50 + offset_has_kv,
HANDCRAFTED_KV_WRONG_TYPE_ALIGN = 55 + offset_has_kv,
HANDCRAFTED_KV_SUCCESS = 800 + offset_has_kv,
@@ -44,6 +45,7 @@ enum handcrafted_file_type {
HANDCRAFTED_TENSORS_BAD_TYPE = 50 + offset_has_tensors,
HANDCRAFTED_TENSORS_BAD_OFFSET = 60 + offset_has_tensors,
HANDCRAFTED_TENSORS_DUPLICATE_NAME = 70 + offset_has_tensors,
+ HANDCRAFTED_TENSORS_NUL_NAME = 72 + offset_has_tensors,
HANDCRAFTED_TENSORS_BAD_ALIGN = 75 + offset_has_tensors,
HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN = 80 + offset_has_tensors,
HANDCRAFTED_TENSORS_SUCCESS = 800 + offset_has_tensors,
@@ -71,6 +73,7 @@ static std::string handcrafted_file_type_name(const enum handcrafted_file_type h
case HANDCRAFTED_KV_EMPTY_KEY: return "KV_EMPTY_KEY";
case HANDCRAFTED_KV_BAD_TYPE: return "KV_BAD_TYPE";
case HANDCRAFTED_KV_DUPLICATE_KEY: return "KV_DUPLICATE_KEY";
+ case HANDCRAFTED_KV_NUL_KEY: return "KV_NUL_KEY";
case HANDCRAFTED_KV_BAD_ALIGN: return "KV_BAD_ALIGN";
case HANDCRAFTED_KV_WRONG_TYPE_ALIGN: return "KV_WRONG_TYPE_ALIGN";
case HANDCRAFTED_KV_SUCCESS: return "KV_RANDOM_KV";
@@ -85,6 +88,7 @@ static std::string handcrafted_file_type_name(const enum handcrafted_file_type h
case HANDCRAFTED_TENSORS_BAD_TYPE: return "TENSORS_BAD_TYPE";
case HANDCRAFTED_TENSORS_BAD_OFFSET: return "TENSORS_BAD_OFFSET";
case HANDCRAFTED_TENSORS_DUPLICATE_NAME: return "TENSORS_DUPLICATE_NAME";
+ case HANDCRAFTED_TENSORS_NUL_NAME: return "TENSORS_NUL_NAME";
case HANDCRAFTED_TENSORS_BAD_ALIGN: return "TENSORS_BAD_ALIGN";
case HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN: return "TENSORS_INCONSISTENT_ALIGN";
case HANDCRAFTED_TENSORS_SUCCESS: return "TENSORS_SUCCESS";
@@ -302,9 +306,12 @@ static FILE * get_handcrafted_file(const unsigned int seed, const enum handcraft
const enum gguf_type type = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].first);
const enum gguf_type type_arr = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].second);
- const std::string key = hft == HANDCRAFTED_KV_EMPTY_KEY
+ std::string key = hft == HANDCRAFTED_KV_EMPTY_KEY
? ""
- : "my_key_" + std::to_string((hft == HANDCRAFTED_KV_DUPLICATE_KEY ? i/2 : i));
+ : "my_key_" + std::to_string((hft == HANDCRAFTED_KV_DUPLICATE_KEY || hft == HANDCRAFTED_KV_NUL_KEY ? i/2 : i));
+ if (hft == HANDCRAFTED_KV_NUL_KEY && i % 2 == 0) {
+ key += std::string("\0x", 2);
+ }
if (hft == HANDCRAFTED_KV_BAD_KEY_SIZE) {
const uint64_t n = -1;
@@ -401,6 +408,9 @@ static FILE * get_handcrafted_file(const unsigned int seed, const enum handcraft
const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;
std::string name = "my_tensor";
+ if (hft == HANDCRAFTED_TENSORS_NUL_NAME) {
+ name += '\0';
+ }
if (hft != HANDCRAFTED_TENSORS_DUPLICATE_NAME) {
name += "_" + std::to_string(i);
}
@@ -773,6 +783,7 @@ static std::pair<int, int> test_handcrafted_file(const unsigned int seed) {
HANDCRAFTED_KV_EMPTY_KEY,
HANDCRAFTED_KV_BAD_TYPE,
HANDCRAFTED_KV_DUPLICATE_KEY,
+ HANDCRAFTED_KV_NUL_KEY,
HANDCRAFTED_KV_BAD_ALIGN,
HANDCRAFTED_KV_WRONG_TYPE_ALIGN,
HANDCRAFTED_KV_SUCCESS,
@@ -787,6 +798,7 @@ static std::pair<int, int> test_handcrafted_file(const unsigned int seed) {
HANDCRAFTED_TENSORS_BAD_TYPE,
HANDCRAFTED_TENSORS_BAD_OFFSET,
HANDCRAFTED_TENSORS_DUPLICATE_NAME,
+ HANDCRAFTED_TENSORS_NUL_NAME,
HANDCRAFTED_TENSORS_BAD_ALIGN,
HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN,
HANDCRAFTED_TENSORS_SUCCESS,