Skip to content

Commit 8805942

Browse files
committed
ggml : prevent integer overflow in tensor size calculation ggml-org#14595
Author : Yuuoniy
1 parent 0bb6675 commit 8805942

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
@@ -677,7 +677,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
677677
gguf_free(ctx);
678678
return nullptr;
679679
}
680-
ctx->size += GGML_PAD(ggml_nbytes(&ti.t), ctx->alignment);
680+
size_t padded_size = GGML_PAD(ggml_nbytes(&ti.t), ctx->alignment);
681+
if (SIZE_MAX - ctx->size < padded_size) {
682+
GGML_LOG_ERROR("%s: tensor '%s' size overflow, cannot accumulate size %zu + %zu\n",
683+
__func__, ti.t.name, ctx->size, padded_size);
684+
gguf_free(ctx);
685+
return nullptr;
686+
}
687+
ctx->size += padded_size;
681688
}
682689
}
683690

0 commit comments

Comments
 (0)