Bad array indexing is considered deadly

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 2 04:26:09 PDT 2017


On 6/1/17 8:25 AM, Paolo Invernizzi wrote:
> On Thursday, 1 June 2017 at 10:26:24 UTC, Steven Schveighoffer wrote:
>> I can detail exactly what happened in my code -- I am accepting dates
>> from a given week from a web request. One of the dates fell outside
>> the week, and so tried to access a 7 element array with index 9.
>> Nothing corrupted memory, but the runtime corrupted my entire process,
>> forcing a shutdown.
>
> And that's a good thing! The input should be validated, especially
> because we are talking about a web request.
>
> See it like being kind with the other side of the connection, informing
> it with a clear "rejected as the date is invalid".

If only that is what happened, I would not have started this thread!

In any case, the way forward is clear -- create containers that don't 
throw Error, and make them easy to use.

I think I will actually publish them, because it's a very useful thing 
to have. You can validate your input all you want, but if you have a 
program bug, or there is something you didn't consider, then the entire 
server isn't crashed because of it. I *like* the bounds checking, I 
don't have to translate back to the input what it will mean for every 
array access in the function -- the simple check is enough.

Still good to have it auto-restart, which I will also do. But having 
some sort of feedback to the client, and an attempt to continue on with 
other unrelated requests is preferable.

-Steve


More information about the Digitalmars-d mailing list