Prevent overflow.

LCD denom was equivalent to using rounding.denom, which is val.denom * 10.
If val.num was at the max # of digits that would overflow, so instead
fix val.denom and actually round the result; since we're adding
5/(val.denom * 10) to accomplish the rounding we want to truncate the result.
This commit is contained in:
John Ralls 2014-12-03 10:18:03 -08:00
parent 701d8030b8
commit b694df7202

View File

@ -1258,8 +1258,8 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info)
{ {
rounding.num = 5; /* Limit the denom to 10^13 ~= 2^44, leaving max at ~524288 */ rounding.num = 5; /* Limit the denom to 10^13 ~= 2^44, leaving max at ~524288 */
rounding.denom = pow(10, max_dp + 1); rounding.denom = pow(10, max_dp + 1);
val = gnc_numeric_add(val, rounding, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD); val = gnc_numeric_add(val, rounding, val.denom, GNC_HOW_RND_TRUNC);
/* Yes, rounding up can cause overflow. Check for it. */
if (gnc_numeric_check(val)) if (gnc_numeric_check(val))
{ {
PWARN ("Bad numeric from rounding: %s.", PWARN ("Bad numeric from rounding: %s.",