Main Content

Invalid use of standard library routine

Wrong arguments to standard library function

Description

This defect occurs when you use invalid arguments with a function from the standard library. This defect picks up errors related to other functions not covered by float, integer, memory, or string standard library routines.

Risk

Invalid arguments to a standard library function result in undefined behavior.

Fix

The fix depends on the root cause of the defect. For instance, the argument to a printf function can be NULL because a pointer was initialized with NULL and the initialization value was not overwritten along a specific execution path.

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 <stdio.h>
#include <stdlib.h>

void print_null(void) {

  printf(NULL); 
}

The function printf takes only string input arguments or format specifiers. In this function, the input value is NULL, which is not a valid string.

Correction — Use Compatible Input Arguments

One possible correction is to change the input arguments to fit the requirements of the standard library routine. In this example, the input argument was changed to a character.

#include <stdio.h>

void print_null(void) {
    char zero_val = '0';
    printf((const char*)zero_val); 
}

Result Information

Group: Programming
Language: C | C++
Default: On
Command-Line Syntax: OTHER_STD_LIB
Impact: High

Version History

Introduced in R2013b