Skip to content

Commit d8cc8a8

Browse files
committed
8282577: ICC_Profile.setData(int, byte[]) invalidates the profile
Backport-of: f66070b00d4311c6e3a6fbf38956fa2d5da5fada
1 parent ddaa77b commit d8cc8a8

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

src/java.desktop/share/native/liblcms/LCMS.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -797,34 +797,43 @@ static cmsHPROFILE _writeCookedTag(const cmsHPROFILE pfTarget,
797797

798798
// now we have all tags moved to the new profile.
799799
// do some sanity checks: write it to a memory buffer and read again.
800+
void* buf = NULL;
800801
if (cmsSaveProfileToMem(p, NULL, &pfSize)) {
801-
void* buf = malloc(pfSize);
802+
buf = malloc(pfSize);
802803
if (buf != NULL) {
803804
// load raw profile data into the buffer
804805
if (cmsSaveProfileToMem(p, buf, &pfSize)) {
805806
pfSanity = cmsOpenProfileFromMem(buf, pfSize);
806807
}
807-
free(buf);
808808
}
809809
}
810810

811+
cmsCloseProfile(p); // No longer needed.
812+
811813
if (pfSanity == NULL) {
812814
// for some reason, we failed to save and read the updated profile
813815
// It likely indicates that the profile is not correct, so we report
814816
// a failure here.
815-
cmsCloseProfile(p);
816-
p = NULL;
817+
free(buf);
818+
return NULL;
817819
} else {
818820
// do final check whether we can read and handle the target tag.
819821
const void* pTag = cmsReadTag(pfSanity, sig);
820822
if (pTag == NULL) {
821823
// the tag can not be cooked
822-
cmsCloseProfile(p);
823-
p = NULL;
824+
free(buf);
825+
cmsCloseProfile(pfSanity);
826+
return NULL;
824827
}
828+
// The profile we used for sanity checking needs to be returned
829+
// since the one we updated is raw - not cooked.
830+
// Except we want to re-open it since the call to cmsReadTag()
831+
// means we may not get back the same bytes as we set.
832+
// Whilst this may change later anyway, we can at least prevent
833+
// it from happening immediately.
825834
cmsCloseProfile(pfSanity);
826-
pfSanity = NULL;
835+
pfSanity = cmsOpenProfileFromMem(buf, pfSize);
836+
free(buf);
837+
return pfSanity;
827838
}
828-
829-
return p;
830839
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2022, 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 8282577
27+
* @summary Verify setting data for a tag doesn't invalidate the profile.
28+
*/
29+
30+
import java.awt.color.ColorSpace;
31+
import java.awt.color.ICC_ColorSpace;
32+
import java.awt.color.ICC_Profile;
33+
34+
public final class SetTagDataValidation {
35+
36+
public static void main(String[] args) throws Exception {
37+
38+
ICC_Profile srgb = ICC_Profile.getInstance(ColorSpace.CS_sRGB);
39+
// Create a new profile, using the srgb data but private to us.
40+
ICC_Profile icc = ICC_Profile.getInstance(srgb.getData());
41+
42+
// Get data for some tag, which one isn't important so long as it exists
43+
int tag = ICC_Profile.icSigBlueColorantTag;
44+
byte[] tagData = icc.getData(tag);
45+
if (tagData == null) {
46+
throw new RuntimeException("No data for tag");
47+
}
48+
// Set the data to be the SAME data which ought to be a harmless no-op
49+
icc.setData(tag, tagData);
50+
51+
// Perform a color conversion - from rgb to rgb but it doesn't matter
52+
// we just need to verify the op is applied and results are sane.
53+
54+
ColorSpace cs = new ICC_ColorSpace(icc);
55+
float[] in = new float[3];
56+
in[0] = 0.4f;
57+
in[1] = 0.5f;
58+
in[2] = 0.6f;
59+
60+
// the toRGB op previously threw an exception - or crashed
61+
float[] out = cs.toRGB(in);
62+
// If we get this far let's validate the results.
63+
if (out == null || out.length !=3) {
64+
throw new RuntimeException("out array invalid");
65+
}
66+
for (int i=0;i<out.length;i++) {
67+
System.out.println(out[i]);
68+
}
69+
for (int i=0;i<out.length;i++) {
70+
if ((Math.abs(in[i]-out[i]) > 0.01)) {
71+
throw new RuntimeException("Inaccurate no-op conversion");
72+
}
73+
}
74+
}
75+
}

test/jdk/java/awt/color/ICC_Profile/MTGetData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.awt.color.ColorSpace;
2525
import java.awt.color.ICC_Profile;
26+
import java.awt.color.CMMException;
2627
import java.lang.reflect.Field;
2728
import java.lang.reflect.Modifier;
2829
import java.util.ArrayList;
@@ -103,6 +104,7 @@ private static void test(byte[] all, byte[] data1, byte[] data2, int tag)
103104
icc.setData(tag, data2);
104105
}
105106
} catch (IllegalArgumentException ignored) {
107+
System.err.println("Ignoring " + ignored);
106108
} catch (Throwable throwable) {
107109
throwable.printStackTrace();
108110
failed = true;

0 commit comments

Comments
 (0)