Main Content

Pointer to non initialized value converted to const pointer

Pointer to constant assigned address that does not contain a value

Description

This defect occurs when a pointer to a constant (const int*, const char*, etc.) is assigned an address that does not yet contain a value.

For instance:

int x; const int * ptr = &x;

Risk

A pointer to a constant stores a value that must not be changed later in the program. If you assign the address of a non-initialized variable to the pointer, it now points to an address with garbage values for the remainder of the program.

Fix

Initialize a variable before assigning its address to a pointer to a constant.

Extend Checker

If a const pointer is initialized incorrectly only for certain system input values, you can see one possible combination of input values causing the defect. See Extend Bug Finder Checkers to Find Defects from Specific System Input Values.

Examples

expand all

#include<stdio.h>

void Display_Parity()
 {
  int num,parity;
  const int* num_ptr = &num;  
  /* Defect: Address &num does not store a value */

  printf("Enter a number\n:");
  scanf("%d",&num);

  parity=((*num_ptr)%2);
  if(parity==0)
    printf("The number is even.");
  else
    printf("The number is odd.");

 }

num_ptr is declared as a pointer to a constant. However the variable num does not contain a value when num_ptr is assigned the address &num.

Correction — Initialize Variable Before Assigning Its Address to const Pointer

One possible correction is to obtain the value of num from the user before &num is assigned to num_ptr.

#include<stdio.h>

void Display_Parity()
 {
  int num,parity;
  const int* num_ptr;

  printf("Enter a number\n:");
  scanf("%d",&num);

 /* Fix: Assign &num to pointer after it receives a value */ 
  num_ptr=&num;                     
  parity=((*num_ptr)%2);
  if(parity==0)
    printf("The number is even.");
  else
    printf("The number is odd.");
 }

The scanf statement stores a value in &num. Once the value is stored, it is legitimate to assign &num to num_ptr.

#include <stdlib.h>

int isElementInArray(const int* arr, const int elem);
void fillArray(int *);

void createArray(int elem) {
    int* arr = (int*) malloc (100*sizeof(int));
    isElementInArray(arr, elem);
}

In this example, the function isElementInArray takes a pointer to a const variable as first argument. The const specifier indicates that the function intends to read and not modify the pointed values. However, the array passed to this function is not initialized. The function isElementInArray, which can only read the array elements, reads non-initialized elements.

Correction – Initialize Memory Before Assigning to const Pointer

Initialize dynamically allocated memory before assigning its address to a const pointer. In this example, the array initialization is done using the fillArray function.

#include <stdlib.h>

int isElementInArray(const int* arr, const int elem);
void fillArray(int *);

void createArray(int elem) {
    int* arr = (int*) malloc (100*sizeof(int));
    fillArray(arr);
    isElementInArray(arr, elem);
}

Result Information

Group: Data flow
Language: C | C++
Default: Off
Command-Line Syntax: NON_INIT_PTR_CONV
Impact: Medium

Version History

Introduced in R2013b