This is an automated email from the ASF dual-hosted git repository.

ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new f3bdb7e7aa [ISSUE #11500] Prevent copying a file onto itself (#11501)
f3bdb7e7aa is described below

commit f3bdb7e7aaf9707cb01d88c7cff9b9d99033a359
Author: Drifter <[email protected]>
AuthorDate: Mon Nov 23 21:19:11 2026 +0800

    [ISSUE #11500] Prevent copying a file onto itself (#11501)
---
 .../apache/rocketmq/common/utils/IOTinyUtils.java  |  4 ++
 .../common/utils/IOTinyUtilsSameFileTest.java      | 75 ++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git 
a/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java 
b/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java
index acba540d3a..0aa91b8f9c 100644
--- a/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java
+++ b/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java
@@ -30,6 +30,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.nio.channels.FileChannel;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -81,6 +82,9 @@ public class IOTinyUtils {
             throw new IllegalArgumentException("source file does not exist.");
         }
         File tf = new File(target);
+        if (tf.exists() && Files.isSameFile(sf.toPath(), tf.toPath())) {
+            throw new IOException("source and target refer to the same file");
+        }
         tf.getParentFile().mkdirs();
         if (!tf.exists() && !tf.createNewFile()) {
             throw new RuntimeException("failed to create target file.");
diff --git 
a/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsSameFileTest.java
 
b/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsSameFileTest.java
new file mode 100644
index 0000000000..64da6ed05f
--- /dev/null
+++ 
b/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsSameFileTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.rocketmq.common.utils;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.junit.Assume;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.fail;
+
+public class IOTinyUtilsSameFileTest {
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Test
+    public void copyFileToItselfDoesNotDestroySource() throws Exception {
+        Path source = folder.newFile("source").toPath();
+        assertCopyRejected(source, source);
+    }
+
+    @Test
+    public void copyFileToHardLinkDoesNotDestroySource() throws Exception {
+        Path source = folder.newFile("source").toPath();
+        Path link = source.resolveSibling("link");
+        try {
+            Files.createLink(link, source);
+        } catch (IOException | UnsupportedOperationException e) {
+            Assume.assumeNoException("Hard links are unavailable on this 
filesystem", e);
+        }
+        assertCopyRejected(source, link);
+    }
+
+    @Test
+    public void copyFileToDifferentFileStillWorks() throws Exception {
+        Path source = folder.newFile("source").toPath();
+        Path target = folder.newFile("target").toPath();
+        byte[] data = "important data".getBytes(StandardCharsets.UTF_8);
+        Files.write(source, data);
+        IOTinyUtils.copyFile(source.toString(), target.toString());
+        assertArrayEquals(data, Files.readAllBytes(target));
+    }
+
+    private void assertCopyRejected(Path source, Path target) throws Exception 
{
+        byte[] data = "important data".getBytes(StandardCharsets.UTF_8);
+        Files.write(source, data);
+        try {
+            IOTinyUtils.copyFile(source.toString(), target.toString());
+            assertArrayEquals(data, Files.readAllBytes(source));
+            fail("Expected an IOException for identical files");
+        } catch (IOException expected) {
+            assertArrayEquals(data, Files.readAllBytes(source));
+        }
+    }
+}

Reply via email to