[Cpp]
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
Void fn (char * str)
{
Memset (str, 0, 64 );
Return;
}
Int main (int argc, char ** argv)
{
Char badstr [32] = "abc ";
Int fd = 1;
Printf ("badstr = % s \ n", badstr );
Printf ("fd = % d \ n", fd );
Fd = 2;
Printf ("fd = % d \ n", fd );
Fn (badstr );
Printf ("fd = % d \ n", fd );
Printf ("badstr = % s \ n", badstr );
Return 0;
}
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
Void fn (char * str)
{
Memset (str, 0, 64 );
Return;
}
Int main (int argc, char ** argv)
{
Char badstr [32] = "abc ";
Int fd = 1;
Printf ("badstr = % s \ n", badstr );
Printf ("fd = % d \ n", fd );
Fd = 2;
Printf ("fd = % d \ n", fd );
Fn (badstr );
Printf ("fd = % d \ n", fd );
Printf ("badstr = % s \ n", badstr );
Return 0;
}
The above code obviously out-of-memory, one watch.
The positioning process is as follows:
[Root @ localhost qiyk] #./test
Badstr = abc
Fd = 1
Fd = 2
Fd = 0
Badstr =
Bus Error [Program exited due to abnormal memory out of bounds]
[Root @ localhost qiyk] #./gdb test
GNU gdb Red Hat Linux (6.6-8. fc7rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License,
Welcome to change it and/or distribute copies of it under certain
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" fo
This GDB was configured as "i386-redhat-linux-gnu "...
Using host libthread_db library "/lib/i686/nosegneg/libthread_db.
(Gdb) B main
Breakpoint 1 at 0x80484cb: file test. cpp, line 13.
(Gdb) r
Starting program:/home/qiyk/test
Breakpoint 1, main () at test. cpp: 13
13 char badstr [32] = "abc ";
(Gdb) n
14 int fd = 1;
(Gdb) watch fd
Hardware watchpoint 2: fd
(Gdb) c
Continuing.
Hardware watchpoint 2: fd [the first manual modification is interrupted here]
Old value = 6317008
New value = 1
Main () at test. cpp: 15
15 printf ("badstr = % s \ n", badstr );
(Gdb) c
Continuing.
Badstr = abc
Fd = 1
Hardware watchpoint 2: fd [the second manual modification is interrupted here]
Old value = 1
New value = 2
Main () at test. cpp: 18
18 printf ("fd = % d \ n", fd );
(Gdb) c
Continuing.
Fd = 2
Hardware watchpoint 2: fd [third accidental modification, interrupted here]
Old value = 2
New value = 0
0x004ea367 in memset () from/lib/i686/nosegneg/libc. so.6
(Gdb) bt [view the on-site Stack]
#0 0x004ea367 in memset () from/lib/i686/nosegneg/libc. so.6
#1 0x080484b8 in fn (str = 0xbf92bd20 "") at test. cpp: 7
#2 0x0804854d in main () at test. cpp: 19
(Gdb) up
#1 0x080484b8 in fn (str = 0xbf92bd20 "") at test. cpp: 7
7 memset (str, 0, 64); [problem point: str out of bounds, resulting in the fd value becoming 0]
(Gdb) q
The program is running. Exit anyway? (Y or n) y