In this section:

Overview

The use of uninitialized memory is a difficult problem to isolate because the effects of the problem may not show up until much later. Many references to uninitialized memory are harmless, which further complicates the problem.

Insure++ distinguishes two sub-categories of the READ_UNINIT_MEM error class

  • copy - This error code is generated whenever an application assigns a variable using an uninitialized value. In itself, this may not be a problem, because the value may be reassigned to a valid value before use or may never be used. This error category is suppressed by default.
  • read - This code is generated whenever an uninitialized value is used in an expression or some other context where it must be incorrect. This error category is enabled by default, but is detected only if the checking_uninit option is on. See Configuration Options (psrc) for details.

The difference between these two categories is illustrated in the following examples.

Full checking may be disabled by setting the checking_uninit off option. See Configuration Options (psrc) for details.

If full uninitialized memory checking is disabled, uninitialized pointers will still be detected, but will be reported in the READ_UNINIT_PTR category.

CodeDescriptionEnabledReportedPlatform
READ_UNINIT_MEMReading uninitialized memory(error)RuntimeWindows/Unix
(copy)

Copy from uninitialized region

(error)RuntimeWindows/Unix
(read)

Use of uninitialized value

(tick)RuntimeWindows/Unix


Problem 1

The following code attempts to use a structure element which has never been initialized.

/*
 * File: readuni1.c
 */
#include <stdio.h>

main()
	{
		struct rectangle {
		int width;
		int height;
	};

	struct rectangle box;
	int area;

	box.width = 5;
	area = box.width*box.height;
	printf("area = %d\n", area);
	return (0);
}

Diagnosis at Runtime

[readuni1.c:17] **READ_UNINIT_MEM(read)**
>>		area = box.width * box.height;
Reading uninitialized memory: box.height
Stack trace where the error occurred:
		main() readuni1.c, 17
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the expression that is in error.
  • Line 5: Stack trace showing the function call sequence leading to the error. 

Problem 2

The following code assigns the value b using memory returned by the malloc system call, which is uninitialized.

/*
 * File: readuni2.c
 */
#include <stdlib.h>

main()
{
	int *a = (int *)malloc(5);
	int b;

	b = *a;
	return (0);
}

Diagnosis at Runtime

[readuni2.c:11] **READ_UNINIT_MEM(copy)**
>>			 b = *a;
Reading uninitialized memory: *a
In block: 0x00062058 thru 0x0006205c (5 bytes)
			block allocated at:
				malloc() (interface)
				main() readuni2.c, 8
Stack trace where the error occurred:
			main() readuni2.c, 11
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the expression that is in error.
  • Line 9: Stack trace showing the function call sequence leading to the error.

Repair

The READ_UNINIT_MEM(copy) error category is suppressed by default, so you will normally only see errors in the read category. In many cases, these will be errors that can be simply corrected by initializing the appropriate variables. In other cases, these values will have been assigned from other uninitialized variables, which can be detected by unsuppressing the copy sub-category and running again.

  • No labels