8221741: ClassCastException can happen when fontconfig.properties is used

Reviewed-by: mbaesken, itakiguchi
This commit is contained in:
Christoph Langer 2020-02-02 17:41:33 +01:00
parent bbc3d16705
commit 9fb0aeecb6
3 changed files with 94 additions and 32 deletions

View File

@ -129,6 +129,8 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
}
}
private static Font2DHandle FONT_HANDLE_NULL = new Font2DHandle(null);
public static final int FONTFORMAT_NONE = -1;
public static final int FONTFORMAT_TRUETYPE = 0;
public static final int FONTFORMAT_TYPE1 = 1;
@ -949,7 +951,7 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
.info("Opening deferred font file " + fileNameKey);
}
PhysicalFont physicalFont;
PhysicalFont physicalFont = null;
FontRegistrationInfo regInfo = deferredFontFiles.get(fileNameKey);
if (regInfo != null) {
deferredFontFiles.remove(fileNameKey);
@ -959,21 +961,19 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
regInfo.javaRasterizer,
regInfo.fontRank);
if (physicalFont != null) {
/* Store the handle, so that if a font is bad, we
* retrieve the substituted font.
*/
initialisedFonts.put(fileNameKey, physicalFont.handle);
} else {
initialisedFonts.put(fileNameKey,
getDefaultPhysicalFont().handle);
initialisedFonts.put(fileNameKey, FONT_HANDLE_NULL);
}
} else {
Font2DHandle handle = initialisedFonts.get(fileNameKey);
if (handle == null) {
/* Probably shouldn't happen, but just in case */
physicalFont = getDefaultPhysicalFont();
initialisedFonts.put(fileNameKey, FONT_HANDLE_NULL);
} else {
physicalFont = (PhysicalFont)(handle.font2D);
}
@ -1080,15 +1080,20 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
*/
public PhysicalFont getDefaultPhysicalFont() {
if (defaultPhysicalFont == null) {
/* findFont2D will load all fonts before giving up the search.
* If the JRE Lucida isn't found (eg because the JRE fonts
* directory is missing), it could find another version of Lucida
* from the host system. This is OK because at that point we are
* trying to gracefully handle/recover from a system
* misconfiguration and this is probably a reasonable substitution.
*/
defaultPhysicalFont = (PhysicalFont)
findFont2D(getDefaultFontFaceName(), Font.PLAIN, NO_FALLBACK);
String defaultFontName = getDefaultFontFaceName();
// findFont2D will load all fonts
Font2D font2d = findFont2D(defaultFontName, Font.PLAIN, NO_FALLBACK);
if (font2d != null) {
if (font2d instanceof PhysicalFont) {
defaultPhysicalFont = (PhysicalFont)font2d;
} else {
if (FontUtilities.isLogging()) {
FontUtilities.getLogger()
.warning("Font returned by findFont2D for default font name " +
defaultFontName + " is not a physical font: " + font2d.getFontName(null));
}
}
}
if (defaultPhysicalFont == null) {
/* Because of the findFont2D call above, if we reach here, we
* know all fonts have already been loaded, just accept any
@ -1096,12 +1101,8 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
* and I don't know how to recover from there being absolutely
* no fonts anywhere on the system.
*/
Iterator<PhysicalFont> i = physicalFonts.values().iterator();
if (i.hasNext()) {
defaultPhysicalFont = i.next();
} else {
throw new Error("Probable fatal error:No fonts found.");
}
defaultPhysicalFont = physicalFonts.values().stream().findFirst()
.orElseThrow(()->new Error("Probable fatal error: No physical fonts found."));
}
}
return defaultPhysicalFont;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -39,6 +39,9 @@ import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -321,15 +324,10 @@ public class TrueTypeFont extends FileFont {
FontUtilities.getLogger().info("open TTF: " + platName);
}
try {
RandomAccessFile raf = (RandomAccessFile)
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Object>() {
public Object run() {
try {
return new RandomAccessFile(platName, "r");
} catch (FileNotFoundException ffne) {
}
return null;
RandomAccessFile raf = AccessController.doPrivileged(
new PrivilegedExceptionAction<RandomAccessFile>() {
public RandomAccessFile run() throws FileNotFoundException {
return new RandomAccessFile(platName, "r");
}
});
disposerRecord.channel = raf.getChannel();
@ -340,9 +338,13 @@ public class TrueTypeFont extends FileFont {
((SunFontManager) fm).addToPool(this);
}
}
} catch (NullPointerException e) {
} catch (PrivilegedActionException e) {
close();
throw new FontFormatException(e.toString());
Throwable reason = e.getCause();
if (reason == null) {
reason = e;
}
throw new FontFormatException(reason.toString());
} catch (ClosedChannelException e) {
/* NIO I/O is interruptible, recurse to retry operation.
* The call to channel.size() above can throw this exception.

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8221741
* @modules java.desktop/sun.font
* @summary Test font initialization
*/
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import sun.font.Font2D;
import sun.font.SunFontManager;
public class DefaultFontTest {
public static void main(String[] args) throws Exception {
// this triggers font initialization
String[] fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
if (fontFamilyNames != null) {
System.out.println("FontFamilies:");
Arrays.asList(fontFamilyNames).stream().forEach((fontname)->System.out.println(fontname));
}
SunFontManager sfm = SunFontManager.getInstance();
Font[] createdFonts = sfm.getCreatedFonts();
if (createdFonts != null) {
System.out.println("\nCreated Fonts:");
Arrays.asList(createdFonts).stream().forEach((font)->System.out.println(font));
}
Font2D[] registeredFonts = sfm.getRegisteredFonts();
if (registeredFonts != null) {
System.out.println("\nRegistered Fonts:");
Arrays.asList(registeredFonts).stream().forEach((font)->System.out.println(font));
}
System.out.println("\nDefault physical font: " + sfm.getDefaultPhysicalFont());
}
}