Skip to content

Commit

Permalink
Fix file leak when using metadata functions (#1359)
Browse files Browse the repository at this point in the history
* Fix file leak when using `metadata` functions

* Add test for file leak
  • Loading branch information
pablobaxter authored Oct 1, 2023
1 parent 59555b9 commit ce4df5e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
7 changes: 4 additions & 3 deletions okio/src/jvmMain/kotlin/okio/ZipFileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ internal class ZipFileSystem internal constructor(
return basicMetadata
}

val source = fileSystem.openReadOnly(zipPath).use { fileHandle ->
fileHandle.source(entry.offset).buffer()
return fileSystem.openReadOnly(zipPath).use { fileHandle ->
return@use fileHandle.source(entry.offset).buffer().use { source ->
source.readLocalHeader(basicMetadata)
}
}
return source.readLocalHeader(basicMetadata)
}

override fun openReadOnly(file: Path): FileHandle {
Expand Down
97 changes: 97 additions & 0 deletions okio/src/jvmTest/kotlin/okio/FileLeakTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2023 Square, Inc. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio

import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import okio.Path.Companion.toPath
import okio.fakefilesystem.FakeFileSystem
import org.junit.After
import org.junit.Before
import org.junit.Test

class FileLeakTest {

private lateinit var fakeFileSystem: FakeFileSystem
private val fakeZip = "/test.zip".toPath()
private val fakeEntry = "some.file".toPath()

@Before
fun setup() {
fakeFileSystem = FakeFileSystem()
with(fakeFileSystem) {
write(fakeZip) {
writeZip {
putEntry(fakeEntry.name) {
writeUtf8("FooBar")
}
}
}
}
}

@After
fun tearDown() {
fakeFileSystem.checkNoOpenFiles()
}

@Test
fun zipFileSystemExistsTest() {
val zipFileSystem = fakeFileSystem.openZip(fakeZip)
assertTrue(zipFileSystem.exists(fakeEntry))
fakeFileSystem.checkNoOpenFiles()
}

@Test
fun zipFileSystemMetadataTest() {
val zipFileSystem = fakeFileSystem.openZip(fakeZip)
assertNotNull(zipFileSystem.metadataOrNull(fakeEntry))
fakeFileSystem.checkNoOpenFiles()
}

@Test
fun zipFileSystemSourceTest() {
val zipFileSystem = fakeFileSystem.openZip(fakeZip)
zipFileSystem.source(fakeEntry).use { source ->
assertEquals("FooBar", source.buffer().readUtf8())
}
fakeFileSystem.checkNoOpenFiles()
}
}

/**
* Writes a ZIP file to a [BufferedSink].
*/
private inline fun <R> BufferedSink.writeZip(action: ZipOutputStream.() -> R): R {
return ZipOutputStream(outputStream()).use(action)
}

/**
* Adds a new ZIP entry named [name], populates it with [action], and closes the entry.
*/
private inline fun <R> ZipOutputStream.putEntry(name: String, action: BufferedSink.() -> R): R {
putNextEntry(ZipEntry(name).apply { time = 0L })
val sink = sink().buffer()
return try {
sink.action()
} finally {
sink.flush()
closeEntry()
}
}

0 comments on commit ce4df5e

Please sign in to comment.