Main Content

Hard-coded object size used to manipulate memory

Memory manipulation with hard-coded size instead of sizeof

Description

This defect occurs when you use hard-coded constants as memory size arguments for these memory functions:

  • Dynamic memory allocation function such as malloc or calloc.

  • Memory manipulation functions such as memcpy, memmove, memcmp, or memset.

When performing memory operations with a string literal, Polyspace® does not report a defect if you hard code the memory size.

Risk

If you hard code object size, your code is not portable to architectures with different type sizes. If the constant value is not the same as the object size, the buffer might or might not overflow.

Fix

For the size argument of memory functions, use sizeof(object).

Examples

expand all

#include <stddef.h>
#include <stdlib.h>
enum {
    SIZE3   = 3,
    SIZE20  = 20
};
extern void fill_ints(int **matrix, size_t nb, size_t s);

void bug_hardcodedmemsize()
{
    size_t i, s;

    s = 4;
    int **matrix = (int **)calloc(SIZE20, s);
    if (matrix == NULL) {
        return; /* Indicate calloc() failure */
    }
    fill_ints(matrix, SIZE20, s);
    free(matrix);
}

In this example, the memory allocation function calloc is called with a memory size of 4. The memory is allocated for an integer pointer, which can be a more or less than 4 bytes depending on your target. If the integer pointer is not 4 bytes, your program can fail.

Correction — Use sizeof(int *)

When calling calloc, replace the hard-coded size with a call to sizeof. This change makes your code more portable.

#include <stddef.h>
#include <stdlib.h>
enum {
    SIZE3   = 3,
    SIZE20  = 20
};
extern void fill_ints(int **matrix, size_t nb, size_t s);

void corrected_hardcodedmemsize()
{
    size_t i, s;

    s = sizeof(int *);
    int **matrix = (int **)calloc(SIZE20, s);
    if (matrix == NULL) {
        return; /* Indicate calloc() failure */
    }
    fill_ints(matrix, SIZE20, s);
    free(matrix);
}

In this example, the function clean_sensitive_memory clears sensitive information from the memory. Here, the memory size argument of memset is hardcoded to be 64 bytes. If s->data cannot accommodate 64 bytes, the program fails and the sensitive information might remain in memory. Polyspace reports defects on the memory operation.

#include<string.h>

struct sensitiveInfo
{
    unsigned char data[64];
    int length;
};

char key[64];

void clean_sensitive_memory (struct sensitiveInfo *s)
{
    memset (s->data, 0, 64);          //Defect
    memset ((void *) key, 0, 64);  //Defect
}

Correction — Use sizeof() for Size Argument

To fix this defect, replace the hardcoded memory sizes by calls to sizeof().

#include<string.h>

struct sensitiveInfo
{
    unsigned char data[64];
    int length;
};

char key[64];

void clean_sensitive_memory (struct sensitiveInfo *s)
{
  memset (s->data, 0, sizeof (s->data));	//Fixed
  memset ((void *) key, 0, sizeof(key)); //Fixed
}

Result Information

Group: Good Practice
Language: C | C++
Default: Off
Command-Line Syntax: HARD_CODED_MEM_SIZE
Impact: Low

Version History

Introduced in R2016b

expand all