Bug 575024 - [JFace] FontRegistry creates invalid FontData arrays
Summary: [JFace] FontRegistry creates invalid FontData arrays
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.21   Edit
Hardware: PC Windows 10
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Platform-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-07-25 16:02 EDT by Guido Schnepp CLA
Modified: 2021-07-26 11:22 EDT (History)
2 users (show)

See Also:


Attachments
Special font definitions for Windows 10 platform (989 bytes, text/plain)
2021-07-25 16:02 EDT, Guido Schnepp CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Guido Schnepp CLA 2021-07-25 16:02:16 EDT
Created attachment 286827 [details]
Special font definitions for Windows 10 platform

org.eclipse.jface*.jar contains properties files in /org/eclipse/jface/resource listing default fonts per platform for specific needs. Attached properties file is from bundle org.eclipse.jface_3.22.100.v20210126-0831.jar (Eclipse 4.21). This file lists two fonts, Consolas and Courier New, for key org.eclipse.jface.textfont (resp. TEXT_FONT).

This file is read on demand by org.eclipse.jface.resource.FontRegistry#readResourceBundle(ResourceBundle, String) into an FontData array with a minimum of eight entries, resulting in two entries having values set and six entries with a value null (code snippet with relevant command):

FontData[] elements = stringToFontData.get(name);
if (elements == null) {
	elements = new FontData[8];
	stringToFontData.put(name, elements);
}

These unneccessary null values result in a NullPointerException in case this FontData array needs to be copied by the o.e.j.r.ArrayFontDescriptor returned by JFaceResources.getTextFontDescriptor(). NPE happens in o.e.j.r.FontDescriptor#copy(FontData[]) function, where null entries are not captured.

I tried this code to get a smaller TEXT_FONT for my control. NPE is thrown in line 2 when a copy of that FontData array is to be created internally:

FontDescriptor descriptor = JFaceResources.getTextFontDescriptor();
               descriptor = descriptor.increaseHeight(-2);
Font           smallFont  = descriptor.createFont(display);

One could argue that NPE should be avoided in FontDescriptor#copy() methods, but having them at all is the result of an FontData array created too big in readResourceBundle, I'd say.
Comment 1 Andrey Loskutov CLA 2021-07-25 16:50:45 EDT
Thanks for report. Do you want to provide a patch?
https://wiki.eclipse.org/Platform/How_to_Contribute
Comment 2 Guido Schnepp CLA 2021-07-26 10:44:51 EDT
Have tried to contribute my first patch, but I feel lost. My commits contains/contained a file not changed by me and not shown in staging but after sending to Gerrit! How can that be? Tried to revert first commit and recommit another, but it feels I messed up everything.  :-(

I have two commits for eclipse.platform.ui now and no clue how to fix that:
- Commit 888b643cf1a272a9bfaaf4306fe6dc69a436056e
- Commit 4331170c6212db607aeb122a086857b96749e65c

Files changed by myself:
bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontDescriptor.java
bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java
tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/resources/FontRegistryTest.java

File not changed by myself but in one of the commits automagically:

bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/ContentTypeDecorator.java
Comment 3 Lars Vogel CLA 2021-07-26 11:22:09 EDT
(In reply to Guido Schnepp from comment #2)
> Have tried to contribute my first patch, but I feel lost. My commits
> contains/contained a file not changed by me and not shown in staging but
> after sending to Gerrit! How can that be? Tried to revert first commit and
> recommit another, but it feels I messed up everything.  :-(
> 
> I have two commits for eclipse.platform.ui now and no clue how to fix that:
> - Commit 888b643cf1a272a9bfaaf4306fe6dc69a436056e
> - Commit 4331170c6212db607aeb122a086857b96749e65c
> 
> Files changed by myself:
> bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontDescriptor.java
> bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java
> tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/resources/
> FontRegistryTest.java
> 
> File not changed by myself but in one of the commits automagically:
> 
> bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/
> ContentTypeDecorator.java

You can use interactive rebase to combine the changes. See https://www.vogella.com/tutorials/EclipseGit/article.html#eclipsegit_interactiverebase

If you want to recreate the whole commit and re-select the files, you can use a soft or mixed reset using the History view.