Skip to content

LLM inference in C/C++

C++ ggml

tools : reject truncated GGUF tensor data in split and export tools (#30497)

ppAlexa8f97d82da33b
diff --git a/tools/export-lora/export-lora.cpp b/tools/export-lora/export-lora.cpp
index e1bc4a2f31..cd7ac39d25 100644
--- a/tools/export-lora/export-lora.cpp
+++ b/tools/export-lora/export-lora.cpp
@@ -104,6 +104,9 @@ struct file_input {
auto offset = gguf_get_data_offset(ctx_gguf) + gguf_get_tensor_offset(ctx_gguf, i_tensor_in);
f_in.seekg(offset);
f_in.read((char* )buf.data(), len);
+ if (!f_in) {
+ throw std::runtime_error("failed to read data for tensor " + name + ", input file may be truncated");
+ }
}
~file_input() {
diff --git a/tools/gguf-split/gguf-split.cpp b/tools/gguf-split/gguf-split.cpp
index c6cdbb98e2..1734559388 100644
--- a/tools/gguf-split/gguf-split.cpp
+++ b/tools/gguf-split/gguf-split.cpp
@@ -362,6 +362,10 @@ struct split_strategy {
}
f_in.seekg(in_offset);
f_in.read((char *)read_buf.data(), len);
+ if (!f_in) {
+ fprintf(stderr, "%s: failed to read %zu bytes at offset %zu, input file may be truncated\n", __func__, len, in_offset);
+ exit(EXIT_FAILURE);
+ }
f_out.write((const char *)read_buf.data(), len);
}
};