Main Content

Writing to const qualified object

Object declared with a const qualifier is modified

Description

This defect occurs when you do one of the following:

  • Use a const-qualified object as the destination of an assignment.

  • Pass a const-qualified object to a function that modifies the argument.

For instance, the defect can occur in the following situations:

  • You pass a const-qualified object as first argument of one of the following functions:

    • mkstemp

    • mkostemp

    • mkostemps

    • mkdtemp

  • You pass a const-qualified object as the destination argument of one of the following functions:

    • strcpy

    • strncpy

    • strcat

    • memset

  • You perform a write operation on a const-qualified object.

Risk

The risk depends upon the modifications made to the const-qualified object.

SituationRisk
Passing to mkstemp, mkostemp, mkostemps, mkdtemp, and so on.These functions replace the last six characters of their first argument with a string. Therefore, they expect a modifiable char array as their first argument.
Passing to strcpy, strncpy, strcat, memset and so on.These functions modify their destination argument. Therefore, they expect a modifiable char array as their destination argument.
Writing to the objectThe const qualifier implies an agreement that the value of the object will not be modified. By writing to a const-qualified object, you break the agreement. The result of the operation is undefined.

Fix

The fix depends on the modification made to the const-qualified object.

SituationFix
Passing to mkstemp, mkostemp, mkostemps, mkdtemp, and so on.Pass a non-const object as first argument of the function.
Passing to strcpy, strncpy, strcat, memset and so on.Pass a non-const object as destination argument of the function.
Writing to the objectPerform the write operation on a non-const object.

See examples of fixes below.

If you do not want to fix the issue, add comments to your result or code to avoid another review. See:

Examples

expand all

#include <string.h>
const int **constpp2int;
int *p2int;
const int cint = 5;
void func(const char *buff)
{
// Modifying a const char    
        strcpy((char*)buff, "XXXXXX"); /*Noncompliant */
// Modifying a const pointer        
  constpp2int = &p2int; 
  //...
  *constpp2int = &cint; 
  //...
  *p2int = 0;   /* Noncompliant */        
       

}

In this example, because buff is const-qualified, modifying the variable results in a violation. The const integer cint is modified when *p2int is set to 0. Polyspace® reports defects when const objects are modified.

Correction — Copy const-Qualified Object to Non-const Object

Avoid using the const qualifier on objects that are intended to be modified.


#include <string.h>
const int **constpp2int;
int *p2int;
int cint = 5;
void func( char *buff)
{
// Modifying a const char    
        strcpy((char*)buff, "XXXXXX"); /*Compliant */
// Modifying a const pointer        
  constpp2int = &p2int; 
  //...
  *constpp2int = &cint; 
  //...
  *p2int = 0;   /* Compliant */        
       

}

Result Information

Group: Programming
Language: C | C++
Default: On for handwritten code, off for generated code
Command-Line Syntax: CONSTANT_OBJECT_WRITE
Impact: High

Version History

Introduced in R2015b