Skip to content

Commit 926391e

Browse files
Alexei VoitylovRealCLanger
authored andcommitted
8323390: Enhance mask blit functionality
Reviewed-by: mbalao Backport-of: 895893f2fbf1c521d4a263d505f0ecbda8d2eeea
1 parent f8546cb commit 926391e

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2024, 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
@@ -2151,27 +2151,33 @@ private void doCopyArea(int x, int y, int w, int h, int dx, int dy) {
21512151
}
21522152

21532153
Blit ob = lastCAblit;
2154-
if (dy == 0 && dx > 0 && dx < w) {
2155-
while (w > 0) {
2156-
int partW = Math.min(w, dx);
2157-
w -= partW;
2158-
int sx = x + w;
2159-
ob.Blit(theData, theData, comp, clip,
2160-
sx, y, sx+dx, y+dy, partW, h);
2154+
try {
2155+
if (dy == 0 && dx > 0 && dx < w) {
2156+
while (w > 0) {
2157+
int partW = Math.min(w, dx);
2158+
w -= partW;
2159+
int sx = Math.addExact(x, w);
2160+
ob.Blit(theData, theData, comp, clip,
2161+
sx, y, sx+dx, y+dy, partW, h);
2162+
}
2163+
return;
21612164
}
2162-
return;
2163-
}
2164-
if (dy > 0 && dy < h && dx > -w && dx < w) {
2165-
while (h > 0) {
2166-
int partH = Math.min(h, dy);
2167-
h -= partH;
2168-
int sy = y + h;
2169-
ob.Blit(theData, theData, comp, clip,
2170-
x, sy, x+dx, sy+dy, w, partH);
2165+
if (dy > 0 && dy < h && dx > -w && dx < w) {
2166+
while (h > 0) {
2167+
int partH = Math.min(h, dy);
2168+
h -= partH;
2169+
int sy = Math.addExact(y, h);
2170+
ob.Blit(theData, theData, comp, clip,
2171+
x, sy, Math.addExact(x, dx), sy+dy, w, partH);
2172+
}
2173+
return;
21712174
}
2175+
ob.Blit(theData, theData, comp, clip, x, y,
2176+
Math.addExact(x, dx), Math.addExact(y, dy), w, h);
2177+
} catch (ArithmeticException ex) {
2178+
// We are hitting integer overflow in Math.addExact()
21722179
return;
21732180
}
2174-
ob.Blit(theData, theData, comp, clip, x, y, x+dx, y+dy, w, h);
21752181
}
21762182

21772183
/*

src/java.desktop/share/native/libawt/java2d/SurfaceData.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2024, 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
@@ -32,6 +32,7 @@
3232
#define _Included_SurfaceData
3333

3434
#include <jni.h>
35+
#include <limits.h>
3536

3637
#ifdef __cplusplus
3738
extern "C" {
@@ -53,6 +54,14 @@ typedef struct {
5354

5455
#define SD_RASINFO_PRIVATE_SIZE 64
5556

57+
#define UNSAFE_TO_ADD(a, b) \
58+
(((a >= 0) && (b >= 0) && (a > (INT_MAX - b))) || \
59+
((a < 0) && (b < 0) && (a < (INT_MIN - b)))) \
60+
61+
#define UNSAFE_TO_SUB(a, b) \
62+
(((b >= 0) && (a < 0) && (a < (INT_MIN + b))) || \
63+
((b < 0) && (a >= 0) && (-b > (INT_MAX - a)))) \
64+
5665
/*
5766
* The SurfaceDataRasInfo structure is used to pass in and return various
5867
* pieces of information about the destination drawable. In particular:

src/java.desktop/share/native/libawt/java2d/loops/MaskBlit.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, 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
@@ -68,14 +68,28 @@ Java_sun_java2d_loops_MaskBlit_MaskBlit
6868
return;
6969
}
7070

71+
if (width <= 0 || height <= 0) {
72+
return;
73+
}
74+
7175
srcInfo.bounds.x1 = srcx;
7276
srcInfo.bounds.y1 = srcy;
77+
if (UNSAFE_TO_ADD(srcx, width) ||
78+
UNSAFE_TO_ADD(srcy, height) ||
79+
UNSAFE_TO_ADD(dstx, width) ||
80+
UNSAFE_TO_ADD(dsty, height)) {
81+
return;
82+
}
7383
srcInfo.bounds.x2 = srcx + width;
7484
srcInfo.bounds.y2 = srcy + height;
7585
dstInfo.bounds.x1 = dstx;
7686
dstInfo.bounds.y1 = dsty;
7787
dstInfo.bounds.x2 = dstx + width;
7888
dstInfo.bounds.y2 = dsty + height;
89+
if (UNSAFE_TO_SUB(srcx, dstx) ||
90+
UNSAFE_TO_SUB(srcy, dsty)) {
91+
return;
92+
}
7993
srcx -= dstx;
8094
srcy -= dsty;
8195
SurfaceData_IntersectBounds(&dstInfo.bounds, &clipInfo.bounds);

0 commit comments

Comments
 (0)