Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor edits; reviewed

The formatted output functions (fprintf() and related functions) convert, format, and print their arguments under control of a format string, defined as follows by the C Standard, 7.21.6.1, paragraph 3 [ISO/IEC 9899:2011]:

...

Common mistakes in creating format strings include

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

The following table summarizes the compliance of various conversion specifications.   The first column contains a conversion specifier character (or characters).   The next four columns consider the combination of the specifier character(s) with the various flags (the apostrophe ['], -, +, the space character, #, and # 0).   The next eight columns consider the combination of the specifier character(s) with the various length modifiers (h, hh, l, ll, j, z, t, and L).   Here, valid Valid combinations are marked with a type name; arguments matched with the conversion specification will be interpreted as that type.   For example, an argument matched with the specifier %hd will be interpreted as a short, so short appears in the cell where d and h intersect.   The last column denotes the expected type of arguments matched with the original specifier character(s).   Throughout the table, valid Valid and meaningful combinations are marked by the (tick) symbol (save for the length modifier columns, as described above).   Valid combinations that have no effect are labeled N/E.   Using a combination marked by the (error) symbol, using a specification not represented in the table, or using an argument of an unexpected type may result in undefined behavior. See undefined behaviors 153, 155, 157, 158, 161, and 162 in Annex J of the C Standard. 

Conversion
Specifier
Character

' XSI

-
+
SPACE


#


0 


h


hh


l


ll


j


z


t


L

Argument
Type

d, i

(tick)

(tick)

(error)

(tick) 

short

signed char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

Signed integer

o

(error)

(tick)

(tick)

(tick) 

unsigned short

unsigned char

unsigned long

unsigned long long

uintmax_t

size_t

ptrdiff_t

(error)

Unsigned integer

u

(tick)

(tick)

(error)

(tick)

 

unsigned short

unsigned  char

unsigned long

unsigned long long

uintmax_t

size_t

ptrdiff_t

(error)

Unsigned integer

x, X

(error)

(tick)

(tick)

(tick) 

unsigned short

unsigned char

unsigned long

unsigned long long

uintmax_t

size_t

ptrdiff_t

(error)

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 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)

None

Legend:

  • SPACE—the space (" ") character
  • N/E—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

The format formatted input functions (fscanf() and related functions) use similarly-specified format strings and impose similar restrictions on their format strings and arguments.

Do not supply an unknown or invalid conversion specification or an invalid combination of flag character, precision, length modifier, conversion specifier; to a formatted IO function. Likewise, do not provide a number or type of arguments that do not match the conversion specifiers argument type of the conversion specifier used in the format string.

Format strings are usually string literals specified at the call site, but they need not be. They should, however, not contain unsanitized data; see FIO30-C. Exclude user input from format strings for more information.

...

Mismatches between arguments and conversion specifications may result in undefined behavior.   Many compilers can Compilers may diagnose type mismatches in formatted output function invocations.   In the following noncompliant code example, the error_type argument to printf() is incorrectly matched with the %s specifier, rather than with the the %d specifier specifier. Likewise, the the error_msg argument is incorrectly matched with the %d specifier instead of the %s specifier.   These usages result in undefined behavior. One possible result of this invocation is that printf() will interpret the error_type argument as a pointer, and try to read a string from the address that error_type contains. This is likely to result , possibly resulting in an access violation.

Code Block
bgColor#ffcccc
langc
#include <stdio.h>
 
void func(void) {
  const char *error_msg = "Resource not available to user.";
  int error_type = 3;
  /* ... */
  printf("Error (type %s): %d\n", error_type, error_msg);
  /* ... */
}

...

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

  /* ... */
}

Risk Assessment

In most cases, incorrectly Incorrectly specified format strings will can result in abnormal program termination. However, in some cases they can be used to corrupt memory in manners a manner controllable by an attacker.

RecommendationRule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO47-C

High

Unlikely

Medium

P6

L2

...

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

Related Guidelines

CERT C++ Secure Coding StandardFIO00-CPP. Take care when creating format strings
ISO/IEC TS 17961:2013Using invalid format strings [invfmtstr]
MITRE CWECWE-686, Function call with incorrect argument type

...