rspec/rules/S5445/java/rule.adoc

37 lines
980 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
Using ``++File.createTempFile++`` as the first step in creating a temporary directory causes a race condition and is inherently unreliable and insecure. Instead, ``++Files.createTempDirectory++`` (Java 7+) should be used.
2021-01-20 04:06:26 +00:00
2021-02-02 15:02:10 +01:00
2021-01-20 04:06:26 +00:00
This rule raises an issue when the following steps are taken in immediate sequence:
2021-01-27 13:42:22 +01:00
* call to ``++File.createTempFile++``
2021-01-20 04:06:26 +00:00
* delete resulting file
2021-01-27 13:42:22 +01:00
* call ``++mkdir++`` on the File object
2021-01-20 04:06:26 +00:00
2021-01-27 13:42:22 +01:00
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++7++``.
2021-01-20 04:06:26 +00:00
== Noncompliant Code Example
----
File tempDir;
tempDir = File.createTempFile("", ".");
tempDir.delete();
tempDir.mkdir(); // Noncompliant
----
== Compliant Solution
----
Path tempPath = Files.createTempDirectory("");
File tempDir = tempPath.toFile();
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]