Main Content

Use of memset with size argument zero

Size argument of function in memset family is zero

Description

This defect occurs when you call a function in the memset family with size argument zero. Functions include memset, wmemset, bzero, SecureZeroMemory, RtlSecureZeroMemory, and so on.

Risk

void *memset (void *ptr, int value, size_t num) fills the first num bytes of the memory block that ptr points to with the specified value. A zero value of num renders the call to memset redundant. The memory that ptr points to:

  • Remains uninitialized, if not previously initialized.

  • Is not cleared and can contain sensitive data, if previously initialized.

Fix

Determine if the zero size argument occurs because of a previous error in your code. Fix the error.

Examples

expand all

#include <stdio.h>
#include <string.h>

void func (unsigned int size)
{
    char str[] = "Buffer to be filled.";
    memset (str,'-',size);
    puts (str);
}

void calling_func(void) {
    unsigned int buf_size=0;
    func(buf_size);
}

In this example, the argument size of memset is zero.

Result Information

Group: Programming
Language: C | C++
Default: Off
Command-Line Syntax: MEMSET_INVALID_SIZE
Impact: Medium

Version History

Introduced in R2015b