Skip to content

Commit fd57723

Browse files
committed
8386485: JFR: RecordingFile::write overwrites original file
Reviewed-by: mgronlun
1 parent c690bfa commit fd57723

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingFile.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
2929
import java.io.EOFException;
3030
import java.io.File;
3131
import java.io.IOException;
32+
import java.nio.file.Files;
3233
import java.nio.file.NoSuchFileException;
3334
import java.nio.file.Path;
3435
import java.util.ArrayList;
@@ -221,9 +222,10 @@ public void close() throws IOException {
221222
*
222223
* @param filter filter that determines if an event should be included, not
223224
* {@code null}
224-
* @throws IOException if an I/O error occurred, it's not a Flight
225-
* Recorder file or a version of a JFR file that can't
226-
* be parsed
225+
* @throws IOException if an I/O error occurred, if {@code destination} is the
226+
* same file as the input file for this
227+
* {@code RecordingFile}, if it's not a Flight Recorder file
228+
* or a version of a JFR file that can't be parsed
227229
*
228230
* @since 19
229231
*/
@@ -235,6 +237,9 @@ public void write(Path destination, Predicate<RecordedEvent> filter) throws IOEx
235237

236238
// package private
237239
List<RemovedEvents> write(Path destination, Predicate<RecordedEvent> filter, boolean collectResults) throws IOException {
240+
if (Files.exists(destination) && Files.isSameFile(destination, file.toPath())) {
241+
throw new IOException("Destination file can't be the same as the input file: " + destination.toAbsolutePath());
242+
}
238243
try (ChunkWriter cw = new ChunkWriter(file.toPath(), destination, filter, collectResults)) {
239244
try (RecordingFile rf = new RecordingFile(cw)) {
240245
while (rf.hasMoreEvents()) {

test/jdk/jdk/jfr/api/consumer/TestRecordingFileWrite.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public static void main(String... args) throws Exception {
6464
throw new AssertionError("Expected at least 50 000 events to be included");
6565
}
6666
verify(scrubbed, ids);
67+
try {
68+
scrubRecording(original, original);
69+
throw new AssertionError("Expected IOException when overwriting");
70+
} catch (IOException e) {
71+
System.out.println(e);
72+
if (!e.getMessage().contains("Destination file can't be the same as the input file")) {
73+
throw new AssertionError("Unexpected error message " + e);
74+
}
75+
}
6776
}
6877

6978
private static void verify(Path scrubbed, Queue<String> events) throws Exception {

0 commit comments

Comments
 (0)