...
This rule applies to any InputStream
or Reader
subclass that provide an implementation of the read()
method. This rule is a specific instance of NUM12-J. Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data.
Noncompliant Code Example (byte
)
This noncompliant code example casts the value returned by the read()
method directly to a value of type byte
, and then compares this value with -1 in an attempt to detect the end of the stream.
...
When the read()
method in this noncompliant code example returns the byte
value 0xFF
, it will be indistinguishable from the -1 value used to indicate the end of stream, because the byte value is promoted and sign extended to an int
before being compared with -1.
Compliant Solution (byte
)
Use a variable of type int
to capture the return value of the byte input method. When the value returned by read()
is not -1, it can be safely cast to type byte
. When read()
returns 0XFF
, the comparison will test 0x000000FF
against 0xFFFFFFFF
and fail.
Code Block | ||
---|---|---|
| ||
FileInputStream in; // initialize stream int inbuff; byte data; while ((inbuff = in.read()) != -1) { data = (byte) inbuff; // ... } |
Noncompliant Code Example (char
)
This noncompliant code example casts the value of type int
returned by the read()
method directly to a value of type char
, which is then compared with -1 in an attempt to detect the end of stream. This conversion leaves the value of c
as 0xffff
(e.g., Character.MAX_VALUE
) instead of -1. Consequently, the test for the end of stream never evaluates to true
.
Code Block | ||
---|---|---|
| ||
FileReader in; // initialize stream char c; while ((c = (char) in.read()) != -1) { // ... } |
Compliant Solution (char
)
Use a variable of type int
to capture the return value of the character input method. When the value of returned by read()
is not -1, it can be safely cast to type char
.
Code Block | ||
---|---|---|
| ||
FileReader in; // initialize stream int inbuff; char data; while ((inbuff = in.read()) != -1) { data = (char) inbuff; // ... } |
Risk Assessment
Historically, using a narrow type to capture the return value of a byte input function has resulted in significant vulnerabilities, including command injection attacks; see CA-1996-22 advisory. Consequently, the severity of this error is high.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO14-J | high | probable | medium | P12 | L1 |
Automated Detection
FindBugs version 1.3.9 can detect violations of this rule with the INT: Bad comparison of nonnegative value with negative constant detector.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
C Secure Coding Standard | FIO34-C. Use int to capture the return value of character IO functions |
C++ Secure Coding Standard | FIO34-CPP. Use int to capture the return value of character IO functions |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a3c9e958cc652df0-b1e841e9-455546e6-967f834e-75894b01517f88a81dde8d17"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a45a8e248f3fb965-ea402a00-40e64a00-ad5a9a6c-61a9816a2604be6659e07977"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Section 4.2 | http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2] "Primitive Types and Values" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a7ef28c151a7ff14-78e99e39-4936439e-b440bdf2-8871ba831951d34d4386ab19"><ac:plain-text-body><![CDATA[ | [[Pugh 2008 | AA. Bibliography#Pugh 08]] | "Waiting for the end" | ]]></ac:plain-text-body></ac:structured-macro> |
...