diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp index ab3cc974867..b4655cbf138 100644 --- a/ggml/src/gguf.cpp +++ b/ggml/src/gguf.cpp @@ -508,6 +508,11 @@ struct gguf_context * gguf_init_from_file_ptr(FILE * file, struct gguf_init_para GGML_LOG_ERROR("%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i); ok = false; } + // check that key is not empty (empty key would trigger GGML_ASSERT) + if (ok && key.empty()) { + GGML_LOG_ERROR("%s: key %" PRIi64 " has empty name\n", __func__, i); + ok = false; + } for (size_t j = 0; ok && j < ctx->kv.size(); ++j) { if (key == ctx->kv[j].key) { GGML_LOG_ERROR("%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n", __func__, key.c_str(), j, i); @@ -619,7 +624,7 @@ struct gguf_context * gguf_init_from_file_ptr(FILE * file, struct gguf_init_para ok = ok && gr.read(info.t.ne[j]); } - // check that all ne are non-negative + // check that all ne are non-negative (negative is invalid; zero-sized tensors are valid in ggml) if (info.t.ne[j] < 0) { GGML_LOG_ERROR("%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n", __func__, info.t.name, j, info.t.ne[j]); @@ -629,7 +634,9 @@ struct gguf_context * gguf_init_from_file_ptr(FILE * file, struct gguf_init_para } // check that the total number of elements is representable - if (ok && ((INT64_MAX/info.t.ne[1] <= info.t.ne[0]) || + // (skip when any dim is 0: zero-sized tensors have 0 elements, no overflow) + if (ok && info.t.ne[1] != 0 && info.t.ne[2] != 0 && info.t.ne[3] != 0 && + ((INT64_MAX/info.t.ne[1] <= info.t.ne[0]) || (INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]) || (INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]))) {