Comment #0 by yxcvbasdfgqwert02 — 2014-04-15T11:01:40Z
Created attachment 1345
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;
}