Subclause 188.8.131.52 of the C Standard [ISO/IEC 9899:2011] defines
ungetc() as follows:
ungetcfunction 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 (
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
ungetwc(), C guarantees only one wide character of pushback (subclause 184.108.40.206). 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
Noncompliant Code Example
In this noncompliant code example, more than one character is pushed back on the stream referenced by
If more than one character needs to be pushed by
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.
Remember to always call
fsetpos(). (See FIO44-C. Only use values for fsetpos() that are returned from fgetpos().)
If used improperly,
ungetwc() can cause data to be truncated or lost.
|(customization)||Users can implement a custom check that triggers a warning when |
Can detect simple violations of this recommendation. In particular, it warns when two calls to
|LDRA tool suite|
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
|[ISO/IEC 9899:2011]||Subclause 220.127.116.11, "The |