From 4590e2d12033ebf1ad82a2b46da552f0638b763a Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Tue, 20 Sep 2022 20:42:30 +0000 Subject: Proposed fix for [c0bc269178], with testcase --- generic/tclCmdMZ.c | 8 ++++++-- tests/switch.test | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/generic/tclCmdMZ.c b/generic/tclCmdMZ.c index 3ff9947..53e12c5 100644 --- a/generic/tclCmdMZ.c +++ b/generic/tclCmdMZ.c @@ -3930,8 +3930,12 @@ TclNRSwitchObjCmd( if (matchVarObj != NULL) { Tcl_Obj *substringObj; - substringObj = Tcl_GetRange(stringObj, - info.matches[j].start, info.matches[j].end-1); + if (info.matches[j].end > 0) { + substringObj = Tcl_GetRange(stringObj, + info.matches[j].start, info.matches[j].end-1); + } else { + TclNewObj(substringObj); + } /* * Never fails; the object is always clean at this point. diff --git a/tests/switch.test b/tests/switch.test index 8ca049c..a7211cb 100644 --- a/tests/switch.test +++ b/tests/switch.test @@ -745,6 +745,13 @@ test switch-14.16 {switch -regexp compilation} { } }} } no +test switch-14.17 {switch -regexp bug [c0bc269178]} { + set result {} + switch -regexp -matchvar m -indexvar i ac { + {(a)(b)?(c)} {set result $m} + } + set result +} {ac a {} c} test switch-15.1 {coroutine safety of non-bytecoded switch} {*}{ -body { -- cgit v0.12