Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ggml/src/gguf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
Expand All @@ -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]))) {

Expand Down