浏览代码

fix a bug of reading CRLF file on win32

blueloveTH 2 年之前
父节点
当前提交
bd351b055f
共有 2 个文件被更改,包括 6 次插入0 次删除
  1. 3 0
      build.ps1
  2. 3 0
      src/io.cpp

+ 3 - 0
build.ps1

@@ -1,3 +1,6 @@
+if (Test-Path build) {
+    Remove-Item -Recurse -Force build
+}
 mkdir build
 cd build
 cmake ..

+ 3 - 0
src/io.cpp

@@ -58,6 +58,9 @@ Bytes _default_import_handler(const Str& name){
             std::vector<char> buffer(ftell(io.fp));
             fseek(io.fp, 0, SEEK_SET);
             size_t sz = io_fread(buffer.data(), 1, buffer.size(), io.fp);
+            PK_ASSERT(sz <= buffer.size());
+            // in text mode, CR may be dropped, which may cause `sz < buffer.size()`
+            if(sz < buffer.size()) buffer.resize(sz);
             PK_UNUSED(sz);
             Bytes b(std::move(buffer));
             if(io.is_text()) return VAR(b.str());