yet another segfault - array out of bound is not caught by try catch

seany seany at uni-bonn.de
Fri Sep 17 11:10:33 UTC 2021


I have now this function, as a private member in a Class :


         double calculate_lineLength( int i)  {
             field.rawData [] * rd;                   // ignore 
the details, this works;
             rd = cast (field.rawData [] *)  dataSet; // ignore 
the details, this works;

             auto l = this.allLines[i];               // this is 
defined as
                                                      // int [][] 
allLines =
                                                      //           
  new int [][] (0,0)
                                                      // in case 
of failure, this is empty
             double r = 0;
             try {
                 writeln("l ", l);                    // prints []
                 writeln("i ", i);                    // prints i, 
in this case, i is set to 0
                 writeln(l.length);                   // prints 0
                 writeln("l0 ", l[0]);                // segfault 
- i want it to be caught
                 write("will print");
                 writeln("rd", (*rd));
                 write("will not print...");
                 auto p0 = (*rd)[l[0]];
                 auto p1 = (*rd)[l[1]];
     	    r = calculate_geoDistance_vincenty(p0.lat,p1.lat, 
p0.lon, p1.lon);
             } catch (RangeError er) {
                 writeln("range error");
             }
                 return r;
         }


Compile with `dub build --compiler=ldc2 `. this should enable 
array bound checking options.


I am debugging with gdb :

`gdb ./myprogram`

Then, in gdb console :

`run arg1 arg2 `

Result is :

         91753
         91754
         91755
         91756
         [New Thread 0x7ffff7560640 (LWP 45344)]
         [New Thread 0x7fffeffff640 (LWP 45345)]
         [New Thread 0x7ffff6d5f640 (LWP 45346)]
         [New Thread 0x7ffff655e640 (LWP 45347)]
         [New Thread 0x7ffff5d5d640 (LWP 45348)]
         [New Thread 0x7ffff555c640 (LWP 45349)]
         [New Thread 0x7ffff4d5b640 (LWP 45350)]
         [New Thread 0x7fffef7fe640 (LWP 45351)]
         [New Thread 0x7fffeeffd640 (LWP 45352)]
         [New Thread 0x7fffee7fc640 (LWP 45353)]
         [New Thread 0x7fffedffb640 (LWP 45354)]
         [New Thread 0x7fffed7fa640 (LWP 45355)]
         [New Thread 0x7fffecff9640 (LWP 45356)]
         [New Thread 0x7fffbffff640 (LWP 45357)]
         [New Thread 0x7fffbf7fe640 (LWP 45358)]
         [New Thread 0x7fffbeffd640 (LWP 45359)]
         [New Thread 0x7fffbe7fc640 (LWP 45360)]
         [New Thread 0x7fffbdffb640 (LWP 45361)]
         [New Thread 0x7fffbd7fa640 (LWP 45362)]
         getting LINES done
         alllines: []
         l []
         i 0
         0
         Thread 1 "myprogram" received signal SIGSEGV, 
Segmentation fault.
         _D14analysisEngine9geoEngine20calculate_lineLengthMFiZd 
(this=<optimized out>, i=0) at source/analysisEngine.d:15526
         15526
         writeln("l0 ", l[0]);

So, to see what is going on, i use the command `bt`:


         #0  
_D14analysisEngine9geoEngine20calculate_lineLengthMFiZd 
(this=<optimized out>, i=0) at source/analysisEngine.d:15526
         #1  0x00005555555dba40 in 
_D14analysisEngine9geoEngine13add_turnLinesMFZv (this=<optimized 
out>) at source/analysisEngine.d:7387
         #2  0x00005555555e375a in 
_D14analysisEngine9geoEngine15analyze_tillageMFZv 
(this=0x7ffff756a000) at source/analysisEngine.d:5329
         #3  0x000055555560d082 in _Dmain (args=...) at 
source/AI.d:123


Okey, I know where to look for : it's the line asking for 
`writeln("l0 ", l[0]);`.


     But should it not be caught by range error ? If I do `print 
l`in gdb, i find :
         $1 = {length = 0, ptr = 0x0}

With `print l[0]` i get: `Attempt to take address of value not 
located in memory.`. I believe the array printing syntax is 
valid; see 
[here](https://phoenix.goucher.edu/~kelliher/cs23/gdb.html).

What absolute rookie mistake am I committing? What does it mean : 
"Attempt to take address of value not located in memory" ? I am 
not even calling / accessing a pointer. I am trying to extract a 
value outside an array bound.

I imagine they all have their addresses. But with the bound 
checking operation in place, would the bound error be triggered 
before the attempt to take unavailable address error has a chance 
to trigger?

Thank you.




More information about the Digitalmars-d-learn mailing list