rspec/rules/S2674/java/rule.adoc
Fred Tingaud 6f24cc0632
Clean rule at root
In some cases, the `rule.adoc` at root of a rule is never included
anywhere and thus is dead code.
It's a maintenance cost by itself, but also it misses opportunities to
inline code that seems used by two documents when in fact only one
document is actually rendered. And this missed opportunity, in turn,
stops us from applying the correct language tag on the code samples.
2023-10-16 16:34:38 +02:00

63 lines
1.7 KiB
Plaintext

== 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[]