JDK-8193516 : Garbage data is added to java.io.DeleteOnExitHook from WDataTransferer, which leads to my files being deleted unexpectedly
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7,8,9,10,15,16,17
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: other
  • CPU: x86
  • Submitted: 2017-12-08
  • Updated: 2021-07-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "9.0.1" 
Java(TM) SE Runtime Environment (build 9.0.1+11) 
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode) 

ADDITIONAL OS VERSION INFORMATION :
Any Windows version

A DESCRIPTION OF THE PROBLEM :
WDataTransferer.translateBytesOrStream puts relative paths on deleteOnExit list which has caused my files (of the same name) to be deleted unexpectedly.

I have worked around this issue by modifying the "files" field in java.io.DeleteOnExitHook using reflection.  The problem is that JDK 9 is starting the process of deprecating reflection.  Therefore my workaround will become an error soon.

This is basically the same problem as:
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8074212

The above bug was closed without being fixed, so I am still experiencing the underlying problem.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a new directory called "Test" with the following files:
    testJavaBug.java
    foo.txt

2. open Command Prompt within this directory

3. Execute the following command in the Command Prompt 
    javac testJavaBug.java

4. Create a compressed directory with "foo.txt" within this directory by selecting "foo.txt", right-clicking, selecting "Send to", and then "Compressed (zipped) folder"

5. Double-click (or right-click > Open) the new .zip folder

6. Right-click on "foo.txt" and select "Copy"

7. Navigate Windows Explorer back to the "Test" directory (this will allow you to see the file disappear on JVM shutdown)

8. Return to the Command Prompt and execute the following line:
    java testJavaBug

9. Notice that the Delete On Exit list (printed to the command line) was populated with a bunch of garbage, including the entry "foo.txt"

10. Notice that the file "foo.txt" was deleted from the "Test" directory

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1. WDataTransferer.translateBytesOrStream SHOULD NOT add garbage or relative paths to java.io.DeleteOnExitHooks "files" field.

2. Local files should not be deleted even if relative paths do make it into the "files" field of java.io.DeleteOnExitHook.
ACTUAL -
1. Garbage data is added to the "files" field of java.io.DeleteOnExitHook.

2. Local files which share the same name as the garbage in the the "files" field of java.io.DeleteOnExitHook are deleted on JVM shutdown.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.util.List;
import java.awt.datatransfer.Clipboard;
import java.lang.reflect.Field;
import java.util.Set;

public class testJavaBug {
    public static void main(String[] args) {
        try {
            // garbage data is added to "files" field in DeleteOnExitHook whether using getTransferData or Clipboard.getData
		
            Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
            Object data = transferable.getTransferData( DataFlavor.javaFileListFlavor );
         
            // The following will print out the list of files to be deleted on JVM shutdown
            Class c = Class.forName("java.io.DeleteOnExitHook");
            Field f = c.getDeclaredField("files");
            f.setAccessible(true);
            System.out.println((Set<String>)f.get(null));
        
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Currently, I am working around this issue by modifying the "files" field of java.io.DeleteOnExitHook using reflection.


Comments
Reproduced the issue on the latest JDK and looking for a right resolution.
09-01-2018

Reported with: ============ JDK 9.0.1 Windows WDataTransferer.translateBytesOrStream puts relative paths on deleteOnExit list which has caused my files (of the same name) to be deleted unexpectedly. ================= Checked this with reported as well as ea builds and could confirm the issue as reported. WDataTransferer.translateBytesOrStream adds garbage or relative paths to java.io.DeleteOnExitHooks "files" field and local files get deleted even if relative paths do make it into the "files" field of java.io.DeleteOnExitHook. Results: ========= 7u80: FAIL JDK 8: FAIL JDK 8u152: FAIL 9: FAIL 9.0.1: FAIL 10: FAIL Steps to reproduce: STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : 1. Create a new directory called "Test" with the following files: testJavaBug.java foo.txt 2. open Command Prompt within this directory 3. Execute the following command in the Command Prompt javac testJavaBug.java 4. Create a compressed directory with "foo.txt" within this directory by selecting "foo.txt", right-clicking, selecting "Send to", and then "Compressed (zipped) folder" 5. Double-click (or right-click > Open) the new .zip folder 6. Right-click on "foo.txt" and select "Copy" 7. Navigate Windows Explorer back to the "Test" directory (this will allow you to see the file disappear on JVM shutdown) 8. Return to the Command Prompt and execute the following line: java testJavaBug 9. Notice that the Delete On Exit list (printed to the command line) was populated with a bunch of garbage, including the entry "foo.txt"
14-12-2017