From cb8365349fbc5bf6da230a8f5a5126d1bf589845 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 22 Mar 2026 22:40:01 -0400 Subject: [PATCH] Fix #2404 (Refactor make_file_body to improve file handling and scope management) --- httplib.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 8698705..e2a838a 100644 --- a/httplib.h +++ b/httplib.h @@ -1062,9 +1062,12 @@ make_file_provider(const std::string &name, const std::string &filepath, inline std::pair make_file_body(const std::string &filepath) { - std::ifstream f(filepath, std::ios::binary | std::ios::ate); - if (!f) { return {0, ContentProvider{}}; } - auto size = static_cast(f.tellg()); + size_t size = 0; + { + std::ifstream f(filepath, std::ios::binary | std::ios::ate); + if (!f) { return {0, ContentProvider{}}; } + size = static_cast(f.tellg()); + } ContentProvider provider = [filepath](size_t offset, size_t length, DataSink &sink) -> bool {