diff --git a/src/llama-adapter.cpp b/src/llama-adapter.cpp index 3e0fe66af..e6678a66d 100644 --- a/src/llama-adapter.cpp +++ b/src/llama-adapter.cpp @@ -396,8 +396,11 @@ static void llama_adapter_lora_init_impl(llama_model & model, const char * path_ llama_file gguf_file(path_lora, "rb"); std::vector read_buf; auto set_tensor = [&](ggml_tensor * orig, ggml_tensor * dev) { - size_t offs = gguf_get_data_offset(ctx_gguf.get()) + gguf_get_tensor_offset(ctx_gguf.get(), gguf_find_tensor(ctx_gguf.get(), orig->name)); - size_t size = ggml_nbytes(orig); + const size_t offs = gguf_get_data_offset(ctx_gguf.get()) + gguf_get_tensor_offset(ctx_gguf.get(), gguf_find_tensor(ctx_gguf.get(), orig->name)); + const size_t size = ggml_nbytes(orig); + if (offs + size < offs || offs + size > gguf_file.size()) { + throw std::runtime_error(format("LoRA tensor '%s' data is not within the file bounds, file is corrupted or incomplete", orig->name)); + } read_buf.resize(size); gguf_file.seek(offs, SEEK_SET); gguf_file.read_raw(read_buf.data(), size);