diff options
author | juj <jujjyl@gmail.com> | 2017-07-18 19:41:21 +0300 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2017-07-18 09:41:21 -0700 |
commit | 275c5ebafdf2860edb965d322bdad4c3e3717bea (patch) | |
tree | c9ca1929de3ebacfa326bd62d68380f4cfb257b1 /src/support/file.cpp | |
parent | e865f2fa2863b6e91521c059a61a4483769bf5c9 (diff) | |
download | binaryen-275c5ebafdf2860edb965d322bdad4c3e3717bea.tar.gz binaryen-275c5ebafdf2860edb965d322bdad4c3e3717bea.tar.bz2 binaryen-275c5ebafdf2860edb965d322bdad4c3e3717bea.zip |
Fix wasm::read_file() to read correctly sized input strings in text mode. (#1088)
Diffstat (limited to 'src/support/file.cpp')
-rw-r--r-- | src/support/file.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/support/file.cpp b/src/support/file.cpp index fd38a7ae3..c1d9e6e33 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -40,8 +40,14 @@ T wasm::read_file(const std::string &filename, Flags::BinaryOption binary, Flags exit(EXIT_FAILURE); } T input(size_t(insize) + (binary == Flags::Binary ? 0 : 1), '\0'); + if (size_t(insize) == 0) return input; infile.seekg(0); infile.read(&input[0], insize); + if (binary == Flags::Text) { + size_t chars = size_t(infile.gcount()); + 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) + input[chars] = '\0'; + } return input; } |