Fix division by zero vulnerability in gguf_init_from_file_impl

This commit is contained in:
ylwango613 2026-01-04 16:18:41 +08:00
parent cef1d23c5a
commit 646e4d0c85
1 changed files with 8 additions and 1 deletions

View File

@ -545,7 +545,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
break;
}
}
// check for zero dimensions
if (ok && (info.t.ne[1] == 0 || info.t.ne[2] == 0 || info.t.ne[3] == 0)) {
GGML_LOG_ERROR("%s: tensor '%s' has zero dimension in shape "
"(%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ")\n",
__func__, info.t.name, info.t.ne[0], info.t.ne[1], info.t.ne[2], info.t.ne[3]);
ok = false;
}
// check that the total number of elements is representable
if (ok && ((INT64_MAX/info.t.ne[1] <= info.t.ne[0]) ||
(INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]) ||