From be30aac3f5f74ffe308db983b372c2b6ddfcfde5 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 22 May 2010 00:45:35 +0000 Subject: [PATCH] fix an occasional stack overflow error due to infinite recursion in Posting/Transaction equality tests This was happening with balance command on certain timelog entries, since 0.8. Now, when testing two postings for equality, their parent transaction's identity is ignored. --- hledger-lib/Hledger/Data/Types.hs | 9 +++++++-- tests/timelog-stack-overflow.test | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/timelog-stack-overflow.test diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 45a907a17..9fe31abbb 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -81,7 +81,12 @@ data Posting = Posting { ptype :: PostingType, ptransaction :: Maybe Transaction -- ^ this posting's parent transaction (co-recursive types). -- Tying this knot gets tedious, Maybe makes it easier/optional. - } deriving (Eq) + } + +-- The equality test for postings ignores the parent transaction's +-- identity, to avoid infinite loops. +instance Eq Posting where + (==) (Posting a1 b1 c1 d1 e1 _) (Posting a2 b2 c2 d2 e2 _) = a1==a2 && b1==b2 && c1==c2 && d1==d2 && e1==e2 data Transaction = Transaction { tdate :: Day, @@ -90,7 +95,7 @@ data Transaction = Transaction { tcode :: String, tdescription :: String, tcomment :: String, - tpostings :: [Posting], + tpostings :: [Posting], -- ^ this transaction's postings (co-recursive types). tpreceding_comment_lines :: String } deriving (Eq) diff --git a/tests/timelog-stack-overflow.test b/tests/timelog-stack-overflow.test new file mode 100644 index 000000000..e30892bfd --- /dev/null +++ b/tests/timelog-stack-overflow.test @@ -0,0 +1,13 @@ +# this gave a stack space overflow error with 0.8-0.9 due to infinite +# recursion in Posting and Transaction's equality tests: +bin/hledger -f - balance +<<< +i 2010/1/1 09:00:00 a:b +o 2010/1/1 09:03:00 +>>>=0 +# incidentally this didn't trigger it.. go figure +bin/hledger -f - balance +<<< +i 2010/1/1 09:00:00 a:b +o 2010/1/1 09:02:00 +>>>=0