diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp index f9a4a9131b..9c41453d8f 100644 --- a/ggml/src/gguf.cpp +++ b/ggml/src/gguf.cpp @@ -704,7 +704,17 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par // compute the exact size needed for the new ggml_context size_t mem_size = 0; - { + if (params.no_alloc) { + if (n_tensors != 0 && SIZE_MAX / n_tensors < ggml_tensor_overhead()) { + GGML_LOG_ERROR("%s: memory size overflow while allocating ggml context\n", __func__); + gguf_free(ctx); + return nullptr; + } + + const size_t overhead = n_tensors * ggml_tensor_overhead(); + + mem_size = overhead; + } else { if ((n_tensors + 1) != 0 && SIZE_MAX / (n_tensors + 1) < ggml_tensor_overhead()) { GGML_LOG_ERROR("%s: memory size overflow while allocating ggml context\n", __func__); gguf_free(ctx); @@ -712,6 +722,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par } const size_t overhead = (n_tensors + 1) * ggml_tensor_overhead(); + if (SIZE_MAX - overhead < ctx->size) { GGML_LOG_ERROR("%s: memory size overflow while allocating ggml context\n", __func__); gguf_free(ctx);