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

2021-09-18 Thread russhy via Digitalmars-d-learn
Double check in your dub.json file and see if you haven't changed 
your buildoptions



DEBUG:

it's caught: https://run.dlang.io/is/F8HkD8


RELEASE:

segfault as expected: https://run.dlang.io/is/oLU2M3


And make sure to use latest version of ldc


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

2021-09-17 Thread jfondren via Digitalmars-d-learn

On Friday, 17 September 2021 at 11:10:33 UTC, seany wrote:
Compile with `dub build --compiler=ldc2 `. this should enable 
array bound checking options.


By default, yes. run `dub -v build --compiler=ldc2` to see the 
exact commands that dub runs.



But should it not be caught by range error ?


Based on what you've said, yes it should.


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.e., a segfault. null (or 0x0 (or 0)) isn't part of the memory 
addresses your program is allowed to access, so memory protection 
prevents the attempt to access it.



What absolute rookie mistake am I committing?


From this it doesn't sound like you are committing one, but if 
you're wanting bounds checking to be a normal part of program 
logic, and not something that only ever happens due to a 
programmer's error, then I think you're cutting against the grain 
of the language, where


- bounds checking is easily removed from all but @safe functions 
with normal flags


- flags exist to remove it from @safe functions also

- the *Error family of exceptions, including RangeError are not 
intended to be catchable


- raising and immediately catching an exception like this is 
slower and more verbose than an explicit test.


Rather than returning an empty array on an error and expecting a 
caller to catch RangeError, you could throw a normal exception on 
error, and then you have tools like `std.exception.ifThrown` to 
make dealing with that exception nicer.


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

2021-09-17 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 17 September 2021 at 11:10:33 UTC, seany wrote:

I have now this function, as a private member in a Class :
} catch (RangeError er) {


I can't remember if you can catch an index OOB error but try 
`catch (Throwable er)` will work if it is catchable at all and 
you can figure out what kind of Error you have by printing its 
name.


 "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.


`Type[]` arrays in D are effectively struct {size_t length; Type* 
ptr; } under the hood. Your problem is the array has no elements 
which is why trying to extract a value outside an array bound is 
an irrecoverable error.


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?


with a null array of zero length `arr`, `arr[0]` with bounds 
check enabled will fail the bounds check before it tries to 
dereference the pointer. if you try `arr.ptr[0]` to bypass the 
bounds checking (which is a very bad idea!) you will then try to 
load from an invalid memory address and crash.