From d4a4169ea4480bf5fc601b13cb276bce8bdacf69 Mon Sep 17 00:00:00 2001 From: Karl Nelson Date: Sat, 5 Sep 2026 11:54:00 -0700 Subject: [PATCH] Fix issue #8: ColorType(int,int,int,int) sign-extends on high alpha Packed ARGB components via 32-bit int arithmetic and only then widened to the long-typed color_value field. With alpha >= 0x80 the packed int was negative, and Java's implicit int->long widening sign-extended it, so getColor() returned a value with the long's upper 32 bits all set to 1 instead of the plain unsigned 32-bit ARGB value the field's own Javadoc describes. Fixed by packing in long-typed arithmetic and masking to 32 bits. Adds CefSettingsTest as a regression test. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01HBFmQs6JDypYP5qdC99yDq --- java/org/cef/CefSettings.java | 9 +++++++- java/tests/junittests/CefSettingsTest.java | 27 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 java/tests/junittests/CefSettingsTest.java diff --git a/java/org/cef/CefSettings.java b/java/org/cef/CefSettings.java index e956cce1..ac1884ef 100644 --- a/java/org/cef/CefSettings.java +++ b/java/org/cef/CefSettings.java @@ -60,7 +60,14 @@ public class ColorType { private ColorType() {} public ColorType(int alpha, int red, int green, int blue) { - color_value = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0); + // Pack in long-typed arithmetic and mask to 32 bits -- packing in + // int arithmetic first (as this used to) sign-extends into the + // long's upper 32 bits whenever alpha >= 0x80 (the sign bit of + // the intermediate 32-bit result), instead of the plain unsigned + // 32-bit ARGB value this field's own Javadoc describes. See + // Thrameos/java-cef#8. + color_value = ((long) alpha << 24 | (long) red << 16 | (long) green << 8 | blue) + & 0xFFFFFFFFL; } public long getColor() { diff --git a/java/tests/junittests/CefSettingsTest.java b/java/tests/junittests/CefSettingsTest.java new file mode 100644 index 00000000..c190bdf2 --- /dev/null +++ b/java/tests/junittests/CefSettingsTest.java @@ -0,0 +1,27 @@ +// Copyright (c) 2026 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that +// can be found in the LICENSE file. + +package tests.junittests; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.cef.CefSettings; +import org.junit.jupiter.api.Test; + +// Plain Java value object -- no CEF native lifecycle involved. +class CefSettingsTest { + @Test + void colorTypeHighAlphaDoesNotSignExtend() { + CefSettings settings = new CefSettings(); + CefSettings.ColorType color = settings.new ColorType(0xFF, 0x11, 0x22, 0x33); + assertEquals(0xFF112233L, color.getColor()); + } + + @Test + void colorTypeLowAlpha() { + CefSettings settings = new CefSettings(); + CefSettings.ColorType color = settings.new ColorType(0x7F, 0x11, 0x22, 0x33); + assertEquals(0x7F112233L, color.getColor()); + } +}