Discarded Read Result
ID |
java.read_readline_return_values |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability |
Description
Reports a while (reader.readLine() != null) loop — one driven by BufferedReader.readLine()
whose return value is never captured, so every line the loop reads is discarded.
Reader.read()/InputStream.read() compared against -1 is deliberately out of scope:
while (in.read() != -1) {} is the ordinary drain idiom, and consuming a byte to make progress is
the point of the call rather than a lost result.
Rationale
read() and readLine() return the data they just consumed from the stream — a character/byte, or a line of text — alongside the end-of-stream signal. Comparing the call directly against the sentinel, as in while (reader.readLine() != null), uses the call only to detect end-of-stream: the line or byte it returned is never stored anywhere and is gone the moment the comparison finishes.
// Bad -- every line read is immediately discarded
while (reader.readLine() != null) {
// nothing here has access to what was just read
}