Skip to content

Commit be9d9ea

Browse files
texmex76Bernhard Gstrein
authored andcommitted
gguf : fix potential infinite loops while parsing (ggml-org#4100)
Co-authored-by: Bernhard Gstrein <gstrein@cs.uni-freiburg.de>
1 parent 6805b92 commit be9d9ea

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

ggml.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -18073,7 +18073,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
1807318073
{
1807418074
ctx->kv = malloc(ctx->header.n_kv * sizeof(struct gguf_kv));
1807518075

18076-
for (uint32_t i = 0; i < ctx->header.n_kv; ++i) {
18076+
for (uint64_t i = 0; i < ctx->header.n_kv; ++i) {
1807718077
struct gguf_kv * kv = &ctx->kv[i];
1807818078

1807918079
//fprintf(stderr, "%s: reading kv %d\n", __func__, i);
@@ -18120,7 +18120,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
1812018120
case GGUF_TYPE_STRING:
1812118121
{
1812218122
kv->value.arr.data = malloc(kv->value.arr.n * sizeof(struct gguf_str));
18123-
for (uint32_t j = 0; j < kv->value.arr.n; ++j) {
18123+
for (uint64_t j = 0; j < kv->value.arr.n; ++j) {
1812418124
ok = ok && gguf_fread_str(file, &((struct gguf_str *) kv->value.arr.data)[j], &offset);
1812518125
}
1812618126
} break;
@@ -18148,7 +18148,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
1814818148
{
1814918149
ctx->infos = malloc(ctx->header.n_tensors * sizeof(struct gguf_tensor_info));
1815018150

18151-
for (uint32_t i = 0; i < ctx->header.n_tensors; ++i) {
18151+
for (uint64_t i = 0; i < ctx->header.n_tensors; ++i) {
1815218152
struct gguf_tensor_info * info = &ctx->infos[i];
1815318153

1815418154
for (int j = 0; j < GGML_MAX_DIMS; ++j) {
@@ -18195,7 +18195,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
1819518195
// compute the total size of the data section, taking into account the alignment
1819618196
{
1819718197
ctx->size = 0;
18198-
for (uint32_t i = 0; i < ctx->header.n_tensors; ++i) {
18198+
for (uint64_t i = 0; i < ctx->header.n_tensors; ++i) {
1819918199
struct gguf_tensor_info * info = &ctx->infos[i];
1820018200

1820118201
const int64_t ne =
@@ -18264,7 +18264,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
1826418264
ggml_set_no_alloc(ctx_data, true);
1826518265

1826618266
// create the tensors
18267-
for (uint32_t i = 0; i < ctx->header.n_tensors; ++i) {
18267+
for (uint64_t i = 0; i < ctx->header.n_tensors; ++i) {
1826818268
const int64_t ne[GGML_MAX_DIMS] = {
1826918269
ctx->infos[i].ne[0],
1827018270
ctx->infos[i].ne[1],

0 commit comments

Comments
 (0)