aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2020-01-14 17:04:55 +0100
committerJay Berkenbilt <ejb@ql.org>2020-01-14 17:07:09 +0100
commitab4061f1ee4e71a586f60bd65b9be4c96bf0bed8 (patch)
tree01cae18b9bccaa005b445853c5d32e857f53abb0 /libqpdf/QUtil.cc
parent211a7f57be1913a32239b9868c85f18cd6b28683 (diff)
downloadqpdf-ab4061f1ee4e71a586f60bd65b9be4c96bf0bed8.tar.zst
Add error detection for read_lines_from_file(FILE*)
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc20
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;
}