]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
ggml : prevent integer overflow in gguf tensor size calculation (#14595)
authorMiaoqian Lin <redacted>
Wed, 9 Jul 2025 12:33:53 +0000 (20:33 +0800)
committerGitHub <redacted>
Wed, 9 Jul 2025 12:33:53 +0000 (14:33 +0200)
ggml/src/gguf.cpp

index 5ffd12b8b27950c4de2b094a9efb0ef32561009f..53504399c57f429d6dfdcc0488350d9379ef4926 100644 (file)
@@ -631,7 +631,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
                 gguf_free(ctx);
                 return nullptr;
             }
-            ctx->size += GGML_PAD(ggml_nbytes(&ti.t), ctx->alignment);
+            size_t padded_size = GGML_PAD(ggml_nbytes(&ti.t), ctx->alignment);
+            if (SIZE_MAX - ctx->size < padded_size) {
+                GGML_LOG_ERROR("%s: tensor '%s' size overflow, cannot accumulate size %zu + %zu\n",
+                    __func__, ti.t.name, ctx->size, padded_size);
+                gguf_free(ctx);
+                return nullptr;
+            }
+            ctx->size += padded_size;
         }
     }