Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1476 (closed defect: fixed)

Opened 11 years ago

Last modified 11 years ago

tango.text.Regex depends on undefined behaviour, breaks on debug runtime

Reported by: Deewiant Assigned to: jascha
Priority: critical Milestone: 0.99.8
Component: Tango Version: trunk
Keywords: Cc:

Description

tango.text.Regex currently breaks as described in D issue 2632 if running on a debug runtime. Since it doesn't seem like a quick answer to that is forthcoming, I request that the code in Tango be fixed in the meanwhile.

The issue is this union in struct Predicate:

union {
    char_t      data_chr;
    ubyte[]     data_bmp;
    string_t    data_str;
};

data_bmp and data_str are arrays, and using data_chr first can lead to a situation where this code, a few lines down from the above, blows up:

// setup bitmap
data_bmp.length = MAX_BITMAP_LENGTH/8;

If data_chr is set first, data_bmp and data_str have null pointers but nonzero lengths: setting the array length at this time causes an assertion failure in the runtime.

Simple example code which works fine on release runtimes but blows up on debug runtimes:

import tango.text.Regex;
int main() {
   return Regex("^").test("");
} 

Removing data_chr from the union should be enough to fix it.

Alternatively, set data_chr to zero before setting data_bmp.length, but that would result in extra allocations if data_bmp.length needs to be set twice in a row without setting data_chr in between.

Change History

02/18/09 21:10:00 changed by Deewiant

  • status changed from new to closed.
  • resolution set to fixed.

(In [4324]) Fix #1476 for now, at least until D issue 2632 is resolved.

The fix was simply to move data_chr out of the union in Predicate. Also added a comment about safe usage of the union, both regarding this issue and otherwise.