www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16698] New: std.regex.matchFirst corrupts stack


          Issue ID: 16698
           Summary: std.regex.matchFirst corrupts stack
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: hsteoh quickfur.ath.cx

import std.datetime;
import std.regex;
import std.stdio;

Date[2] parseDateRange()
    auto rangeRe = regex(`^\s*(\d+-\d+-\d+)(?:\s+to\s+(\d+-\d+-\d+))?`);
    auto m = matchFirst("2016-01-01", rangeRe);

    return [Date(2016,1,1), Date(2016,1,1)];

int main(string[] args)
    auto range = parseDateRange();
    writefln("PPPPPParsed range=%s", range);
    return 0;

This is reduced from a larger program.  The symptom is that std.datetime dies
with an array range assertion.  But the real problem is that somewhere between
parseDateRange() and main(), the Date[2] return value gets corrupted to garbage
values, so std.datetime gets confused by a Date object that is in an invalid
state.  Scarily enough, this bug is only detected when I attempt to print the
Date object; in the original code, it was causing silent failures of date
checks because the corrupted Date objects return nonsensical results when used
in Date comparisons.

Commenting out the call to matchFirst() makes the problem go away. Note that
this reduced code actually doesn't do anything with the match results; the very
fact that matchFirst() was called triggers the corruption bug.  In the original
code the return value is, of course, built from the regex match results, but
the bug seems to happen even if we don't touch the match results.

Nov 16 2016