Insure++ Reference - PARM_DANGLING
This error is generated whenever a parameter declared as an array is
actually passed a pointer to a block of memory that has been freed.
The following code frees its memory block before passing it to
foo .
1: /*
2: * File: parmdngl.c
3: */
4: #include <stdlib.h>
5:
6: char foo(a)
7: char a[10];
8: {
9: return a[0];
10: }
11:
12: main()
13: {
14: char *a;
15: a = (char *)malloc(10);
16: free(a);
17: foo(a);
18: return (0);
19: }
[parmdngl.c:8] **PARM_DANGLING**
1. >> {
2. Array parameter is dangling pointer: a
3. Pointer : 0x0001adb0
4. In block : 0x0001adb0 thru 0x0001adb9 (10 bytes)
block allocated at:
malloc() (interface)
main() parmdngl.c, 15
stack trace where memory was freed:
5. main () parmdngl.c, 16
Stack trace where the error occurred:
foo() parmdngl.c, 8
6. main() freedngl.c,17
- Source line at which the problem was detected.
- Description of the problem and the parameter that is in error.
- Value of the pointer that was passed and has been deallocated.
- Information about the block of memory addressed by this
pointer, including information about where this block was
allocated.
- Indication of the line at which this block was freed.
- Stack trace showing the function call sequence leading to the
error.
This error is normally caused by freeing a piece of memory too soon.
A good strategy is to examine the line of code indicated by the
diagnostic message which shows where the memory block was freed and
check that it should indeed have been de-allocated.
A second check is to verify that the correct parameter was passed to
the subroutine.
A third strategy which is sometimes useful is to NULL
pointers that have been freed and then check in the called subroutine for
this case. Code similar to the following is often useful
#include <stdlib.h>
char foo(a)
char *a;
{
if(a) return a[0];
return '!';
}
main()
{
char *a;
a = (char *)malloc(10);
free(a);
a = NULL;
foo(a);
return (0);
}
The combination of resetting the pointer to NULL
after freeing it and the check in the called subroutine prevents misuse
of dangling pointers.
PARM_BAD_RANGE
PARM_NULL
|