From 266f2e8377d5840b0b2d778876f8706f311c48f7 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Wed, 19 Aug 2026 16:04:16 +0200 Subject: [PATCH] Guard FontRegistry.createFont against filterData returning null filterData() is documented to return null for an empty font list, and does so, but createFont() dereferences its result unconditionally to check for a zero length. Registering an empty FontData[] under a symbolic name and then looking that name up therefore fails with java.lang.NullPointerException: Cannot read the array length because "validData" is null instead of falling back to the default font the way an unresolvable name otherwise does. Treat null like the empty result it stands for, and add a regression test registering an empty FontData[] and asserting the default-font fallback. Note that filterData() never actually returns a zero-length array - it falls back to the first entry when nothing matches - so the existing length check alone was dead code. Assisted-by: Claude Opus 5 --- .../org/eclipse/jface/resource/FontRegistry.java | 2 +- .../jface/tests/resources/FontRegistryTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java index 9cf6626338b..56ce389a29f 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java @@ -497,7 +497,7 @@ private FontRecord createFont(String symbolicName, FontData[] fonts) { } FontData[] validData = filterData(fonts, display); - if (validData.length == 0) { + if (validData == null || validData.length == 0) { //Nothing specified return null; } diff --git a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/resources/FontRegistryTest.java b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/resources/FontRegistryTest.java index 71044aaded4..ccca67ead11 100644 --- a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/resources/FontRegistryTest.java +++ b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/resources/FontRegistryTest.java @@ -187,6 +187,20 @@ public void get_forNameThatWasNeverRegistered_returnsDefaultFontAndIsStableAfter assertSame(first, second); } + @Test + public void get_forNameRegisteredWithoutAnyFontData_returnsDefaultFont() { + FontRegistry fontRegistry = new FontRegistry(); + // an empty array leaves nothing to filter, so filterData() yields no usable data at all + fontRegistry.put("fontWithoutData", new FontData[0]); + + Font first = fontRegistry.get("fontWithoutData"); + Font second = fontRegistry.get("fontWithoutData"); + + assertSame(fontRegistry.get(JFaceResources.DEFAULT_FONT), first, + "a name that cannot be resolved to any font data must fall back to the default font"); + assertSame(first, second); + } + @Test public void getBoldAndGetItalic_returnSameInstanceOnRepeatedCalls() { FontRegistry fontRegistry = new FontRegistry();