diff options
author | Jay Berkenbilt <ejb@ql.org> | 2020-01-14 17:04:55 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2020-01-14 17:07:09 +0100 |
commit | ab4061f1ee4e71a586f60bd65b9be4c96bf0bed8 (patch) | |
tree | 01cae18b9bccaa005b445853c5d32e857f53abb0 /libqpdf/QUtil.cc | |
parent | 211a7f57be1913a32239b9868c85f18cd6b28683 (diff) | |
download | qpdf-ab4061f1ee4e71a586f60bd65b9be4c96bf0bed8.tar.zst |
Add error detection for read_lines_from_file(FILE*)
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r-- | libqpdf/QUtil.cc | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 70066b32..63adbed2 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -1043,6 +1043,20 @@ QUtil::read_file_into_memory( fclose(f); } +static bool read_char_from_FILE(char& ch, FILE* f) +{ + auto len = fread(&ch, 1, 1, f); + if (len == 0) + { + if (ferror(f)) + { + throw std::runtime_error("failure reading character from file"); + } + return false; + } + return true; +} + std::list<std::string> QUtil::read_lines_from_file(char const* filename) { @@ -1050,7 +1064,7 @@ QUtil::read_lines_from_file(char const* filename) std::list<std::string> lines; FILE* f = safe_fopen(filename, "rb"); FileCloser fc(f); - auto next_char = [&f](char& ch) { return (fread(&ch, 1, 1, f) > 0); }; + auto next_char = [&f](char& ch) { return read_char_from_FILE(ch, f); }; read_lines_from_file(next_char, lines, false); return lines; } @@ -1061,7 +1075,7 @@ QUtil::read_lines_from_file(char const* filename, bool preserve_eol) std::list<std::string> lines; FILE* f = safe_fopen(filename, "rb"); FileCloser fc(f); - auto next_char = [&f](char& ch) { return (fread(&ch, 1, 1, f) > 0); }; + auto next_char = [&f](char& ch) { return read_char_from_FILE(ch, f); }; read_lines_from_file(next_char, lines, preserve_eol); return lines; } @@ -1089,7 +1103,7 @@ std::list<std::string> QUtil::read_lines_from_file(FILE* f, bool preserve_eol) { std::list<std::string> lines; - auto next_char = [&f](char& ch) { return (fread(&ch, 1, 1, f) > 0); }; + auto next_char = [&f](char& ch) { return read_char_from_FILE(ch, f); }; read_lines_from_file(next_char, lines, preserve_eol); return lines; } |