Bug 402708

Summary: Division by zero errors in investement reports
Product: [Applications] kmymoney Reporter: zoltan1980
Component: reportsAssignee: KMyMoney Devel Mailing List <kmymoney-devel>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: git (master)   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In: 5.0.3
Sentry Crash Report:

Description zoltan1980 2018-12-30 14:45:50 UTC
I get division by zero errors in three different places.

In investment performance report, triggered by having a negative investment balance at the start of the report period (rather unconventional, but useful under certain circumstances):

Thread 1 "kmymoney" received signal SIGFPE, Arithmetic exception.
0x00007fffeb28255a in __gmp_exception () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
(gdb) bt
#0  0x00007fffeb28255a in __gmp_exception () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#1  0x00007fffeb28258e in __gmp_divide_by_zero () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#2  0x00007fffeb29f425 in __gmpq_div () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#3  0x00007ffff163c6ab in AlkValue::operator/(AlkValue const&) const () from /usr/lib/x86_64-linux-gnu/libalkimia5.so.7
#4  0x00007ffff6198df5 in MyMoneyMoney::operator/ (this=0x7fffffffc520, _b=...) at ./kmymoney/mymoney/mymoneymoney.cpp:374
#5  0x00007fffc531ac20 in reports::QueryTable::helperROI (this=0x55555b1ee970, buys=..., sells=..., startingBal=..., endingBal=..., cashIncome=...)
    at ./kmymoney/plugins/views/reports/core/querytable.cpp:1061
#6  0x00007fffc531ebde in reports::QueryTable::constructPerformanceRow (this=0x55555b1ee970, account=..., result=..., all=...)
    at ./kmymoney/plugins/views/reports/core/querytable.cpp:1408
#7  0x00007fffc53209a6 in reports::QueryTable::constructAccountTable (this=0x55555b1ee970) at ./kmymoney/plugins/views/reports/core/querytable.cpp:1518
#8  0x00007fffc530fb83 in reports::QueryTable::init (this=0x55555b1ee970) at ./kmymoney/plugins/views/reports/core/querytable.cpp:91
#9  0x00007fffc530fab6 in reports::QueryTable::QueryTable (this=0x55555b1ee970, _report=...) at ./kmymoney/plugins/views/reports/core/querytable.cpp:77
#10 0x00007fffc52ac2ec in KReportTab::updateReport (this=0x55555b196f50) at ./kmymoney/plugins/views/reports/kreportsview_p.h:365
[...]

In capital gains report, triggered by add/remove shares transactions:

#0  0x00007fffeb28255a in __gmp_exception () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#1  0x00007fffeb28258e in __gmp_divide_by_zero () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#2  0x00007fffeb29f425 in __gmpq_div () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#3  0x00007ffff163c6ab in AlkValue::operator/(AlkValue const&) const () from /usr/lib/x86_64-linux-gnu/libalkimia5.so.7
#4  0x00007ffff6198df5 in MyMoneyMoney::operator/ (this=0x7fffffffc970, _b=...) at ./kmymoney/mymoney/mymoneymoney.cpp:374
#5  0x00007fffc531fa80 in reports::QueryTable::constructCapitalGainRow (this=0x55555b20e020, account=..., result=...)
    at ./kmymoney/plugins/views/reports/core/querytable.cpp:1459
#6  0x00007fffc5320f7b in reports::QueryTable::constructAccountTable (this=0x55555b20e020) at ./kmymoney/plugins/views/reports/core/querytable.cpp:1537
#7  0x00007fffc530fb83 in reports::QueryTable::init (this=0x55555b20e020) at ./kmymoney/plugins/views/reports/core/querytable.cpp:91
#8  0x00007fffc530fab6 in reports::QueryTable::QueryTable (this=0x55555b20e020, _report=...) at ./kmymoney/plugins/views/reports/core/querytable.cpp:77
#9  0x00007fffc52ac2ec in KReportTab::updateReport (this=0x55555b618bd0) at ./kmymoney/plugins/views/reports/kreportsview_p.h:365
[...]

Under the same conditions after fixing the one above:

#0  0x00007fffeb28255a in __gmp_exception () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#1  0x00007fffeb28258e in __gmp_divide_by_zero () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#2  0x00007fffeb29f425 in __gmpq_div () from /usr/lib/x86_64-linux-gnu/libgmp.so.10
#3  0x00007ffff163c6ab in AlkValue::operator/(AlkValue const&) const () from /usr/lib/x86_64-linux-gnu/libalkimia5.so.7
#4  0x00007ffff6198df5 in MyMoneyMoney::operator/ (this=0x7fffffffcaa0, _b=...) at ./kmymoney/mymoney/mymoneymoney.cpp:374
#5  0x00007fffc53120f0 in reports::QueryTable::constructTotalRows (this=0x55555b223c30) at ./kmymoney/plugins/views/reports/core/querytable.cpp:362
#6  0x00007fffc5310cce in reports::QueryTable::init (this=0x55555b223c30) at ./kmymoney/plugins/views/reports/core/querytable.cpp:269
#7  0x00007fffc530fb36 in reports::QueryTable::QueryTable (this=0x55555b223c30, _report=...) at ./kmymoney/plugins/views/reports/core/querytable.cpp:77
#8  0x00007fffc52ac36c in KReportTab::updateReport (this=0x55555b4f1500) at ./kmymoney/plugins/views/reports/kreportsview_p.h:365
[...]
Comment 1 zoltan1980 2018-12-30 14:50:57 UTC
I uploaded a fix for review. The instructions said the it will automatically get assigned, but it did not and complains that "This revision needs review, but there are no reviewers specified."

https://phabricator.kde.org/D17871
Comment 2 Jack 2018-12-30 16:19:03 UTC
I just added KMyMoney as reviewer in Phabricator.
Comment 3 Thomas Baumgart 2019-01-17 08:07:32 UTC
Git commit 741e848593162aa670c735b812b0574dbb483907 by Thomas Baumgart.
Committed on 17/01/2019 at 07:28.
Pushed by tbaumgart into branch '5.0'.

Fix division by zero errors in QueryTable

Summary:
Code used to try to divide by zero. Fixed it to always check for the
denominator being 0 beforehand.

Original patch by Zoltan Ivanfi
FIXED-IN: 5.0.3

Reviewers: #kmymoney, tbaumgart

Reviewed By: #kmymoney, tbaumgart

Subscribers: aacid, tbaumgart, ostroffjh

Differential Revision: https://phabricator.kde.org/D17871

M  +8    -5    kmymoney/plugins/views/reports/core/querytable.cpp
M  +28   -0    kmymoney/plugins/views/reports/core/tests/querytable-test.cpp
M  +1    -0    kmymoney/plugins/views/reports/core/tests/querytable-test.h

https://commits.kde.org/kmymoney/741e848593162aa670c735b812b0574dbb483907