Bad array indexing is considered deadly

Moritz Maxeiner via Digitalmars-d digitalmars-d at puremagic.com
Wed May 31 12:17:16 PDT 2017


On Wednesday, 31 May 2017 at 13:04:52 UTC, Steven Schveighoffer 
wrote:
> [...]
>
> What are your thoughts? Have you run into this? If so, how did 
> you solve it?
>

It is not that accessing the array out of bounds *leading* to 
data corruption that is the issue here, but that in general you 
have to assume that the index *being* out of bounds is itself the 
*result* of *already occurred* data corruption; and if data 
corruption occurred for the index, you *cannot* assume that 
*only* the index has been affected. The runtime cannot simply 
assume the index being out of bounds is not the result of already 
occurred data corruption, because that is inherently unsafe, so 
it *must* terminate asap as the default.

If you get the index as the input to your process - and thus 
*know* that it being out of bounds is not the result of previous 
data corruption - then you should check this yourself before 
accessing the array and handle it appropriately (e.g. via 
Exception).

So in your specific use case I would say use a wrapper. This is 
one of the reasons why I am working on my own library for data 
structures (libds).


More information about the Digitalmars-d mailing list