Wiki Markup |
---|
Section 7.19.7.11 of C99 defines {{ungetc()}} as follows: \[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]: |
The
ungetc
function pushes the character specified byc
(converted to anunsigned char
) back onto the input stream pointed to bystream
. 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 bystream
) to a file positioning function (fseek
,fsetpos
, orrewind
) 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()
, C99 only guarantees one wide character of pushback (section Section 7.24.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()
).
...
Remember to always call fgetpos()
before fsetpos()
. (see See rule 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 | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO13-C | medium | probable | high | P4 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||
---|---|---|---|---|---|---|---|
|
|
|
|
...
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
Related Guidelines
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 7.19.7.11, "The {{ Wiki Markup ungetc
}} function"
Bibliography
...
FIO12-C. Prefer setvbuf() to setbuf() 09. Input Output (FIO)