Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Each conversion specification is introduced by the % character followed (in order) by

  • zero Zero or more flags (in any order), which modify the meaning of the conversion specification
  • an An optional minimum field width
  • an An optional precision that gives the minimum number of digits to appear for certain conversion specifiers
  • an An optional length modifier that specifies the size of the argument
  • a A conversion specifier character that indicates the type of conversion to be applied

Common mistakes in creating format strings include

  • providing Providing insufficient arguments for the format string
  • using Using invalid conversion specifiers
  • using Using a flag character that is incompatible with the conversion specifier
  • using Using a length modifier that is incompatible with the conversion specifier
  • mismatching Mismatching the argument type and conversion specifier
  • using Using an argument of type other than int for width or precision

...

Conversion
Specifier
Character

' XSI

-
+
SPACE


#


0

 


h


hh


l


ll


j


z


t


L

Argument
Type

d, i

(tick)

(tick)

(error)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

signed Signed integer

o

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned Unsigned integer

u

(tick)

(tick)

(error)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned Unsigned integer

x

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned Unsigned integer

X

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned Unsigned integer

f, F

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

e, E

(error)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

g, G

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

a, A

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

c

(error)

(tick)

(error)

(error)

 

(error)

(error)

wint_t

(error)

(error)

(error)

(error)

(error)

int or wint_t

s

(error)

(tick)

(error)

(error)

 

(error)

(error)

NTWS

(error)

(error)

(error)

(error)

(error)

NTBS or NTWS

p

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

void*

n

(error)

(tick)

(error)

(error)

 

short*

char*

long*

long long*

intmax_t*

size_t*

ptrdiff_t*

(error)

pointer Pointer to integer

C XSI

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

wint_t

S XSI

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

NTWS

%

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

noneNone

Legend:

  • SPACE—the space (" ") character
  • N/E—No —no effect
  • NTBS—char* argument pointing to a null-terminated byte string
  • NTWS—wchar_t* argument pointing to a null-terminated wide character string
  • XSI—ISO/IEC 9945-2003 XSI extension

...

This compliant solution ensures that the format arguments match their respective format specifications.:

Code Block
bgColor#ccccff
langc
const char *error_msg = "Resource not available to user.";
int error_type = 3;
/* ... */
printf("Error (type %d): %s\n", error_type, error_msg);

...

In this compliant solution, the field width and precision arguments to printf() format directives are of type int.:

Code Block
bgColor#ccccff
langc
int print_int(int i, int width, int prec) {
  int n;

  n = printf("%*.*d", width, prec, i);

  return n;
}

...

Tool

Version

Checker

Description

GCC

Include Page
GCC_V
GCC_V

 

Can detect violations of this recommendation when the -Wformat flag is used.

Klocwork

Include Page
Klocwork_V
Klocwork_V

SV.FMT_STR

 

LDRA tool suite

Include Page
LDRA_V
LDRA_V

486 S
589 S

Fully implemented.

PRQA QA-C
Include Page
PRQA_V
PRQA_V

0179 (U)
0180 (C99)
0184 (U)
0185 (U)
0190 (U)
0191 (U)
0192 (U)
0193 (U)
0194 (U)
0195 (U)
0196 (U)
0197 (U)
0198 (U)
0199 (U)
0200 (U)
0201 (U)
0202 (I)
0206 (U)

Partially implemented.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...