From 28d58d74b0e3837b5ca9c2020bd14cf21ee98a41 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 17 Feb 2021 16:16:19 +0000 Subject: Fix abs(-9223372036854775808) special-case on platforms where sizeof(Tcl_WideInt) > sizeof(int64_t). Theoretical, yes, but at least add a testcase for this (expr-38.14) --- generic/tclBasic.c | 11 ++++++++++- tests/expr.test | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/generic/tclBasic.c b/generic/tclBasic.c index 2ed4270..b2d3f28 100644 --- a/generic/tclBasic.c +++ b/generic/tclBasic.c @@ -7906,7 +7906,16 @@ ExprAbsFunc( } goto unChanged; } else if (l == WIDE_MIN) { - if (mp_init_i64(&big, l) != MP_OKAY) { + if (sizeof(Tcl_WideInt) > sizeof(int64_t)) { + Tcl_WideUInt ul = -(Tcl_WideUInt)WIDE_MIN; + if (mp_init(&big) != MP_OKAY || mp_unpack(&big, 1, 1, + sizeof(Tcl_WideInt), 0, 0, &ul) != MP_OKAY) { + return TCL_ERROR; + } + if (mp_neg(&big, &big) != MP_OKAY) { + return TCL_ERROR; + } + } else if (mp_init_i64(&big, l) != MP_OKAY) { return TCL_ERROR; } goto tooLarge; diff --git a/tests/expr.test b/tests/expr.test index 9add1f1..5435a18 100644 --- a/tests/expr.test +++ b/tests/expr.test @@ -6699,6 +6699,9 @@ test expr-38.12 {abs and -0x0 [Bug 2954959]} { test expr-38.13 {abs and 0.0 [Bug 2954959]} { ::tcl::mathfunc::abs 1e-324 } 1e-324 +test expr-38.14 {abs and WIDE_MIN special-case} { + ::tcl::mathfunc::abs -9223372036854775808 +} 9223372036854775808 testConstraint testexprlongobj [llength [info commands testexprlongobj]] testConstraint testexprdoubleobj [llength [info commands testexprdoubleobj]] -- cgit v0.12