Re: gdb not catching out-of-bounds pointer



Tomás Ó hÉilidhe <toe@xxxxxxxxxxx> writes:

Here's my program:

#include <string.h>

int main(void)
{
char buf1[5];
char buf2[5]; /* Only got room for a 4-character string */

strcpy(buf2, "dogs5"); /* Wups the source string is too long */

return 0;
}

I don't get a segfault because buf2 spills over into buf1. Ideally I
would like gdb to catch the out-of-bounds error, but instead it runs
it without error:

(gdb) run
Starting program: /home/toe/blah

Program exited normally.
(gdb)

Is there anyway I can set gdb to catch this kind of error?

If you know what array is being overrun, and you just want to know
where, you might be able to set a watchpoint at the address one byte
past the end of the array.

Otherwise, you need something like valgrind, which effectively
single-steps your program and does bounds checking. It will be
dramatically slower than running the program normally, and isn't
supported on all platfoms.

Unfortunately, this is something that C tools have a hard time dealing
with, other than to tell the programmer "Be more careful."
.



Relevant Pages

  • Re: [linux-usb-devel] Re: Fw: Re: oops in choose_configuration()
    ... pointer, an array, and a pointer to an array, especially when following ... We start out with the first interface setting, as we always know we have ... The bMaxPower value could be different for different altsettings. ...
    (Linux-Kernel)
  • Re: "init.c: No such file or directory."
    ... I ran the program with a debugging option (gdb) and I got the ... I've tried starting with a working version and building it up to the point where it fails, and my experience with that method hasn't been good. ... If it fails with array size 65536, ...
    (comp.lang.fortran)
  • Re: Passing C++ array to Fortran function?
    ... > Now the problem I'm experiencing comes when I try to pass an array to ... Here's a dump from a run through gdb: ... If you declare a specific bound in the Ftn callee it ...
    (comp.lang.fortran)
  • Re: Passing C++ array to Fortran function?
    ... >> Now the problem I'm experiencing comes when I try to pass an array to ... Because it came with GCC. ... No run-time errors per se, but ITPLBV has its own error messages - it ... keeps reporting that X is empty, as my gdb listing above shows. ...
    (comp.lang.fortran)
  • Taking the control by abusing array index.
    ... This traceroute has a similar bug, it refers to an array of pointers ... You have to find in this case the address of ptr. ...
    (Vuln-Dev)