| |
| Posted by Jonathan M Davis in reply to mw | PermalinkReply |
|
Jonathan M Davis
| On Friday, July 21, 2023 3:27:45 PM MDT mw via Digitalmars-d-learn wrote:
> Hi,
>
> I have thought array index out of bound always throw exceptions.
>
> However, I just debugged a case, where out of bound array index didn't throw exception, and just hang the thread, which is much harder to debug (than exception which tells the exact error and source line location).
>
> So my question: array index out of bound may not throw exception in D?
>
> I tried DMD and LDC, both have this problem.
>
> Is there any flag I can pass to the compiler to let it always throw exception?
>
> Thanks.
Well, strictly speaking, it throws a RangeError, which is an Error, not an Exception (they both derive from Throwable). And it's not guaranteed that stuff like scope statements, and catches, and the like get run with Errors. Errors are basically supposed to kill your program rather than be recoverable (though they should normally still be printed out by the runtime like with an Exception). However, it at least used to be the case that all of the stack unwinding was in place for them even if it's not guaranteed by the language. So, I don't know what exactly happens right now if a RangeError gets thrown in a separate thread. It would not entirely surprise me if having the thread hang is normal at the moment, though ideally, what should be happening is that the program as a whole would be killed after printing out the RangeError. Either way, threads certainly complicate the matter.
However, ignoring the issue of it happening on a separate thread, whether bounds checking occurs in a piece of code depends on a variety of factors - a key one being whether the code in question is @safe or not.
If no compiler flags are used, then you should be getting bounds checking in all code.
If -release is used, then you should be getting bounds checking in @safe code, but it will not happen in @system or @trusted code. So, if you're not marking code with @safe, and it's not in templated or auto functions where @safe is inferred, then you won't be getting bounds checking in that code if you use -release.
If you use -boundscheck=on, then you should be getting bounds checking in all code.
If you use -boundscheck=safeonly, then you should be getting bounds checking in @safe code (but only @safe code) like with -release.
If you use -boundscheck=off, then you shouldn't be getting bounds checking anywhere.
So, if I had to guess, you did something like use -release with code that isn't @safe, and so bounds checking was turned off, but it's also possible that you ran into some issue with how threads are handled and didn't get info on the RangeError when you should have. Unfortunately, it's been long enough since I dealt with a program that ran into a problem like this on a separate thread that I don't know exactly what the current state of that is. But the first step for you would to be sure of which compiler flags that you're using and whether the code in question is @safe. That will tell you whether a RangeError should be being thrown or not when an array index is out-of-bounds.
- Jonathan M Davis
|