Strange char[] behaviour

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Tue Mar 21 05:35:44 PST 2006


Thomas Kuehne wrote:
> The code above isn't complete thus makes testing it a bit difficult.
> As a start, let's try to find out the real coverage numbers:
> 
> replace
> 
>>>  for (int i = 0; i < catenatedStrings.length; ++i) {
>>>      root.addSuffix(catenatedStrings[i..$]);
>>>  }
> 
> with something like
> 
>>>  writefln("LOOP_PRE");
>>>  for (int i = 0; writefln("LOOP_CHECK"), i < catenatedStrings.length; ++i, writefln("LOOP_INC")) {
>>>      writefln("LOOP");
>>>      root.addSuffix(catenatedStrings[i..$]);
>>>  }
>>>  writefln("LOOP_POST");
> 
> That isn't the kind of code I usually want to touch, but since the RT
> (and maybe the line numbers in the debug info) might be corrupted...

I'm really grateful that you want to help here. Ok, here are the results:

First I commented the original loop and copy-pasted it beneath the original:

/++
  for (int i = 0; i < catenatedStrings.length; ++i) {
      root.addSuffix(catenatedStrings[i..$]);
  }
++/

  for (int i = 0; i < catenatedStrings.length; ++i) {
      root.addSuffix(catenatedStrings[i..$]);
  }

Guess what, the runtime code segfaults! Then I removed one line from the
commented part:

/++
  for (int i = 0; i < catenatedStrings.length; ++i) {
      root.addSuffix(catenatedStrings[i..$]);
++/

  for (int i = 0; i < catenatedStrings.length; ++i) {
      root.addSuffix(catenatedStrings[i..$]);
  }

Still segfaults. Then yet another line:

/++
  for (int i = 0; i < catenatedStrings.length; ++i) {
++/

  for (int i = 0; i < catenatedStrings.length; ++i) {
      root.addSuffix(catenatedStrings[i..$]);
  }

Now it "works"! (But the coverage info is still wrong)

Then I tried the writefln-method:

<snip>
       |    public T[] searchLCS() {
      2|        root = new GenericNode();
       |
      2|        writefln("Pre loop");
    652|        for (int i = 0; writefln("Loop check"), i <
catenatedStrings.length; ++i, writefln("Loop inc")) {
    324|                root.addSuffix(catenatedStrings[i..$]);
    312|        }
      2|        writefln("Post loop");
    312|
   1438|        return root.searchLCS(sentinels);
       |    }
    312|
<snip>

I wasn't able to split the for-statement fully, but here is the best
non-segfaulting version:

<snip>
       |    public T[] searchLCS() {
      2|        root = new GenericNode();
       |
      2|        writefln("Pre loop");
    328|        for (int i = 0; writefln("Loop check"), i <
catenatedStrings.length;
    324|        ++i,
    334|        writefln("Loop inc")) {
    324|                root.addSuffix(catenatedStrings[i..$]);
    334|        }
   1550|        writefln("Post loop");
       |
    336|        return root.searchLCS(sentinels);
       |    }
<snip>

I haven't got much spare time to hunt this bug down, but I'll try to
create a "minimal" test case this week. Maybe you can check it out then?
There are at least two cases here, the one segfaults without code
coverage on (char[]-strings) and the other one pops out when coverage is
turned on. I've tested these with DMD 0.147, 0.148 & 0.149, but now I'm
going to upgrade to 0.150.

-- 
Jari-Matti



More information about the Digitalmars-d-bugs mailing list