Bug 788 - Assertion failed with unbalanced remainder and input from stdin
Summary: Assertion failed with unbalanced remainder and input from stdin
Status: RESOLVED FIXED
Alias: None
Product: Ledger
Classification: Unclassified
Component: expr (show other bugs)
Version: 3.0.0
Hardware: All All
: P1 normal
Assignee: John Wiegley
URL:
Depends on:
Blocks:
 
Reported: 2012-08-23 09:21 UTC by Martin Michlmayr
Modified: 2016-01-22 17:06 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Michlmayr 2012-08-23 09:21:53 UTC
I get the following assertion:

cat b | ledger -f - bal
While parsing file "", line 4:
Error: Assertion failed in "/home/tbm/ledger/src/error.cc", line 100: ledger::string ledger::source_context(const path&, istream_pos_type, istream_pos_type, const string&): in.gcount() == static_cast<std::streamsize>(len)

This only happens what input comes from stdin.  When I specify the actual file with -f, I get the correct error:

ledger -f b bal
While parsing file "/home/tbm/personal/finance/b", line 4:
While balancing transaction from "/home/tbm/personal/finance/b", lines 2-4:
> 2012-08-21 * Test
>     A          10.00 GBP
>     B          -8.00 GBP
Unbalanced remainder is:
            2.00 GBP
Amount to balance against:
           10.00 GBP
Error: Transaction does not balance

This is a regression which was introduced some time between 2011-11-26 and 2012-03-10.
Comment 9 Johann Klähn 2013-01-17 05:26:45 UTC
I was not yet able to reproduce your problem using the old patch.
Nevertheless I rewrote it somewhat, fixing a seperate bug (include directive with relative paths for -f /dev/stdin) in the process.
Can you confirm that the code in this pull request works for your case?
https://github.com/ledger/ledger/pull/123
Comment 8 John Wiegley 2013-01-17 02:20:53 UTC
The fundamental command that gets run is:

  ledger -f - --base -p "this month" bal
Comment 7 Johann Klähn 2013-01-17 01:34:44 UTC
Can you share the hours alias/script so I can write a regression test for this? I'm unable to locate it inside the source tree.
Comment 6 John Wiegley 2013-01-16 18:14:09 UTC
I had to revert this change because it breaks my only use of Ledger currently!  I get this:

    vulcan:~/src/ledger $ hours
    Error: boost::filesystem::file_size: Operation not permitted: "/dev/stdin"
Comment 4 Johann Klähn 2013-01-08 16:11:12 UTC
It seems like `source_context` expects the file path to be set to "/dev/stdin" when ledger reads from stdin but currently an empty string is passed (as that is what `context.pathname` is set to in the calling functions) when using '-f -' instead of '-f /dev/stdin'. Ledger then tries to open that file and check its length against the input stream. Obviously this won't do.

Several solutions:
a) `source_context` could check for "" too and handle it the same way as "/dev/stdin". Optional: Check/Assert that file exists → Cleaner assertion message for those cases
b) `session_t::read_data` or `parse_context_stack_t::push(shared_ptr<std::istream> stream, …)` could be adjusted to set the pathname to "/dev/stdin" (`session_t::read_data` already has custom logic for '-f -')

Grepping the source code, "/dev/stdin" is also used in `item_context`, so this has to be adjusted too, if option a is used.

I think I will go with option a as an empty file name doesn't provide a source context either.
Comment 3 Martin Michlmayr 2012-10-23 05:59:40 UTC
Adding Johann since this might relate to #735.
Comment 2 Martin Michlmayr 2012-10-23 05:58:35 UTC
Here's another way to reproduce this:

$ cat a
amount,
10,

$ cat a | ledger convert -f -
While parsing file "", line 2:
While parsing transaction:
Error: Assertion failed in "/home/tbm/ledger/src/error.cc", line 100: ledger::string ledger::source_context(const path&, istream_pos_type, istream_pos_type, const string&): in.gcount() == static_cast<std::streamsize>(len)
Comment 1 Martin Michlmayr 2012-10-03 12:11:55 UTC
Setting to P1 since this is imho an issue that should be fixed in 3.0.

Reason: it's a regression and a big usability issue.