Skip to content

Commit 3131dd1

Browse files
author
Harshitha Onkar
committed
8346465: Add a check in setData() to restrict the update of Built-In ICC_Profiles
Reviewed-by: aivanov, jdv, prr, serb
1 parent af5db51 commit 3131dd1

File tree

7 files changed

+228
-23
lines changed

7 files changed

+228
-23
lines changed

Diff for: src/java.desktop/share/classes/java/awt/color/ICC_Profile.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,23 @@ public sealed class ICC_Profile implements Serializable
107107
*/
108108
private transient volatile ProfileDeferralInfo deferralInfo;
109109

110+
111+
/**
112+
* Set to {@code true} for {@code BuiltInProfile}, {@code false} otherwise.
113+
* This flag is used in {@link #setData(int, byte[])} to prevent modifying
114+
* built-in profiles.
115+
*/
116+
private final transient boolean builtIn;
117+
110118
/**
111119
* The lazy registry of singleton profile objects for specific built-in
112120
* color spaces defined in the ColorSpace class (e.g. CS_sRGB),
113121
* see getInstance(int cspace) factory method.
114122
*/
115123
private interface BuiltInProfile {
116124
/*
117-
* Deferral is only used for standard profiles. Enabling the appropriate
118-
* access privileges is handled at a lower level.
125+
* ProfileDeferralInfo is used for built-in profile creation only,
126+
* and all built-in profiles should be constructed using it.
119127
*/
120128
ICC_Profile SRGB = new ICC_ProfileRGB(new ProfileDeferralInfo(
121129
"sRGB.pf", ColorSpace.TYPE_RGB, 3, CLASS_DISPLAY));
@@ -763,14 +771,20 @@ private interface BuiltInProfile {
763771
*/
764772
ICC_Profile(Profile p) {
765773
cmmProfile = p;
774+
builtIn = false;
766775
}
767776

768777
/**
769778
* Constructs an {@code ICC_Profile} object whose loading will be deferred.
770779
* The ID will be 0 until the profile is loaded.
780+
*
781+
* <p>
782+
* Note: {@code ProfileDeferralInfo} is used for built-in profile
783+
* creation only, and all built-in profiles should be constructed using it.
771784
*/
772785
ICC_Profile(ProfileDeferralInfo pdi) {
773786
deferralInfo = pdi;
787+
builtIn = true;
774788
}
775789

776790
/**
@@ -1131,17 +1145,34 @@ private static byte[] getData(Profile p, int tagSignature) {
11311145
* This method is useful for advanced applications which need to access
11321146
* profile data directly.
11331147
*
1148+
* <p>
1149+
* Note: JDK built-in ICC Profiles cannot be updated using this method
1150+
* as it will result in {@code IllegalArgumentException}. JDK built-in
1151+
* profiles are those obtained by {@code ICC_Profile.getInstance(int colorSpaceID)}
1152+
* where {@code colorSpaceID} is one of the following:
1153+
* {@link ColorSpace#CS_sRGB}, {@link ColorSpace#CS_LINEAR_RGB},
1154+
* {@link ColorSpace#CS_PYCC}, {@link ColorSpace#CS_GRAY} or
1155+
* {@link ColorSpace#CS_CIEXYZ}.
1156+
*
11341157
* @param tagSignature the ICC tag signature for the data element you want
11351158
* to set
11361159
* @param tagData the data to set for the specified tag signature
11371160
* @throws IllegalArgumentException if {@code tagSignature} is not a
11381161
* signature as defined in the ICC specification.
1139-
* @throws IllegalArgumentException if a content of the {@code tagData}
1162+
* @throws IllegalArgumentException if the content of the {@code tagData}
11401163
* array can not be interpreted as valid tag data, corresponding to
11411164
* the {@code tagSignature}
1165+
* @throws IllegalArgumentException if this is a built-in profile for one
1166+
* of the pre-defined color spaces, that is those which can be obtained
1167+
* by calling {@code ICC_Profile.getInstance(int colorSpaceID)}
11421168
* @see #getData
1169+
* @see ColorSpace
11431170
*/
11441171
public void setData(int tagSignature, byte[] tagData) {
1172+
if (builtIn) {
1173+
throw new IllegalArgumentException("Built-in profile cannot be modified");
1174+
}
1175+
11451176
if (tagSignature == ICC_Profile.icSigHead) {
11461177
verifyHeader(tagData);
11471178
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8346465
27+
* @summary Tests if setData() throws IAE for BuiltIn profiles
28+
*/
29+
30+
import java.awt.color.ColorSpace;
31+
import java.awt.color.ICC_Profile;
32+
import java.io.ByteArrayInputStream;
33+
import java.io.ByteArrayOutputStream;
34+
import java.io.FileInputStream;
35+
import java.io.ObjectInputStream;
36+
import java.io.ObjectOutputStream;
37+
import java.util.Map;
38+
39+
public class BuiltInProfileCheck {
40+
private static final int HEADER_TAG = ICC_Profile.icSigHead;
41+
private static final int INDEX = ICC_Profile.icHdrDeviceClass;
42+
private static final String EXCEPTION_MSG = "Built-in profile cannot be modified";
43+
/**
44+
* {@link #prepareTestProfile(String, boolean, int)}
45+
* stores the profile to test in testProfile.
46+
*/
47+
private static ICC_Profile testProfile;
48+
49+
private static final Map<Integer, String> colorSpace = Map.of(
50+
ColorSpace.CS_sRGB, "CS_sRGB",
51+
ColorSpace.CS_PYCC, "CS_PYCC",
52+
ColorSpace.CS_GRAY, "CS_GRAY",
53+
ColorSpace.CS_CIEXYZ, "CS_CIEXYZ",
54+
ColorSpace.CS_LINEAR_RGB, "CS_LINEAR_RGB"
55+
);
56+
57+
public static void main(String[] args) throws Exception {
58+
System.out.println("CASE 1: Testing BuiltIn Profile");
59+
for (int cs : colorSpace.keySet()) {
60+
prepareTestProfile("Default", true, cs);
61+
testProfile(true, cs);
62+
}
63+
System.out.println("Passed\n");
64+
65+
System.out.println("CASE 2: Testing Custom Profile");
66+
prepareTestProfile("Default", false, ColorSpace.CS_sRGB);
67+
testProfile(false, ColorSpace.CS_sRGB);
68+
System.out.println("Passed\n");
69+
70+
System.out.println("CASE 3: Testing Built-In Profile"
71+
+ " Serialization & Deserialization");
72+
for (int cs : colorSpace.keySet()) {
73+
prepareTestProfile("Serialize", true, cs);
74+
testProfile(true, cs);
75+
}
76+
System.out.println("Passed\n");
77+
78+
System.out.println("CASE 4: Testing Custom Profile"
79+
+ " Serialization & Deserialization");
80+
prepareTestProfile("Serialize", false, ColorSpace.CS_sRGB);
81+
testProfile(false, ColorSpace.CS_sRGB);
82+
System.out.println("Passed\n");
83+
84+
System.out.println("CASE 5: Test reading Built-In profile from .icc file");
85+
prepareTestProfile("ReadFromFile", true, ColorSpace.CS_sRGB);
86+
testProfile(true, ColorSpace.CS_sRGB);
87+
System.out.println("Passed\n");
88+
89+
System.out.println("CASE 6: Test reading Custom profile from .icc file");
90+
prepareTestProfile("ReadFromFile", false, ColorSpace.CS_sRGB);
91+
testProfile(false, ColorSpace.CS_sRGB);
92+
System.out.println("Passed\n");
93+
}
94+
95+
private static void prepareTestProfile(String testCase,
96+
boolean isBuiltIn, int cs) {
97+
ICC_Profile builtInProfile = ICC_Profile.getInstance(cs);
98+
// if isBuiltIn=true use builtInProfile else create a copy
99+
testProfile = isBuiltIn
100+
? builtInProfile
101+
: ICC_Profile.getInstance(builtInProfile.getData());
102+
103+
switch (testCase) {
104+
case "Default" -> {
105+
// empty case block
106+
// no further processing of testProfile required for default case
107+
}
108+
case "Serialize" -> {
109+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
110+
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
111+
oos.writeObject(testProfile);
112+
113+
byte[] array = baos.toByteArray();
114+
try (ObjectInputStream ois =
115+
new ObjectInputStream(new ByteArrayInputStream(array))) {
116+
testProfile = (ICC_Profile) ois.readObject();
117+
}
118+
} catch (Exception e) {
119+
throw new RuntimeException("Test Failed ! Serial-Deserialization"
120+
+ " case failed", e);
121+
}
122+
}
123+
case "ReadFromFile" -> {
124+
// .icc files serialized on older JDK version
125+
String filename = isBuiltIn ? "builtIn.icc" : "custom.icc";
126+
String testDir = System.getProperty("test.src")
127+
+ System.getProperty("file.separator");
128+
filename = testDir + filename;
129+
130+
try (FileInputStream fileIn = new FileInputStream(filename);
131+
ObjectInputStream ois = new ObjectInputStream(fileIn)) {
132+
testProfile = (ICC_Profile) ois.readObject();
133+
} catch (Exception e) {
134+
throw new RuntimeException("Test Failed ! Unable to fetch"
135+
+ " .icc files", e);
136+
}
137+
}
138+
}
139+
}
140+
141+
private static void testProfile(boolean isBuiltIn, int cs) {
142+
byte[] headerData = testProfile.getData(HEADER_TAG);
143+
// Set profile class to valid icSigInputClass = 0x73636E72
144+
headerData[INDEX] = 0x73;
145+
headerData[INDEX + 1] = 0x63;
146+
headerData[INDEX + 2] = 0x6E;
147+
headerData[INDEX + 3] = 0x72;
148+
149+
if (isBuiltIn) {
150+
System.out.println("Testing: " + colorSpace.get(cs));
151+
try {
152+
// Try updating a built-in profile, IAE is expected
153+
testProfile.setData(HEADER_TAG, headerData);
154+
throw new RuntimeException("Test Failed! IAE NOT thrown for profile "
155+
+ colorSpace.get(cs));
156+
} catch (IllegalArgumentException iae) {
157+
if (!iae.getMessage().equals(EXCEPTION_MSG)) {
158+
throw new RuntimeException("Test Failed! IAE with exception msg \""
159+
+ EXCEPTION_MSG + "\" NOT thrown for profile "
160+
+ colorSpace.get(cs));
161+
}
162+
}
163+
} else {
164+
// Modifying custom profile should NOT throw IAE
165+
testProfile.setData(HEADER_TAG, headerData);
166+
}
167+
}
168+
}
Binary file not shown.
Binary file not shown.

Diff for: test/jdk/java/awt/color/ICC_Profile/SetHeaderInfo.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, 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
@@ -37,7 +37,9 @@ public static void main(String[] args) {
3737
ColorSpace.CS_CIEXYZ, ColorSpace.CS_PYCC,
3838
ColorSpace.CS_GRAY};
3939
for (int cspace : cspaces) {
40-
ICC_Profile icc = ICC_Profile.getInstance(cspace);
40+
ICC_Profile builtInProfile = ICC_Profile.getInstance(cspace);
41+
ICC_Profile icc = ICC_Profile.getInstance(builtInProfile.getData());
42+
4143
testSame(icc);
4244
testCustom(icc);
4345
// some corner cases

Diff for: test/jdk/java/awt/color/ICC_ProfileSetNullDataTest.java

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2025, 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
@@ -23,29 +23,33 @@
2323

2424
import java.awt.color.ColorSpace;
2525
import java.awt.color.ICC_Profile;
26+
import java.util.Map;
2627

2728
/**
2829
* @test
2930
* @bug 4823896 7042594
3031
* @summary Test checks behavior of the ICC_Profile.setData(int, byte[])
3132
*/
3233
public final class ICC_ProfileSetNullDataTest {
34+
private static final Map<Integer, String> colorSpace = Map.of(
35+
ColorSpace.CS_sRGB, "CS_sRGB",
36+
ColorSpace.CS_PYCC, "CS_PYCC",
37+
ColorSpace.CS_GRAY, "CS_GRAY",
38+
ColorSpace.CS_CIEXYZ, "CS_CIEXYZ",
39+
ColorSpace.CS_LINEAR_RGB, "CS_LINEAR_RGB"
40+
);
3341

3442
public static void main(String[] args) {
35-
test(ICC_Profile.getInstance(ColorSpace.CS_sRGB));
36-
test(ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB));
37-
test(ICC_Profile.getInstance(ColorSpace.CS_CIEXYZ));
38-
test(ICC_Profile.getInstance(ColorSpace.CS_PYCC));
39-
test(ICC_Profile.getInstance(ColorSpace.CS_GRAY));
40-
}
41-
42-
private static void test(ICC_Profile profile) {
43-
byte[] tagData = null;
44-
try {
45-
profile.setData(ICC_Profile.icSigCmykData, tagData);
46-
} catch (IllegalArgumentException e) {
47-
return;
43+
for (int cs : colorSpace.keySet()) {
44+
ICC_Profile builtInProfile = ICC_Profile.getInstance(cs);
45+
ICC_Profile profile = ICC_Profile.getInstance(builtInProfile.getData());
46+
try {
47+
profile.setData(ICC_Profile.icSigCmykData, null);
48+
throw new RuntimeException("IAE expected, but not thrown for "
49+
+ "ColorSpace: " + colorSpace.get(cs));
50+
} catch (IllegalArgumentException e) {
51+
// IAE expected
52+
}
4853
}
49-
throw new RuntimeException("IllegalArgumentException expected");
5054
}
5155
}

Diff for: test/jdk/sun/java2d/cmm/ProfileOp/SetDataTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, 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
@@ -29,7 +29,6 @@
2929
* @run main SetDataTest
3030
*/
3131

32-
3332
import java.util.ArrayList;
3433
import java.util.List;
3534
import java.awt.color.ICC_Profile;
@@ -47,7 +46,8 @@ static class TestCase {
4746
static byte[] invalidTRCData;
4847

4948
static {
50-
profile = ICC_Profile.getInstance(CS_GRAY);
49+
ICC_Profile builtInProfile = ICC_Profile.getInstance(CS_GRAY);
50+
profile = ICC_Profile.getInstance(builtInProfile.getData());
5151
validTRCdata = profile.getData(icSigGrayTRCTag);
5252
invalidTRCData = new byte[]{0x42, 0x42, 0x42, 0x42, 1, 3, 4, 6,};
5353
}

0 commit comments

Comments
 (0)