summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2019-01-18 22:18:06 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2019-01-18 22:18:06 (GMT)
commitaa23371ad970074cb736062b7818877657b8243b (patch)
treecea6b362d1fc7f6315ccc14561204ab0e5ef465d
parent3c7e5cc3a228b2a86bc56bad4fc6961a21500fc0 (diff)
downloadtcl-aa23371ad970074cb736062b7818877657b8243b.zip
tcl-aa23371ad970074cb736062b7818877657b8243b.tar.gz
tcl-aa23371ad970074cb736062b7818877657b8243b.tar.bz2
Clamp output of TclIndexDecode() between -1 (TCL_INDEX_NONE) and INT_MAX. Use this to produce slightly better error-message for lsort -index
-rw-r--r--generic/tclCmdIL.c13
-rw-r--r--generic/tclUtil.c10
-rw-r--r--tests/cmdIL.test5
3 files changed, 21 insertions, 7 deletions
diff --git a/generic/tclCmdIL.c b/generic/tclCmdIL.c
index 34e9382..a1a7f3e 100644
--- a/generic/tclCmdIL.c
+++ b/generic/tclCmdIL.c
@@ -4720,9 +4720,16 @@ SelectObjFromSublist(
return NULL;
}
if (currentObj == NULL) {
- Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf(
- "element %d missing from sublist \"%s\"",
- index, TclGetString(objPtr)));
+ if (index == TCL_INDEX_NONE) {
+ index = TCL_INDEX_END - infoPtr->indexv[i];
+ Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf(
+ "element end-%d missing from sublist \"%s\"",
+ index, TclGetString(objPtr)));
+ } else {
+ Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf(
+ "element %d missing from sublist \"%s\"",
+ index, TclGetString(objPtr)));
+ }
Tcl_SetErrorCode(infoPtr->interp, "TCL", "OPERATION", "LSORT",
"INDEXFAILED", NULL);
infoPtr->resultCode = TCL_ERROR;
diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index a2670d5..04fc2af 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -4159,10 +4159,14 @@ TclIndexDecode(
int encoded, /* Value to decode */
int endValue) /* Meaning of "end" to use, > TCL_INDEX_END */
{
- if (encoded <= TCL_INDEX_END) {
- return (encoded - TCL_INDEX_END) + endValue;
+ if (encoded > TCL_INDEX_END) {
+ return encoded;
}
- return encoded;
+ endValue += encoded - TCL_INDEX_END;
+ if (endValue >= 0) {
+ return endValue;
+ }
+ return TCL_INDEX_NONE;
}
/*
diff --git a/tests/cmdIL.test b/tests/cmdIL.test
index 360d6b0..e4931a4 100644
--- a/tests/cmdIL.test
+++ b/tests/cmdIL.test
@@ -232,7 +232,7 @@ test cmdIL-3.5.3 {SortCompare procedure, -index option (out of range, calculated
} -returnCodes error -result {index "-2" cannot select an element from any list}
test cmdIL-3.5.4 {SortCompare procedure, -index option (out of range, calculated index)} -body {
lsort -index end-4 {{1 . c} {2 . b} {3 . a}}
-} -returnCodes error -result {element -2 missing from sublist "1 . c"}
+} -returnCodes error -result {element end-4 missing from sublist "1 . c"}
test cmdIL-3.5.5 {SortCompare procedure, -index option} {
lsort -index {} {a b}
} {a b}
@@ -248,6 +248,9 @@ test cmdIL-3.5.8 {SortCompare procedure, -index option (out of range, calculated
test cmdIL-3.5.9 {SortCompare procedure, -index option (out of range, calculated index)} -body {
lsort -index end+2 {{1 . c} {2 . b} {3 . a}}
} -returnCodes error -result {index "end+2" cannot select an element from any list}
+test cmdIL-3.5.10 {SortCompare procedure, -index option (out of range, calculated index)} -body {
+ lsort -index 0 {{}}
+} -returnCodes error -result {element 0 missing from sublist ""}
test cmdIL-3.6 {SortCompare procedure, -index option} {
lsort -integer -index 2 {{1 15 30} {2 5 25} {3 25 20}}
} {{3 25 20} {2 5 25} {1 15 30}}