ref param mismatch --> segfault

spir denis.spir at gmail.com
Mon Jan 24 14:21:57 PST 2011


On 01/24/2011 10:07 PM, bearophile wrote:
> spir:
>
>> Certainly ref qualifiers should be checked,
>> shouldn't they? The situation indeed is somewhat special here.
>>
>> Foo getFoo(Token[] tokens, ref uint index)
>> {...}
>> Term[] getTerms (Term) (Term function (Token[], uint) parseFunc, Token[] tokens)
>> {...}
>> void main()
>> {
>>       Foo[] foos = getTerms!(Foo)(&getFoo, s);	# ==>  segfault
>> }
>
> Please, create a very small but complete program that shows the problem :-) It will go to Bugzilla.
>
> Bye,
> bearophile

I guess this does the job? (replaced token stream with plain stream, for the 
example, thus it's really artificial since getFloat just does what parse!float 
would do, but well... shows the bug).

float getFloat(string s, ref uint index) {
     if (index >= s.length)
         return -1;

     float fl;
     string[] ss = s[index..$].split();
     string s0 = ss[0];

     try
         fl = to!float(s0);
     catch (Exception _)
         return -1;
     index += s0.length;
     return fl;
}

Term[] terms (Term) (Term function (string, uint) parseFunc, string s) {
     Term[] terms = [];
     Term term;
     uint index = 0;
     static enum SEP = ' ';

     term = parseFunc(s, index);
     if (term != -1) {
         terms ~= term;
         while (true) {
             if (index >= s.length || s[index] != SEP)
                 break;
             ++ index;
             term = parseFunc(s, index);
             if (term == -1)
                 break;
             terms ~= term;
         }
     }

     return terms;
}

unittest {
     string s = "3.33 1.0 2.2";
     float[] floats = terms!(float)(&getFloat, s);
     writeln(floats);
}

If you replace "uint" by "ref uint" in terms' signature, works fine.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list