From 7d643810163a944f8fec5c891b9053c12069b509 Mon Sep 17 00:00:00 2001 From: itchyny Date: Thu, 17 Aug 2023 15:56:57 +0900 Subject: [PATCH] Fix overflow on numeric comparison (#2849) Although #2839 fixed the overflow of exponent subtraction, there still is possibility of overflow in the `D2U` macro. This patch fixes the overflow in the `D2U` macro, and also truncates the maximum digits to `DEC_MAX_DIGITS`. --- src/jv.c | 4 +++- tests/jq.test | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/jv.c b/src/jv.c index b763272b..85541694 100644 --- a/src/jv.c +++ b/src/jv.c @@ -528,7 +528,9 @@ static decContext* tsd_dec_ctx_get(pthread_key_t *key) { if (key == &dec_ctx_key) { decContextDefault(ctx, DEC_INIT_BASE); - ctx->digits = INT32_MAX - (ctx->emax - ctx->emin - 1); + // make sure (Int)D2U(rhs->exponent-lhs->exponent) does not overflow + ctx->digits = MIN(DEC_MAX_DIGITS, + INT32_MAX - (DECDPUN - 1) - (ctx->emax - ctx->emin - 1)); ctx->traps = 0; /*no errors*/ } else if (key == &dec_ctx_dbl_key) diff --git a/tests/jq.test b/tests/jq.test index 1dcb2542..e35722fd 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -569,7 +569,7 @@ true true # #2825 -(1e999999999, 10e999999999) > (1e-1147483648, 0.1e-1147483648) +(1e999999999, 10e999999999) > (1e-1147483646, 0.1e-1147483646) null true true