Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8274929: Crash while reading specific clipboard content
Backport-of: effcc86667f0ed7cf2899384fbc5fd97b8c9f7b5
  • Loading branch information
kevinrushforth committed Nov 12, 2021
1 parent 5f9d973 commit 99d138d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
Expand Up @@ -1302,7 +1302,10 @@ JNIEXPORT jobjectArray JNICALL Java_com_sun_glass_ui_win_WinSystemClipboard_popM
//LPFILEGROUPDESCRIPTORW for MS_FILE_DESCRIPTOR_UNICODE
//LPFILEGROUPDESCRIPTORA for MS_FILE_DESCRIPTOR
LPFILEGROUPDESCRIPTORW pdata = reinterpret_cast<LPFILEGROUPDESCRIPTORW>(me.getMem());
if (pdata->cItems > 0) {
jlong bufferSize = me.size() - sizeof(UINT);
if ((pdata->cItems > 0) &&
(bufferSize == (jlong)pdata->cItems * itemSize))
{
mimes.erase(MS_FILE_CONTENT);
mimes.erase(MS_FILE_DESCRIPTOR_UNICODE);
mimes.erase(MS_FILE_DESCRIPTOR);
Expand Down
2 changes: 2 additions & 0 deletions tests/system/src/test/addExports
@@ -1,6 +1,8 @@
#
--add-exports javafx.base/com.sun.javafx=ALL-UNNAMED
#
--add-exports java.desktop/sun.awt.datatransfer=ALL-UNNAMED
#
--add-exports javafx.graphics/com.sun.glass.ui=ALL-UNNAMED
--add-exports javafx.graphics/com.sun.glass.ui.monocle=ALL-UNNAMED
--add-exports javafx.graphics/com.sun.javafx.animation=ALL-UNNAMED
Expand Down
112 changes: 109 additions & 3 deletions tests/system/src/test/java/test/javafx/scene/input/ClipboardTest.java
Expand Up @@ -24,6 +24,7 @@
*/
package test.javafx.scene.input;

import com.sun.javafx.PlatformUtil;
import javafx.application.Platform;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
Expand All @@ -33,14 +34,18 @@
import org.junit.Test;
import test.util.Util;

import java.awt.*;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertEquals;
import sun.awt.datatransfer.ClipboardTransferable;
import sun.awt.datatransfer.SunClipboard;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;


public class ClipboardTest {
Expand Down Expand Up @@ -114,8 +119,109 @@ public void testPasteUTF8String() throws Exception {
assertEquals(text, clipboard.getContent(DataFormat.PLAIN_TEXT)));
}

/*
* @bug 8274929
*/
@Test
public void testCustomClipboard() throws Exception {
// Test is only valid on Windows
assumeTrue(PlatformUtil.isWindows());

// Put custom content on the system clipboard (via AWT)
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new ClipboardTransferable(new CustomClipboard()), null);

Util.runAndWait(() -> {
// Read the system Clipboard
boolean hasString = clipboard.hasString();
assertFalse(hasString);
});
}

@AfterClass
public static void teardown() {
Platform.exit();
}

private static final class CustomClipboard extends SunClipboard {

private static final int CF_HDROP = 15;

// Custom Drag and drop data to put on the system clipboard (using AWT)
private static final byte[] CF_HDROP_BYTES = new byte[] {
// HEADER
0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
// D:\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0x44, 0x00, 0x3A, 0x00, 0x5C, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00,
// terminate entry
0x00, 0x00,
// terminate content
0x00, 0x00,
};

public CustomClipboard() {
super("CustomClipboard");
}

@Override
public long getID() {
return 0;
}

@Override
protected void clearNativeContext() {
// no-op
}

@Override
protected void setContentsNative(final Transferable transferable) {
// no-op
}

@Override
protected long[] getClipboardFormats() {
return new long[] { CF_HDROP };
}

@Override
protected byte[] getClipboardData(final long id) {
if (id == CF_HDROP) {
return CF_HDROP_BYTES;
} else {
return new byte[0];
}
}

@Override
protected void registerClipboardViewerChecked() {
// no-op
}

@Override
protected void unregisterClipboardViewerChecked() {
// no-op
}

}

}

1 comment on commit 99d138d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.