Skip to content

Distribute and run LLMs with a single file.

Check for i/o errors in httplib read_file()

Justine Tunney41f243e67806
diff --git a/llama.cpp/server/httplib.h b/llama.cpp/server/httplib.h
index 28746000..98c59d5d 100644
--- a/llama.cpp/server/httplib.h
+++ b/llama.cpp/server/httplib.h
@@ -2227,12 +2227,18 @@ inline std::string decode_url(const std::string &s,
}
inline void read_file(const std::string &path, std::string &out) {
- std::ifstream fs(path, std::ios_base::binary);
- fs.seekg(0, std::ios_base::end);
- auto size = fs.tellg();
- fs.seekg(0);
- out.resize(static_cast<size_t>(size));
- fs.read(&out[0], static_cast<std::streamsize>(size));
+ out.resize(0);
+ int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
+ if (fd != -1) {
+ ssize_t size = lseek(fd, 0, SEEK_END);
+ if (size != -1) {
+ out.resize(static_cast<size_t>(size));
+ ssize_t got = pread(fd, &out[0], size, 0);
+ if (got == -1) got = 0;
+ out.resize(static_cast<size_t>(got));
+ }
+ close(fd);
+ }
}
inline std::string file_extension(const std::string &path) {