From 9da4e6ea0e2631f0643578e8c3603d1f5f825179 Mon Sep 17 00:00:00 2001 From: Jukka Jylänki Date: Mon, 28 Mar 2016 15:33:57 +0300 Subject: Fix wasm::read_file() to abort if input file is too big to read in when building a 32-bit executable and size_t is not 64-bit. --- src/support/file.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/support/file.cpp') diff --git a/src/support/file.cpp b/src/support/file.cpp index 8813750d4..0401ea724 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -27,8 +27,13 @@ T wasm::read_file(const std::string &filename, bool debug) { exit(EXIT_FAILURE); } infile.seekg(0, std::ios::end); - size_t insize = infile.tellg(); - T input(insize + 1, '\0'); + std::streamoff insize = infile.tellg(); + if (sizeof(size_t) == 4 && insize >= 0xFFFFFFFFU) { + // Building a 32-bit executable where size_t == 32 bits, we are not able to create strings larger than 2^32 bytes in length, so must abort here. + std::cerr << "Failed opening '" << filename << "': Input file too large: " << insize << " bytes. Try rebuilding in 64-bit mode." << std::endl; + exit(EXIT_FAILURE); + } + T input((size_t)insize + 1, '\0'); infile.seekg(0); infile.read(&input[0], insize); return input; -- cgit v1.2.3 From 246c58952f4f56ebc9614459b218cbc885dd13d4 Mon Sep 17 00:00:00 2001 From: Jukka Jylänki Date: Mon, 28 Mar 2016 21:33:03 +0300 Subject: Use std::numeric_limits::max() instead of 0xFFFFFFFF. --- src/support/file.cpp | 5 +++-- src/wasm-s-parser.h | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'src/support/file.cpp') diff --git a/src/support/file.cpp b/src/support/file.cpp index 0401ea724..da7054f7f 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -17,6 +17,7 @@ #include "support/file.h" #include +#include template T wasm::read_file(const std::string &filename, bool debug) { @@ -27,8 +28,8 @@ T wasm::read_file(const std::string &filename, bool debug) { exit(EXIT_FAILURE); } infile.seekg(0, std::ios::end); - std::streamoff insize = infile.tellg(); - if (sizeof(size_t) == 4 && insize >= 0xFFFFFFFFU) { + std::streampos insize = infile.tellg(); + if (insize >= std::numeric_limits::max()) { // Building a 32-bit executable where size_t == 32 bits, we are not able to create strings larger than 2^32 bytes in length, so must abort here. std::cerr << "Failed opening '" << filename << "': Input file too large: " << insize << " bytes. Try rebuilding in 64-bit mode." << std::endl; exit(EXIT_FAILURE); diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 4c22fb1e3..c3f32e809 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -23,6 +23,7 @@ #define wasm_wasm_s_parser_h #include +#include #include "wasm.h" #include "mixed_arena.h" @@ -825,7 +826,7 @@ private: ret->align = atoi(eq); } else if (str[0] == 'o') { uint64_t offset = atoll(eq); - if (offset > 0xffffffff) onError(); + if (offset > std::numeric_limits::max()) onError(); ret->offset = (uint32_t)offset; } else onError(); i++; -- cgit v1.2.3 From 5cc2c2182332cd17bf12e3cdb58e61d0582eafc1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 28 Mar 2016 18:13:58 -0700 Subject: streampos may be signed on some platforms --- src/support/file.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/support/file.cpp') diff --git a/src/support/file.cpp b/src/support/file.cpp index da7054f7f..c93086990 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -29,12 +29,12 @@ T wasm::read_file(const std::string &filename, bool debug) { } infile.seekg(0, std::ios::end); std::streampos insize = infile.tellg(); - if (insize >= std::numeric_limits::max()) { + if (size_t(insize) >= std::numeric_limits::max()) { // Building a 32-bit executable where size_t == 32 bits, we are not able to create strings larger than 2^32 bytes in length, so must abort here. std::cerr << "Failed opening '" << filename << "': Input file too large: " << insize << " bytes. Try rebuilding in 64-bit mode." << std::endl; exit(EXIT_FAILURE); } - T input((size_t)insize + 1, '\0'); + T input(size_t(insize) + 1, '\0'); infile.seekg(0); infile.read(&input[0], insize); return input; -- cgit v1.2.3