summaryrefslogtreecommitdiff
path: root/src/support
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-04-24 16:59:26 -0700
committerGitHub <noreply@github.com>2024-04-24 23:59:26 +0000
commitaa931628d73a7ca69305760c161654aee8c20fe2 (patch)
treed46b277031858a564fa4e6ca3c0dca845d76dded /src/support
parent403568091b42c6bfe38e46d73d8d10cd4c0747e9 (diff)
downloadbinaryen-aa931628d73a7ca69305760c161654aee8c20fe2.tar.gz
binaryen-aa931628d73a7ca69305760c161654aee8c20fe2.tar.bz2
binaryen-aa931628d73a7ca69305760c161654aee8c20fe2.zip
Do not add an extra null character when reading files (#6538)
The new wat parser currently considers itself to be at the end of the file whenever it cannot lex another token. This is not quite right, but fixing it causes parser errors because of the extra null character we were appending to files when we read them. This null character is not useful since we can already read files as `std::string`, which always has an implicit null character, so remove it. Clean up some users of `read_file` while we're at it.
Diffstat (limited to 'src/support')
-rw-r--r--src/support/file.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/support/file.cpp b/src/support/file.cpp
index feb05e136..08b9e31af 100644
--- a/src/support/file.cpp
+++ b/src/support/file.cpp
@@ -71,7 +71,8 @@ T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) {
<< "': Input file too large: " << insize
<< " bytes. Try rebuilding in 64-bit mode.";
}
- T input(size_t(insize) + (binary == Flags::Binary ? 0 : 1), '\0');
+ // Zero-initialize the string or vector with the expected size.
+ T input(size_t(insize), '\0');
if (size_t(insize) == 0) {
return input;
}
@@ -82,8 +83,7 @@ T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) {
// 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.resize(chars + 1);
- input[chars] = '\0';
+ input.resize(chars);
}
return input;
}