Skip to content

Commit aa93162

Browse files
authored
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.
1 parent 4035680 commit aa93162

File tree

5 files changed

+5
-11
lines changed

5 files changed

+5
-11
lines changed

src/support/file.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) {
7171
<< "': Input file too large: " << insize
7272
<< " bytes. Try rebuilding in 64-bit mode.";
7373
}
74-
T input(size_t(insize) + (binary == Flags::Binary ? 0 : 1), '\0');
74+
// Zero-initialize the string or vector with the expected size.
75+
T input(size_t(insize), '\0');
7576
if (size_t(insize) == 0) {
7677
return input;
7778
}
@@ -82,8 +83,7 @@ T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) {
8283
// Truncate size to the number of ASCII characters actually read in text
8384
// mode (which is generally less than the number of bytes on Windows, if
8485
// \r\n line endings are present)
85-
input.resize(chars + 1);
86-
input[chars] = '\0';
86+
input.resize(chars);
8787
}
8888
return input;
8989
}

src/tools/wasm-ctor-eval.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,8 +1436,6 @@ int main(int argc, const char* argv[]) {
14361436
});
14371437
options.parse(argc, argv);
14381438

1439-
auto input(read_file<std::string>(options.extra["infile"], Flags::Text));
1440-
14411439
Module wasm;
14421440
options.applyFeatures(wasm);
14431441

src/tools/wasm-metadce.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ int main(int argc, const char* argv[]) {
485485
Fatal() << "no graph file provided.";
486486
}
487487

488-
auto input(read_file<std::string>(options.extra["infile"], Flags::Text));
489-
490488
Module wasm;
491489
options.applyFeatures(wasm);
492490

src/tools/wasm2js.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,7 @@ int main(int argc, const char* argv[]) {
976976
ModuleReader reader;
977977
reader.read(input, wasm, "");
978978
} else {
979-
auto input(
980-
read_file<std::vector<char>>(options.extra["infile"], Flags::Text));
979+
auto input(read_file<std::string>(options.extra["infile"], Flags::Text));
981980
if (options.debug) {
982981
std::cerr << "s-parsing..." << std::endl;
983982
}

src/wasm/wasm-io.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ bool useNewWATParser = false;
3939

4040
static void readTextData(std::string& input, Module& wasm, IRProfile profile) {
4141
if (useNewWATParser) {
42-
std::string_view in(input.c_str());
43-
if (auto parsed = WATParser::parseModule(wasm, in);
42+
if (auto parsed = WATParser::parseModule(wasm, input);
4443
auto err = parsed.getErr()) {
4544
Fatal() << err->msg;
4645
}

0 commit comments

Comments
 (0)