diff --git a/single_include/toml.hpp b/single_include/toml.hpp index 2cb762a..71176fc 100644 --- a/single_include/toml.hpp +++ b/single_include/toml.hpp @@ -15063,7 +15063,7 @@ try_parse(std::istream& is, std::string fname = "unknown file", spec s = spec::d // read whole file as a sequence of char assert(fsize >= 0); - std::vector letters(static_cast(fsize)); + std::vector letters(static_cast(fsize), '\0'); is.read(reinterpret_cast(letters.data()), fsize); return detail::parse_impl(std::move(letters), std::move(fname), std::move(s)); @@ -15253,7 +15253,15 @@ try_parse(FILE* fp, std::string filename, spec s = spec::default_version()) // read whole file as a sequence of char assert(fsize >= 0); std::vector letters(static_cast(fsize)); - std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); + const auto actual = std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); + if(actual != static_cast(fsize)) + { + return err(std::vector{error_info( + std::string("File size changed: \"") + filename + + std::string("\" make sure that FILE* is in binary mode " + "to avoid LF <-> CRLF conversion"), {} + )}); + } return detail::parse_impl(std::move(letters), std::move(filename), std::move(s)); } @@ -15291,7 +15299,12 @@ parse(FILE* fp, std::string filename, spec s = spec::default_version()) // read whole file as a sequence of char assert(fsize >= 0); std::vector letters(static_cast(fsize)); - std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); + const auto actual = std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); + if(actual != static_cast(fsize)) + { + throw file_io_error(errno, "File size changed; make sure that " + "FILE* is in binary mode to avoid LF <-> CRLF conversion", filename); + } auto res = detail::parse_impl(std::move(letters), std::move(filename), std::move(s)); if(res.is_ok())