www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dub segfault and range error handling

reply seany <seany uni-bonn.de> writes:
I create a new project with : `dub init myproj`.

Then I change the source/app.d file with this :



         `
         import std.stdio;
         import std.math;
         import std.stdio;
         import std.conv;
         import std.format;
         import std.math;
         import std.algorithm;
         import std.net.curl;
         import std.json;
         //import dlib.image;
         import std.path;
         import std.array;
         import std.net.curl;
         import core.stdc.stdlib;
         import std.datetime;
         import std.file;
         //import opmix.dup;
         import std.parallelism;
         import std.exception;
         import core.exception;




         void main() {

                 int[] a = new int[] (0);
                 try {
                         // writeln(a[1]);
                         test();
                 } catch ( RangeError re) {
                         writeln("wrong");
                 } catch (Throwable T){
                         writeln("something else");
                 }
         }

         void test() {


                 int [] b =  new int[0];
                 int c = b[4];
                 writeln(c);

         }

`

I compile with : `dub build -b release --compiler=ldc2`

The result executing the compiled binary 'myproj' is is ( whether 
`writeln (a[1])` is uncommented, or the  `test()` function is 
uncommented) some random number, usually negative with large 
absolute value, such as `-1894658200` .

If this is embedded in a class, then the result is segfault.

Is this expected behavior?

Thank you.
Sep 16 2021
next sibling parent seany <seany uni-bonn.de> writes:
On Thursday, 16 September 2021 at 20:49:28 UTC, seany wrote:
 I create a new project with : `dub init myproj`.

 Then I change the source/app.d file with this :

 [...]
PS :compiling with : `dub build -b release ` ( i.e. no ldc2) is a direct segfault of the code posted above. PREEMPT Mon Dec 21 22:00:46 UTC 2020 x86_64 GNU/Linux
Sep 16 2021
prev sibling parent jfondren <julian.fondren gmail.com> writes:
On Thursday, 16 September 2021 at 20:49:28 UTC, seany wrote:
 I compile with : `dub build -b release --compiler=ldc2`
 The result executing the compiled binary 'myproj' is is ( 
 whether `writeln (a[1])` is uncommented, or the  `test()` 
 function is uncommented) some random number, usually negative 
 with large absolute value, such as `-1894658200` .
 If this is embedded in a class, then the result is segfault.

 Is this expected behavior?
Yes. You have a -release build, "defaulting to disabled asserts/contracts/invariants, and bounds checks in safe functions only", and you have an array out of bounds in a system function, so your array out of bounds goes unchecked.
Sep 16 2021