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); } };