Skip to content

Commit 275c5eb

Browse files
jujkripken
authored andcommitted
Fix wasm::read_file() to read correctly sized input strings in text mode. (#1088)
1 parent e865f2f commit 275c5eb

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

src/support/file.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ T wasm::read_file(const std::string &filename, Flags::BinaryOption binary, Flags
4040
exit(EXIT_FAILURE);
4141
}
4242
T input(size_t(insize) + (binary == Flags::Binary ? 0 : 1), '\0');
43+
if (size_t(insize) == 0) return input;
4344
infile.seekg(0);
4445
infile.read(&input[0], insize);
46+
if (binary == Flags::Text) {
47+
size_t chars = size_t(infile.gcount());
48+
input.resize(chars+1); // Truncate size to the number of ASCII characters actually read in text mode (which is generally less than the number of bytes on Windows, if \r\n line endings are present)
49+
input[chars] = '\0';
50+
}
4551
return input;
4652
}
4753

0 commit comments

Comments
 (0)