Skip to content

Commit 26a48ad

Browse files
authored
ggml : prevent integer overflow in gguf tensor size calculation (#14595)
1 parent ffd59e7 commit 26a48ad

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ggml/src/gguf.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
631631
gguf_free(ctx);
632632
return nullptr;
633633
}
634-
ctx->size += GGML_PAD(ggml_nbytes(&ti.t), ctx->alignment);
634+
size_t padded_size = GGML_PAD(ggml_nbytes(&ti.t), ctx->alignment);
635+
if (SIZE_MAX - ctx->size < padded_size) {
636+
GGML_LOG_ERROR("%s: tensor '%s' size overflow, cannot accumulate size %zu + %zu\n",
637+
__func__, ti.t.name, ctx->size, padded_size);
638+
gguf_free(ctx);
639+
return nullptr;
640+
}
641+
ctx->size += padded_size;
635642
}
636643
}
637644

0 commit comments

Comments
 (0)