www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12582] New: Non-existant named capture groups cause runtime

https://issues.dlang.org/show_bug.cgi?id=12582

          Issue ID: 12582
           Summary: Non-existant named capture groups cause runtime range
                    violation or segmentation fault in regex
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: yxcvbasdfgqwert02 gmx.de

Created attachment 1345
  --> https://issues.dlang.org/attachment.cgi?id=1345&action=edit
Source code for test case

Trying to get a named capture group which doesn't exist in a regular expression
causes a segmentation fault (with -release) or a Range Violation (without
-release).

Since a segmentation fault can't be catched (at least not with Linux 64 bit),
the program reliably crashes. 

Fix: Throw a descriptive exception instead.

In this simple test program, the regular expression could be fixed to avoid the
problem. In my complex real-world program, regular expressions are given by the
user (configuration file). The program must not crash but has to display an
error message containing the regular expression to fix.

In dmd 2.065, the SEGV / range violation is caused in regex.d, line 2285:

 trusted uint lookupNamedGroup(String)(NamedGroup[] dict, String name)
{//equal is  system?
    auto fnd = assumeSorted!"cmp(a,b) <
0"(map!"a.name"(dict)).lowerBound(name).length;
    // fnt might be invalid if capture group doesn't exist.
    // fix: throw an exception if so
    enforce(equal(dict[fnd].name, name), text("no submatch named ", name));
    return dict[fnd].group;
}

--
Apr 15 2014