Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Incorrect argument handling by [regexp]

Closed / Fixed697b1bbfe3387b8f777e55d4aba9a4e20833e8edCreated 4 years agoUpdated 14 days ago
Descriptionplain2022-09-18 11:30:05
Here are the results of six invocations of the "regexp" command.
They indicate incorrect argument handling.


% set str "a-z"
a-z
% regexp - $str; #1
bad option "-": must be -all, -about, -indices, -inline, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --
% regexp \- $str; #2
1
% regexp {-} $str; #3
bad option "-": must be -all, -about, -indices, -inline, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --
% regexp -\\ $str; #4
couldn't compile regular expression pattern: invalid escape \ sequence
% regexp \\- $str; #5
1
% set initstring "-"
-
% regexp $initstring $str; #6
1
%

This discussion thread at c.l.t. provides some clues:

    https://groups.google.com/g/comp.lang.tcl/c/pbqqj8tcIUk
Referenced by6
  • b40148c895Ticket [1902890fff] compiled regexp behaves differently status still Pending with 3 other changes ticket2026-11-18 18:27:58
  • b0610ccd6fFix [697b1bbfe3] and [1902890fff]: Incorrect argument handling by [regexp] check-in2026-11-18 18:21:59
  • 33b0599e53Fix [697b1bbfe3] and [1902890fff]: Incorrect argument handling by [regexp] check-in2026-11-18 17:39:07
  • 6e93204003Fix [697b1bbfe3] and [1902890fff]: Incorrect argument handling by [regexp] check-in2026-10-14 09:26:56
  • 0b36adaf16Fix [1902890]: compiled [regexp] did not treat an RE value starting with "-" as a switch, unlike the command. When the RE is not known at compile time and there is no --, check it at runtime and call [regexp] itself if it starts with "-". Also fixes [7e4d5a0a2b] and case #6 of [697b1bbfe3]. New tests regexpComp-25.1 to 25.7. check-in2026-10-11 07:35:29
  • a64804ce70Fix cases #2 and #4 of [697b1bbfe3]: the bytecode compiler for [regexp] checked for a leading "-" only in simple words, so patterns like \- or -\\ were compiled instead of being reported as bad options. Check the value of any word known at compile time, as for [regsub]. Incompatible for compiled code relying on it; for the next feature release. New tests regexpComp-24.12 to 24.15. check-in2026-10-10 21:10:10
serhiy.storchakawiki2026-10-10 21:12:12

Still the same in 8.6, 9.0 and 9.1. Cases #1, #3 and #5 are correct: an argument starting with "-" is taken as an option unless it follows --. #2 and #4 differ from the command only in compiled code: the bytecode compiler checked for a leading "-" only in words without backslashes, so \- and -\\ were compiled as patterns. #6 (a pattern in a variable) is the same problem as [1902890fff].

Proposed fix for #2 and #4 in branch regexp-compile-dash-pattern: the compiler checks the value of any word known at compile time, as for regsub, so both now give "bad option" as in #1. Tests regexpComp-24.12 to 24.15.

serhiy.storchaka Ticket [697b1bbfe3]: 2 changes [History diff]2026-10-10 21:12:29
serhiy.storchakawiki2026-10-11 07:39:52

Proposed fix for case #6 in branch regexp-compile-runtime-switch, see [1902890fff].

dkf Ticket [697b1bbfe3]: 3 changes [History diff]2026-11-08 20:53:27
jan.nijtmanswiki2026-11-18 17:42:21

Fixed in [trunk] and core-9-0-branch

Many thanks!

Would be nice to backport this to 8.6.19 as well.

serhiy.storchakawiki2026-11-18 18:27:58

Backported to core-8-6-branch: [b0610ccd6f]. In 8.6, regexp \- a-z reports "bad option" instead of 9.0's "ambiguous option", compiled and uncompiled alike, so regexpComp-24.12 expects that.

serhiy.storchaka Closed ticket [697b1bbfe3]. [History diff]2026-11-18 18:32:47
Log in (anonymous is fine) to add a comment.