rspec/rules/S2674/java/rule.adoc

63 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
You cannot assume that any given stream reading call will fill the ``++byte[]++`` passed in to the method. Instead, you must check the value returned by the read method to see how many bytes were read. Fail to do so, and you introduce bug that is both harmful and difficult to reproduce.
Similarly, you cannot assume that ``++InputStream.skip++`` will actually skip the requested number of bytes, but must check the value returned from the method.
This rule raises an issue when an ``++InputStream.read++`` method that accepts a ``++byte[]++`` is called, but the return value is not checked, and when the return value of ``++InputStream.skip++`` is not checked. The rule also applies to ``++InputStream++`` child classes.
=== Noncompliant code example
[source,java]
----
public void doSomething(String fileName) {
try {
InputStream is = new InputStream(file);
byte [] buffer = new byte[1000];
is.read(buffer); // Noncompliant
// ...
} catch (IOException e) { ... }
}
----
=== Compliant solution
[source,java]
----
public void doSomething(String fileName) {
try {
InputStream is = new InputStream(file);
byte [] buffer = new byte[1000];
int count = 0;
while (count = is.read(buffer) > 0) {
// ...
}
} catch (IOException e) { ... }
}
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/VzdGBQ[CERT, FIO10-J.] - Ensure the array is filled when using read() to fill an array
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]