summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAustin Wise <AustinWise@gmail.com>2021-07-14 01:03:30 -0700
committerMartin Michlmayr <tbm@cyrius.com>2021-08-24 16:32:10 +0800
commite712ce4e7cb6aac7469682cc05079707cbfb759b (patch)
tree5e9aafce7c776f8880b5c4229bf2727bfa2353ff
parent71c0903dffeb8e6750abdfba3c2e6e99f38f146c (diff)
downloadfork-ledger-e712ce4e7cb6aac7469682cc05079707cbfb759b.tar.gz
fork-ledger-e712ce4e7cb6aac7469682cc05079707cbfb759b.tar.bz2
fork-ledger-e712ce4e7cb6aac7469682cc05079707cbfb759b.zip
Fix silent errors when reading lines from input files.
Handle files that don't end with a new line. Throw an error when the buffer size is exceeded. Fixes #516 Contributes to #1149
-rw-r--r--src/textual.cc14
-rw-r--r--test/input/parsing.dat6
-rw-r--r--test/regress/516.test4
3 files changed, 22 insertions, 2 deletions
diff --git a/src/textual.cc b/src/textual.cc
index b8523dbe..3b6ef5f4 100644
--- a/src/textual.cc
+++ b/src/textual.cc
@@ -311,9 +311,14 @@ std::streamsize instance_t::read_line(char *& line)
check_for_signal();
- in.getline(context.linebuf, parse_context_t::MAX_LINE);
+ const size_t maxLine = parse_context_t::MAX_LINE;
+ in.getline(context.linebuf, maxLine);
std::streamsize len = in.gcount();
+ if (in.fail() && len == (parse_context_t::MAX_LINE - 1)) {
+ throw_(parse_error, _f("Line exceeds %1% characters") % maxLine);
+ }
+
if (len > 0) {
context.linenum++;
@@ -329,7 +334,12 @@ std::streamsize instance_t::read_line(char *& line)
line = context.linebuf;
}
- --len;
+ if (!in.eof()) {
+ // if we are not at the end of the file, len includes the new line character,
+ // even through it does not appear in linebuf
+ --len;
+ }
+
while (len > 0 && std::isspace(line[len - 1])) // strip trailing whitespace
line[--len] = '\0';
diff --git a/test/input/parsing.dat b/test/input/parsing.dat
new file mode 100644
index 00000000..3e10804b
--- /dev/null
+++ b/test/input/parsing.dat
@@ -0,0 +1,6 @@
+
+;NOTE: this file should NOT end in a new line
+;See https://github.com/ledger/ledger/issues/516
+2021/07/14 test
+ Assets $30
+ Income -$30 \ No newline at end of file
diff --git a/test/regress/516.test b/test/regress/516.test
new file mode 100644
index 00000000..538b5cb3
--- /dev/null
+++ b/test/regress/516.test
@@ -0,0 +1,4 @@
+test cleared --file test/input/parsing.dat --cleared-format "%-30(account) %15(get_at(total_expr, 0)) %15(get_at(total_expr, 1))\n%/"
+Assets $30 0
+Income $-30 0
+end test