Tcl Source Code

View Ticket
Login
Ticket UUID: 697b1bbfe3387b8f777e55d4aba9a4e20833e8ed
Title: Incorrect argument handling by [regexp]
Type: Bug Created on: 2022-09-18 11:30:05
Submitter: erikleunissen Assigned to: nobody
Subsystem: 18. Commands M-Z Severity: Minor
Priority: 5 Medium Last modified: 2026-10-11 07:39:52
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:
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
User Comments:
serhiy.storchaka added on 2026-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 added on 2026-10-11 07:39:52:

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