Skip to content

Commit

Permalink
Fix #3738: javalib Files.createTemp*(bogusPath) no longer loops forev…
Browse files Browse the repository at this point in the history
…er (#3739)
  • Loading branch information
LeeTibbert committed Feb 9, 2024
1 parent 575b60b commit 36509f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
16 changes: 14 additions & 2 deletions javalib/src/main/scala/java/nio/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ object Files {
attrs: Array[FileAttribute[_]]
): Path = {
val p = if (prefix == null) "" else prefix
val temp = FileHelpers.createTempFile(p, "", dir, minLength = false)
val temp = FileHelpers.createTempFile(
p,
"",
dir,
minLength = false,
throwOnError = true
)
if (temp.delete() && temp.mkdir()) {
val tempPath = temp.toPath()
setAttributes(tempPath, attrs)
Expand Down Expand Up @@ -307,7 +313,13 @@ object Files {
attrs: Array[FileAttribute[_]]
): Path = {
val p = if (prefix == null) "" else prefix
val temp = FileHelpers.createTempFile(p, suffix, dir, minLength = false)
val temp = FileHelpers.createTempFile(
p,
suffix,
dir,
minLength = false,
throwOnError = true
)
val tempPath = temp.toPath()
setAttributes(tempPath, attrs)
tempPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import StandardCopyOption._
import org.junit.Test
import org.junit.Assert._
import org.junit.Assume._
import org.junit.Ignore

import scala.util.{Try, Failure}

Expand Down Expand Up @@ -552,6 +553,31 @@ class FilesTest {
} finally Files.delete(dir)
}

/* This test is @Ignore'd for Continuous Integration (CI) because
* it creates, on purpose, an infinite loop when the condition described
* in Issue 3738 is present: bad path argument causes an infinite loop.
*
* The test can be run manually. It passes on JVM and should soon pass
* on Scala Native.
*
* If ever Scala Native gets the JUnit test timeout feature, this
* test can be changed for use in CI.
*/

// Issue 3738
@Ignore
@Test def filesCreateTempDirectoryDoesNotLoopInfinitely(): Unit = {
val path = Paths.get("/+")

try {
val f = Files.createTempFile(path, "Alpha", "Omega")
Files.delete(f)
fail("expected IOException or a sub-class of it")
} catch {
case e: IOException =>
}
}

@Test def filesCreateTempFileWorksWithNullPrefix(): Unit = {
val file = Files.createTempFile(null, "txt")
try {
Expand All @@ -576,6 +602,24 @@ class FilesTest {
} finally Files.delete(file)
}

/* See comments above filesCreateTempDirectoryDoesNotLoopInfinitely()
* test above. Same song, next verse.
*/

// Issue 3738
@Ignore
@Test def filesCreateTempFileDoesNotLoopInfinitely(): Unit = {
val path = Paths.get("/+")

try {
val f = Files.createTempFile(path, "Alpha", "Omega")
Files.delete(f)
fail("expected IOException or a sub-class of it")
} catch {
case e: IOException =>
}
}

@Test def filesIsRegularFileReportsFilesAsSuch(): Unit = {
withTemporaryDirectory { dirFile =>
val dir = dirFile.toPath
Expand Down

0 comments on commit 36509f7

Please sign in to comment.