Static Analysis Problem Type Reference

Unsafe format specifier

Some forms of formatted input can cause buffer overflow and should not be used.

Care must be taken on formatted input to avoid buffer overflow. In particular, the "%s" input format is inherently unsafe. Better is "%ddds", where ddd is the sized of the destination buffer, for example "%24s".

ID

code location

Description

1

Format mismatch

The unsafe formatted input statement

Example

          
#include <stdio.h>

char buffer[1024];

int main(int argc, char **argv)
{
    scanf("%s", buffer); // unsafe: could overflow buffer
    // better is scanf("%.1024s", buffer);
    printf("read %s\n", buffer);
    return 0;
}