Search

ParaSoft

HOME
PRODUCTS
SUPPORT
ABOUT
WHAT'S NEW
EVENTS


Insure++

Quick facts

Add-on Modules:
   -INUSE
   -TCA

Comparisons

Technical Papers

Support & Manuals

FAQs

Recent Reviews

User Testimonials

Press Releases


Insure tool to debug c++





Insure++ Reference - WRITE_NULL



WRITE_NULL

Writing to a NULL pointer

This error is generated whenever an attempt is made to dereference a NULL pointer.

Problem

This code attempts to use a pointer which has not been explicitly assigned. Since the variable a is global, it is initialized to zero by default, which results in dereferencing a NULL pointer in line 8.

	1:	/*
	2:	 * File: writnull.c
	3:	 */
	4:	int *a;
	5:
	6:	main()
	7:	{
	8:		*a = 123; bug
	9:		return (0);
	10:	}

Diagnosis (at runtime)

	[writnull.c:8] **WRITE_NULL**
1.	>> 		*a = 123;
	
2.		Writing null pointer: a

		Stack trace where the error occurred:
3.				main() writnull.c, 8
	
4.		**Memory corrupted. Program may crash!!**
  1. Source line at which the problem was detected.
  2. Description of the problem and the expression that is in error.
  3. Stack trace showing the function call sequence leading to the error.
  4. Informational message indicating that a serious error has occurred which may cause the program to crash.

Repair

A common cause of this problem is the one shown in the example - use of a pointer that has not been explicitly assigned and which is initialized to zero. This is usually due to the omission of an assignment or allocation statement which would give the pointer a reasonable value.

The example code might, for example, be corrected as follows

	1:  /*
	2:   * File: writnull.c (Modified)
	3:   */
	4:  int *a;
	5:
	6:  main()
	7:  {
	8:       int b;
	9:
	10:      a = &b;
	11:      *a = 123;
	12:  	 return (0);
	13:  }

A second common source of this error is code which dynamically allocates memory but then zeroes pointers as blocks are freed. In this case, the error would indicate reuse of a freed block.

A final common problem is caused when one of the dynamic memory allocation routines, malloc, calloc, or realloc, fails and returns a NULL pointer. This can happen either because your program passes bad arguments or simply because it asks for too much memory. A simple way of finding this problem with Insure++ is to enable the RETURN_FAILURE error code via your .psrc file and run the program again. It will then issue diagnostic messages every time a system call fails, including the memory allocation routines.


< WRITE_DANGLING > WRITE_OVERFLOW
Tools to debug c++ and java
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft