Debugging Dynamic Memory Usage Errors Using HP WDB (5900-1474; WDB 6.2; January 2011)
Example 12 Bounds-checking to detect out-of-bounds writes
Sample Program
bash-2.05b$ cat bounds.c
1 #include<stdio.h>
2
3 int main()
4 {
5 char *cp = (char*)malloc(100);
6 cp[-1] = 100;
7 strcpy(cp,"Hello");
8 cp[100] = 100;
9 free(cp);
10 exit(0);
11 }
Sample Debugging Session
bash-2.05b$ gdb bounds
HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00
and target hppa1.1-hp-hpux11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 5.5.0 (based on GDB) is covered
by the GNU General Public License. Type "show copying" to
see the conditions to change it and/or distribute copies.
Type "show warranty" for warranty/support.
..
(gdb) set heap-check bounds on
(gdb) b main
Breakpoint 1 at 0x2c04: file bounds.c, line 5 from bounds.
(gdb) b 10
Breakpoint 2 at 0x2c58: file bounds.c, line 10 from bounds.
(gdb) r
Starting program: bounds
Breakpoint 1, main () at bounds.c:5
5 char *cp = (char*)malloc(100);
(gdb) c
Continuing.
warning: Memory block (size = 100 address = 0x4042a3c8)
appears to be corrupted at the beginning.
Allocation context not found
#1 main() at bounds.c:5
#2 _start() from /usr/lib/libc.2
#3 _start() from /opt/langtools/lib/librtc.sl
#4 $START$() from
warning: Use command backtrace (bt) to see the current context.
Ignore top 4 frames belonging to leak detection library of gdb.
0x70e78d7c in __rtc_event+0 () from /opt/langtools/lib/librtc.sl
Memory-Debugging Features of WDB 49