diff options
author | Steve Bennett <steveb@workware.net.au> | 2020-07-12 06:40:36 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2020-07-12 06:48:43 +1000 |
commit | da293a1eef2ddd709f10b63744032302ca3597d3 (patch) | |
tree | 6a51da2ea65e95cd101362b6fc4713a5b8d5b16f | |
parent | 4e2cdacaad9473324807009b125907c76a7c7569 (diff) | |
download | jimtcl-da293a1eef2ddd709f10b63744032302ca3597d3.zip jimtcl-da293a1eef2ddd709f10b63744032302ca3597d3.tar.gz jimtcl-da293a1eef2ddd709f10b63744032302ca3597d3.tar.bz2 |
string last: fix segfault with invalid index
[string last foo bar -1] gave segfault due to missing
check for invalid index.
Fixes #161
Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r-- | jim.c | 9 | ||||
-rw-r--r-- | regtest.tcl | 5 | ||||
-rw-r--r-- | tests/string.test | 3 |
3 files changed, 13 insertions, 4 deletions
@@ -2590,7 +2590,7 @@ int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *seco * is out of range. */ static int JimRelToAbsIndex(int len, int idx) { - if (idx < 0) + if (idx < 0 && idx > -INT_MAX) return len + idx; return idx; } @@ -13979,9 +13979,7 @@ badcompareargs: } str = Jim_String(argv[2]); len = Jim_Utf8Length(interp, argv[2]); - if (idx != INT_MIN && idx != INT_MAX) { - idx = JimRelToAbsIndex(len, idx); - } + idx = JimRelToAbsIndex(len, idx); if (idx < 0 || idx >= len || str == NULL) { Jim_SetResultString(interp, "", 0); } @@ -14015,6 +14013,9 @@ badcompareargs: return JIM_ERR; } idx = JimRelToAbsIndex(l2, idx); + if (idx < 0) { + idx = 0; + } } else if (option == OPT_LAST) { idx = l2; diff --git a/regtest.tcl b/regtest.tcl index 14ce59f..5b7249c 100644 --- a/regtest.tcl +++ b/regtest.tcl @@ -355,6 +355,11 @@ puts "TEST 51 PASSED" catch {lsearch -all -command abc def} puts "TEST 52 PASSED" +# REGTEST 53 +# string last with invalid index +catch {string last foo bar -1} +puts "TEST 53 PASSED" + # TAKE THE FOLLOWING puts AS LAST LINE diff --git a/tests/string.test b/tests/string.test index 5a22229..3624565 100644 --- a/tests/string.test +++ b/tests/string.test @@ -449,6 +449,9 @@ test string-7.5 {string last} { test string-7.6 {string last} { string las x xxxx123xx345x678 } 12 +test string-7.7 {string last, bad index} { + string last ba badbad -1 +} -1 test string-7.13 {string last, start index} { ## Constrain to last 'a' should work string last ba badbad end-1 |