Debugging Dynamic Memory Usage Errors Using HP WDB v6.3 (5900-2181, August 2012)

Example 13 Detecting heap corruption using the info corruption command
Sample Program
$cat infobounds.c
1 #include <stdio.h>
2 #include <malloc.h>
3
4 char *t;
5 char *t1;
6 char *t2;
7 char *t3;
8
9 char * sm_malloc(sz)
10 int sz;
11 {
12 return malloc(sz); /* line number 12 */
13 }
14
15 main()
16 {
17 t = (char *)sm_malloc(10);
18 strcpy(t, "123456789123");
19 t1 = (char *)sm_malloc(10);
20 strcpy(t1, "12345678912");
21 t2 = (char *)sm_malloc(10);
22 strcpy(t2, "1234567891");
23 t3 = (char *)sm_malloc(10);
24 strcpy(t3, "123456789");
25 printf("Hello\n");
26 free (t);
27 free (t1);
28 free (t2);
29 free (t3);
30 free (t);
31 free (t1);
32 exit(1);
33 }
Sample Debugging Session
$ gdb infobounds
HP gdb 5.6 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 5.6 (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) b 25
Breakpoint 1 at 0x4000b70:1: file infobounds.c,
line 25 from infobounds.
(gdb) set heap-check on
(gdb) run
Starting program: infobounds
Breakpoint 1, main ()
at .infobounds.c:25
25 printf("Hello\n");
(gdb) info corruption
Analyzing heap ...
Following blocks appear to be corrupted
No. Total bytes Blocks Address Function
0 10 1 0x400124e0 sm_malloc()
1 10 1 0x40012500 sm_malloc()
42