Skip to content

Commit 1acc508

Browse files
committed
8274939: Incorrect size of the pixel storage is used by the robot on macOS
Reviewed-by: clanger Backport-of: eff5dafba9f72bd0612357712ffa472ce1c9166a
1 parent 3990eb2 commit 1acc508

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CRobot.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2022, 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
@@ -169,9 +169,9 @@ public void keyRelease(final int keycode) {
169169
*/
170170
@Override
171171
public int getRGBPixel(int x, int y) {
172-
int c[] = new int[1];
173-
double scale = fDevice.getScaleFactor();
174-
getScreenPixels(new Rectangle(x, y, (int) scale, (int) scale), c);
172+
int scale = fDevice.getScaleFactor();
173+
int[] c = new int[scale * scale];
174+
getScreenPixels(new Rectangle(x, y, scale, scale), c);
175175
return c[0];
176176
}
177177

src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2022, 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
@@ -321,6 +321,11 @@ static inline void autoDelay(BOOL isMove) {
321321
jint picY = y;
322322
jint picWidth = width;
323323
jint picHeight = height;
324+
jsize size = (*env)->GetArrayLength(env, pixels);
325+
if (size < (long) picWidth * picHeight || picWidth < 0 || picHeight < 0) {
326+
JNU_ThrowInternalError(env, "Invalid arguments to get screen pixels");
327+
return;
328+
}
324329

325330
CGRect screenRect = CGRectMake(picX / scale, picY / scale,
326331
picWidth / scale, picHeight / scale);

test/jdk/java/awt/Robot/CheckCommonColors/CheckCommonColors.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
* @key headful
4343
* @bug 8215105 8211999
4444
* @summary tests that Robot can capture the common colors without artifacts
45+
* @run main/othervm CheckCommonColors
46+
* @run main/othervm -Xcheck:jni CheckCommonColors
4547
*/
4648
public final class CheckCommonColors {
4749

0 commit comments

Comments
 (0)