From 50ae8fdcf6340dd85f832fb31b363e767f9430f8 Mon Sep 17 00:00:00 2001 From: dgp Date: Fri, 19 Oct 2018 19:18:30 +0000 Subject: [d1d05d1c7a] Tests and fix for bignums in end-offset index value parsing. --- generic/tclUtil.c | 46 ++++++++++++++++++++++++---------------------- tests/util.test | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/generic/tclUtil.c b/generic/tclUtil.c index 4ea9c2e..d3136e2 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -3660,10 +3660,12 @@ TclFormatInt( * GetWideForIndex -- * * This function produces a wide integer value corresponding to the - * list index held in *objPtr. The parsing supports all values + * index value held in *objPtr. The parsing supports all values * recognized as any size of integer, and the syntaxes end[-+]$integer * and $integer[-+]$integer. The argument endValue is used to give - * the meaning of the literal index value "end". + * the meaning of the literal index value "end". Index arithmetic + * on arguments outside the wide integer range are only accepted + * when interp is a working interpreter, not NULL. * * Results: * When parsing of *objPtr successfully recognizes an index value, @@ -3994,6 +3996,8 @@ SetEndOffsetFromAny( bytes = TclGetStringFromObj(objPtr, &length); if ((*bytes != 'e') || (strncmp(bytes, "end", (size_t)((length > 3) ? 3 : length)) != 0)) { + + badIndexFormat: if (interp != NULL) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( "bad index \"%s\": must be end?[+-]integer?", bytes)); @@ -4013,35 +4017,33 @@ SetEndOffsetFromAny( * This is our limited string expression evaluator. Pass everything * after "end-" to TclParseNumber. */ + ClientData cd; + int t; if (TclIsSpaceProc(bytes[4])) { goto badIndexFormat; } if (TclParseNumber(NULL, objPtr, NULL, bytes+4, length-4, NULL, TCL_PARSE_INTEGER_ONLY) != TCL_OK) { - return TCL_ERROR; - } - if (objPtr->typePtr != &tclIntType) { - goto badIndexFormat; + goto badIndexFormat; } - offset = objPtr->internalRep.wideValue; - if (bytes[3] == '-') { - - /* TODO: Review overflow concerns here! */ - offset = -offset; + TclGetNumberFromObj(NULL, objPtr, &cd, &t); + if (t == TCL_NUMBER_BIG) { + /* Truncate to the signed wide range. */ + if (mp_isneg((mp_int *)cd)) { + offset = (bytes[3] == '-') ? LLONG_MAX : LLONG_MIN; + } else { + offset = (bytes[3] == '-') ? LLONG_MIN : LLONG_MAX; + } + } else { + /* assert (t == TCL_NUMBER_INT); */ + offset = (*(Tcl_WideInt *)cd); + if (bytes[3] == '-') { + offset = (offset == LLONG_MIN) ? LLONG_MAX : -offset; + } } } else { - /* - * Conversion failed. Report the error. - */ - - badIndexFormat: - if (interp != NULL) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "bad index \"%s\": must be end?[+-]integer?", bytes)); - Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", NULL); - } - return TCL_ERROR; + goto badIndexFormat; } /* diff --git a/tests/util.test b/tests/util.test index 34113c0..9422fc3 100644 --- a/tests/util.test +++ b/tests/util.test @@ -735,6 +735,43 @@ test util-9.45 {TclGetIntForIndex} { test util-9.46 {TclGetIntForIndex} { string index abcd end+4294967294 } {} +# TIP 502 +test util-9.47 {TclGetIntForIndex} { + string index abcd 0x10000000000000000 +} {} +test util-9.48 {TclGetIntForIndex} { + string index abcd -0x10000000000000000 +} {} +test util-9.49 {TclGetIntForIndex} -body { + string index abcd end*1 +} -returnCodes error -match glob -result * +test util-9.50 {TclGetIntForIndex} -body { + string index abcd {end- 1} +} -returnCodes error -match glob -result * +test util-9.51 {TclGetIntForIndex} -body { + string index abcd end-end +} -returnCodes error -match glob -result * +test util-9.52 {TclGetIntForIndex} -body { + string index abcd end-x +} -returnCodes error -match glob -result * +test util-9.53 {TclGetIntForIndex} -body { + string index abcd end-0.1 +} -returnCodes error -match glob -result * +test util-9.54 {TclGetIntForIndex} { + string index abcd end-0x10000000000000000 +} {} +test util-9.55 {TclGetIntForIndex} { + string index abcd end+0x10000000000000000 +} {} +test util-9.56 {TclGetIntForIndex} { + string index abcd end--0x10000000000000000 +} {} +test util-9.57 {TclGetIntForIndex} { + string index abcd end+-0x10000000000000000 +} {} +test util-9.58 {TclGetIntForIndex} { + string index abcd end--0x8000000000000000 +} {} test util-10.1 {Tcl_PrintDouble - rounding} {ieeeFloatingPoint} { convertDouble 0x0000000000000000 -- cgit v0.12