Commit 22d68b8ac for llama.cpp

commit 22d68b8ac3be9f81121b52268de6a8d094dacc07
Author: Pascal <admin@serveurperso.com>
Date:   Mon Nov 30 16:30:43 2026 +0100

    gguf: reject keys and tensor names containing NUL bytes (#33355)

    * gguf: reject keys and tensor names containing NUL bytes

    Keys and tensor names are compared as full strings when checking for
    duplicates, but looked up as C strings by gguf_find_key and ggml_set_name.
    A key such as tokenizer.chat_template0x therefore shadows the real key
    for llama.cpp while other readers still see the original value, and two
    tensor names differing only after a NUL collapse to the same name.

    * gguf-py: reject keys and tensor names containing NUL bytes

    Match the C parser, so that tools built on GGUFReader such as model scanners
    flag these files instead of showing a value that llama.cpp does not use.

diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp
index 1a57b608e..61e93553b 100644
--- a/ggml/src/gguf.cpp
+++ b/ggml/src/gguf.cpp
@@ -554,6 +554,10 @@ static struct gguf_context * gguf_init_from_reader(const struct gguf_reader & gr
                 GGML_LOG_ERROR("%s: key %" PRIi64 " is empty\n", __func__, i);
                 ok = false;
             }
+            if (ok && key.find('\0') != std::string::npos) {
+                GGML_LOG_ERROR("%s: key %" PRIi64 " contains a NUL byte\n", __func__, i);
+                ok = false;
+            }
             if (ok && !seen_keys.insert(key).second) {
                 GGML_LOG_ERROR("%s: duplicate key '%s' for KV pair %" PRIi64 "\n", __func__, key.c_str(), i);
                 ok = false;
@@ -636,6 +640,11 @@ static struct gguf_context * gguf_init_from_reader(const struct gguf_reader & gr
                 GGML_LOG_ERROR("%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n", __func__, i);
                 ok = false;
             }
+            if (ok && name.find('\0') != std::string::npos) {
+                GGML_LOG_ERROR("%s: tensor name %" PRIi64 " contains a NUL byte\n", __func__, i);
+                ok = false;
+                break;
+            }
             if (name.length() >= GGML_MAX_NAME) {
                 GGML_LOG_ERROR("%s: tensor name %" PRIi64 " is too long: %zu >= %d\n", __func__, i, name.length(), GGML_MAX_NAME);
                 ok = false;
diff --git a/gguf-py/gguf/gguf_reader.py b/gguf-py/gguf/gguf_reader.py
index d8b07ef01..b059a2e8f 100644
--- a/gguf-py/gguf/gguf_reader.py
+++ b/gguf-py/gguf/gguf_reader.py
@@ -296,9 +296,13 @@ class GGUFReader:
         offset_tensor = self._get(offs, np.uint64)
         offs += int(offset_tensor.nbytes)

+        name = str(bytes(name_data), encoding = 'utf-8')
+        if '\0' in name:
+            raise ValueError(f'Tensor name {name!r} contains a NUL byte')
+
         return ReaderField(
             orig_offs,
-            str(bytes(name_data), encoding = 'utf-8'),
+            name,
             [name_len, name_data, n_dims, dims, raw_dtype, offset_tensor],
             [1, 3, 4, 5],
         )
@@ -308,6 +312,9 @@ class GGUFReader:
             orig_offs = offs
             kv_klen, kv_kdata = self._get_str(offs)
             offs += int(kv_klen.nbytes + kv_kdata.nbytes)
+            key = str(bytes(kv_kdata), encoding = 'utf-8')
+            if '\0' in key:
+                raise ValueError(f'Key {key!r} contains a NUL byte')
             raw_kv_type = self._get(offs, np.uint32)
             offs += int(raw_kv_type.nbytes)
             parts: list[npt.NDArray[Any]] = [kv_klen, kv_kdata, raw_kv_type]
@@ -316,7 +323,7 @@ class GGUFReader:
             parts += field_parts
             self._push_field(ReaderField(
                 orig_offs,
-                str(bytes(kv_kdata), encoding = 'utf-8'),
+                key,
                 parts,
                 [idx + idxs_offs for idx in field_idxs],
                 field_types,