Out of bounds access

Revision en1, by DBradac, 2017-02-20 23:22:10

Everyone has had a bug in their code because of accessing out of array bounds. However, in some cases, the program will run as expected.

I have two examples from today's contest: problem B — 24829988 problem C — 24831518

In both of these problems, I had stupid bugs. For problem B, it's possible that the argument k of the function is zero and this line

if (ind == f[k-1]) return (int) (n % 2)

will access f[ - 1] which is undefined.

I have found this bug, and after some stress testing locally and on CodeForces, I decided not to resubmit because it seemed fine.

Now, in problem C, I iterated up to 1029 and the value of i xor x can become larger than the size of the array. However, this is only an issue if that piece of memory is not used by the process and it will result in seg fault.

Otherwise, nothing will be changed because we add zero.

I only found this bug while I was trying to hack someone else's solution so I couldn't resubmit and I was pretty sure it was going to fail system tests because locally it resulted in seg fault for some cases.

To my surprise, both submissions were actually successful, and my F failed (but that's irrelevant to this post xD) My question is this: what do you usually do when you find a bug like this during the contest and do you stress test your solutions before submitting to prevent it?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English DBradac 2017-02-20 23:22:10 1451 Initial revision (published)