0
int array[100];
int *ptr = &array[102];

Supposing that there was some way to precisely detect buffer overflow, when will this code experience a buffer overflow? Is it compile time? My research tells me no, but I'm not sure.

Sara
  • 121
  • 7
  • This code does nothing *per se*, please show a minimal test code. – ouah Oct 15 '15 at 22:17
  • 1
    A static analysis tool or a very smart compiler might be able to pick up this error, but you typically won't see a problem until you read or write via `ptr`, at run-time. – Paul R Oct 15 '15 at 22:18
  • @ouah I was referring to the invalid ptr assignment. It's a hypothetical question. – Sara Oct 15 '15 at 22:20
  • @PaulR Meaning, for example, if I attempted to dereference ptr and assign its value to an int variable? – Sara Oct 15 '15 at 22:21
  • 2
    @Sara the second declaration invokes undefined behavior, in practice as soon as you write to `*ptr` your buffer is overflown. – ouah Oct 15 '15 at 22:22
  • Thanks! @ouah That was my intuition, but I wasn't sure. – Sara Oct 15 '15 at 22:24

0 Answers0