www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - sort bug: Goes outside of limits of array

dmd v0.100, Fedora Core 1 Linux.

I was getting some segfaults while sorting arrays of real's.  So I 
implemented by own copy of the comparison function for real's and found 
that this eventually gets called where one of the things being compared 
is outside of the limits of the array.

I get assert failure on the 4th assertion.  Here's the call stack on 
that assert, as reported by gdb:



 #0  0x0804a423 in _d_assert ()
 #1  0x0804a369 in _assert_8sort_bug ()
 #2  0x0804a292 in _D7ti_real10TypeInfo_e7compareFPvPvZi ()
 #3  0x0804d9cc in _adSort ()
 #4  0x0804a32e in _Dmain ()
 #5  0x0804aa78 in main ()

Here's the code that I ran:
 import std.stdio;
 
 void *sort_min; void *sort_max;
 extern(C) int _D7ti_real10TypeInfo_e7compareFPvPvZi(void *p1,void *p2) {
   writef("min=%x max=%x p1=%x p2=%x\n", cast(uint)sort_min,
cast(uint)sort_max, cast(uint)p1, cast(uint)p2); fflush(null);
 
   assert(sort_min <= p1);
   assert(p1 <= sort_max);
   assert(sort_min <= p2);
   assert(p2 <= sort_max);
 
   int ret = cast(int)(*cast(real *)p1 - *cast(real *)p2);
   return ret;
 }
 
 void main() {
   while(1) {
     real[200] foo;
     sort_min = &foo[0]; sort_max = &foo[length-1];
     foo.sort;
     writef("\n");
   }
 }

Aug 23 2004