From 8f97d82da33b5f4262a87b9c4208298378b8a165 Mon Sep 17 00:00:00 2001 From: ppAlexa Date: Mon, 19 Oct 2026 11:57:46 +0530 Subject: [PATCH] tools : reject truncated GGUF tensor data in split and export tools (#30497) * The problem here in the repo is gguf-split can silently produce corrupted tensor output when the input GGUF file is truncated. The fix is to add tensor-data bounds checks and short-read checks in gguf-split.cpp and export-lora.cpp, and a regression test that checks truncated input is rejected and valid tensor data is preserved.# * Bug : GGUF-split can silently produce corrupted tensor output when the input GGUF file is truncated. Fix : add short-read checks in gguf-split.cpp (in copy_file_to_file in split_strategy) and export-lora.cpp (in read_tensor_data of file_input) so that truncated input is rejected. --- tools/export-lora/export-lora.cpp | 3 +++ tools/gguf-split/gguf-split.cpp | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/tools/export-lora/export-lora.cpp b/tools/export-lora/export-lora.cpp index e1bc4a2f3..cd7ac39d2 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 c6cdbb98e..173455938 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); } };