浏览代码

Fix #2404 (Refactor make_file_body to improve file handling and scope management)

yhirose 1 周之前
父节点
当前提交
cb8365349f
共有 1 个文件被更改,包括 6 次插入3 次删除
  1. 6 3
      httplib.h

+ 6 - 3
httplib.h

@@ -1062,9 +1062,12 @@ make_file_provider(const std::string &name, const std::string &filepath,
 
 inline std::pair<size_t, ContentProvider>
 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<size_t>(f.tellg());
+  size_t size = 0;
+  {
+    std::ifstream f(filepath, std::ios::binary | std::ios::ate);
+    if (!f) { return {0, ContentProvider{}}; }
+    size = static_cast<size_t>(f.tellg());
+  }
 
   ContentProvider provider = [filepath](size_t offset, size_t length,
                                         DataSink &sink) -> bool {