CertC-FIO13

Never push back anything other than one read character

Required inputs: IR

Subclause 7.21.7.10 of the C Standard [ ISO/IEC 9899:2011 ] defines ungetc() as follows:

The ungetc function pushes the character specified by c (converted to an unsigned char ) back onto the input stream pointed to by stream . Pushed-back characters will be returned by subsequent reads on that stream in the reverse order of their pushing. A successful intervening call (with the stream pointed to by stream ) to a file positioning function ( fseek , fsetpos , or rewind ) discards any pushed-back characters for the stream. The external storage corresponding to the stream is unchanged.

One character of pushback is guaranteed.

Consequently, multiple calls to ungetc() on the same stream must be separated by a call to a read function or a file-positioning function (which will discard any data pushed by ungetc() ).

Likewise, for ungetwc() , C guarantees only one wide character of pushback (subclause 7.29.3.10). Consequently, multiple calls to ungetwc() on the same stream must be separated by a call to a read function or a file-positioning function (which will discard any data pushed by ungetwc() ).

Noncompliant Code Example

In this noncompliant code example, more than one character is pushed back on the stream referenced by fp :

FILE *fp;
char *file_name;

/* Initialize file_name */

fp = fopen(file_name, "rb");
if (fp == NULL) {
  /* Handle error */
}

/* Read data */

if (ungetc('\n', fp) == EOF) {
  /* Handle error */
}
if (ungetc('\r', fp) == EOF) {
  /* Handle error */
}

/* Continue */
Compliant Solution

If more than one character needs to be pushed by ungetc() , then fgetpos() and fsetpos() should be used before and after reading the data instead of pushing it back with ungetc() . Note that this solution applies only if the input is seekable.

FILE *fp;
fpos_t pos;
char *file_name;

/* Initialize file_name */

fp = fopen(file_name, "rb");
if (fp == NULL) {
  /* Handle error */
}

/* Read data */

if (fgetpos(fp, &pos)) {
  /* Handle error */
}

/* Read the data that will be "pushed back" */

if (fsetpos(fp, &pos)) {
  /* Handle error */
}

/* Continue */

Remember to always call fgetpos() before fsetpos() . (See FIO44-C. Only use values for fsetpos() that are returned from fgetpos() .)

Risk Assessment

If used improperly, ungetc() and ungetwc() can cause data to be truncated or lost.

Recommendation Severity Likelihood Detectable Repairable Priority Level
FIO13-C Medium Probable No No P4 L3
Bibliography
[ ISO/IEC 9899:2011 ] Subclause 7.21.7.10, "The ungetc Function"
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/input-output-fio/fio13-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

second_call_without_reset

Never push back anything other than one read character.

None

False

Options

allocators

allocators : set[bauhaus.analysis.config.QualifiedName] = {'fopen'}

Set of functions returning a new stream.
 

deallocators

deallocators

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'fclose': 0
}
Functions closing a stream, mapped to the argument position of the stream.
 

one_time_functions

one_time_functions

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'ungetc': 1
}
Functions that may operate once on a stream, mapped to the argument position of the stream.
 

reset_functions

reset_functions

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'fgetc': 0,
   'fgets': 2,
   'fread': 3,
   'fseek': 0,
   'fsetpos': 0,
   'rewind': 0
}
Functions resetting a stream, mapped to the argument position of the stream.