[Issue 12582] New: Non-existant named capture groups cause runtime range violation or segmentation fault in regex

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 15 04:01:40 PDT 2014


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 at puremagic.com
          Reporter: yxcvbasdfgqwert02 at 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;
}

--


More information about the Digitalmars-d-bugs mailing list