mirror of
https://github.com/openjdk/jdk.git
synced 2026-04-09 14:38:42 +00:00
2163516: Font.createFont can be persuaded to leak temporary files
Reviewed-by: igor
This commit is contained in:
parent
22f94de7e6
commit
de4c8e0eb5
@ -2361,7 +2361,7 @@ public final class FontManager {
|
||||
font2D = new TrueTypeFont(fontFilePath, null, 0, true);
|
||||
break;
|
||||
case Font.TYPE1_FONT:
|
||||
font2D = new Type1Font(fontFilePath, null);
|
||||
font2D = new Type1Font(fontFilePath, null, isCopy);
|
||||
break;
|
||||
default:
|
||||
throw new FontFormatException("Unrecognised Font Format");
|
||||
|
||||
@ -174,8 +174,17 @@ public class TrueTypeFont extends FileFont {
|
||||
super(platname, nativeNames);
|
||||
useJavaRasterizer = javaRasterizer;
|
||||
fontRank = Font2D.TTF_RANK;
|
||||
verify();
|
||||
init(fIndex);
|
||||
try {
|
||||
verify();
|
||||
init(fIndex);
|
||||
} catch (Throwable t) {
|
||||
close();
|
||||
if (t instanceof FontFormatException) {
|
||||
throw (FontFormatException)t;
|
||||
} else {
|
||||
throw new FontFormatException("Unexpected runtime exception.");
|
||||
}
|
||||
}
|
||||
Disposer.addObjectRecord(this, disposerRecord);
|
||||
}
|
||||
|
||||
|
||||
@ -39,6 +39,7 @@ import java.nio.BufferUnderflowException;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import sun.java2d.Disposer;
|
||||
import sun.java2d.DisposerRecord;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.awt.Font;
|
||||
@ -76,6 +77,27 @@ import java.awt.Font;
|
||||
*/
|
||||
public class Type1Font extends FileFont {
|
||||
|
||||
private static class T1DisposerRecord implements DisposerRecord {
|
||||
String fileName = null;
|
||||
|
||||
T1DisposerRecord(String name) {
|
||||
fileName = name;
|
||||
}
|
||||
|
||||
public synchronized void dispose() {
|
||||
java.security.AccessController.doPrivileged(
|
||||
new java.security.PrivilegedAction() {
|
||||
public Object run() {
|
||||
|
||||
if (fileName != null) {
|
||||
(new java.io.File(fileName)).delete();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
WeakReference bufferRef = new WeakReference(null);
|
||||
|
||||
private String psName = null;
|
||||
@ -124,6 +146,17 @@ public class Type1Font extends FileFont {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a Type1 Font.
|
||||
* @param platname - Platform identifier of the font. Typically file name.
|
||||
* @param nativeNames - Native names - typically XLFDs on Unix.
|
||||
*/
|
||||
public Type1Font(String platname, Object nativeNames)
|
||||
throws FontFormatException {
|
||||
|
||||
this(platname, nativeNames, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* - does basic verification of the file
|
||||
* - reads the names (full, family).
|
||||
@ -131,12 +164,25 @@ public class Type1Font extends FileFont {
|
||||
* @throws FontFormatException - if the font can't be opened
|
||||
* or fails verification, or there's no usable cmap
|
||||
*/
|
||||
public Type1Font(String platname, Object nativeNames)
|
||||
public Type1Font(String platname, Object nativeNames, boolean createdCopy)
|
||||
throws FontFormatException {
|
||||
super(platname, nativeNames);
|
||||
fontRank = Font2D.TYPE1_RANK;
|
||||
checkedNatives = true;
|
||||
verify();
|
||||
try {
|
||||
verify();
|
||||
} catch (Throwable t) {
|
||||
if (createdCopy) {
|
||||
T1DisposerRecord ref = new T1DisposerRecord(platname);
|
||||
Disposer.addObjectRecord(bufferRef, ref);
|
||||
bufferRef = null;
|
||||
}
|
||||
if (t instanceof FontFormatException) {
|
||||
throw (FontFormatException)t;
|
||||
} else {
|
||||
throw new FontFormatException("Unexpected runtime exception.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized ByteBuffer getBuffer() throws FontFormatException {
|
||||
|
||||
@ -55,17 +55,23 @@ public class DeleteFont {
|
||||
if (!gotException) {
|
||||
throw new RuntimeException("No expected IOException");
|
||||
}
|
||||
badRead(-2);
|
||||
badRead(8193);
|
||||
badRead(-2, Font.TRUETYPE_FONT);
|
||||
badRead(8193, Font.TRUETYPE_FONT);
|
||||
|
||||
badRead(-2, Font.TYPE1_FONT);
|
||||
badRead(8193, Font.TYPE1_FONT);
|
||||
|
||||
// Make sure GC has a chance to clean up before we exit.
|
||||
System.gc(); System.gc();
|
||||
}
|
||||
|
||||
static void badRead(final int retval) {
|
||||
static void badRead(final int retval, int fontType) {
|
||||
int num = 2;
|
||||
byte[] buff = new byte[16*8192]; // Multiple of 8192 is important.
|
||||
for (int ct=0; ct<num; ++ct) {
|
||||
try {
|
||||
Font.createFont(
|
||||
Font.TRUETYPE_FONT,
|
||||
fontType,
|
||||
new ByteArrayInputStream(buff) {
|
||||
@Override
|
||||
public int read(byte[] buff, int off, int len) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user