From: triplenom Date: Thu, 1 Jan 2026 02:24:43 +0000 (-0500) Subject: llama: handle short reads in direct I/O path (#18504) X-Git-Tag: upstream/0.0.7599 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=0f8d11f7d2aa45faee6e8ee9d08f1a77c0c1b80e;p=pkg%2Fggml%2Fsources%2Fllama.cpp llama: handle short reads in direct I/O path (#18504) --- diff --git a/src/llama-mmap.cpp b/src/llama-mmap.cpp index 23b648a2..232005e1 100644 --- a/src/llama-mmap.cpp +++ b/src/llama-mmap.cpp @@ -240,9 +240,10 @@ struct llama_file::impl { throw std::runtime_error("unexpectedly reached end of file"); } } else { - bool successful = false; - while (!successful) { - off_t ret = read(fd, ptr, len); + size_t bytes_read = 0; + while (bytes_read < len) { + const size_t to_read = len - bytes_read; + ssize_t ret = ::read(fd, reinterpret_cast(ptr) + bytes_read, to_read); if (ret == -1) { if (errno == EINTR) { @@ -251,10 +252,16 @@ struct llama_file::impl { throw std::runtime_error(format("read error: %s", strerror(errno))); } if (ret == 0) { + // EOF: allow if this read was only pulling alignment padding past file end + off_t pos = lseek(fd, 0, SEEK_CUR); + if (pos != -1 && (size_t) pos == size) { + std::memset(reinterpret_cast(ptr) + bytes_read, 0, len - bytes_read); + return; + } throw std::runtime_error("unexpectedly reached end of file"); } - successful = true; + bytes_read += (size_t) ret; } } }