src/windows/native/sun/windows/awt_Font.cpp
Print this page
@@ -47,13 +47,16 @@
extern jboolean IsMultiFont(JNIEnv *env, jobject obj)
{
if (obj == NULL) {
return JNI_FALSE;
}
- if (env->EnsureLocalCapacity(2))
+ if (env->EnsureLocalCapacity(2)) {
+ env->ExceptionClear();
return JNI_FALSE;
+ }
jobject peer = env->CallObjectMethod(obj, AwtFont::peerMID);
+ env->ExceptionClear();
if (peer == NULL) {
return JNI_FALSE;
}
jobject fontConfig = env->GetObjectField(peer, AwtFont::fontConfigID);
jboolean result = fontConfig != NULL;
@@ -64,14 +67,16 @@
extern jstring GetTextComponentFontName(JNIEnv *env, jobject font)
{
DASSERT(font != NULL);
if (env->EnsureLocalCapacity(2)) {
+ env->ExceptionClear();
return NULL;
}
jobject peer = env->CallObjectMethod(font, AwtFont::peerMID);
DASSERT(peer != NULL);
+ if (peer == NULL) return NULL;
jstring textComponentFontName =
(jstring) env->GetObjectField(peer, AwtFont::textComponentFontNameID);
env->DeleteLocalRef(peer);
return textComponentFontName;
}
@@ -189,10 +194,13 @@
if (awtFont != NULL) {
return awtFont;
}
awtFont = Create(env, font, angle, awScale);
+ if (awtFont == NULL) {
+ return NULL;
+ }
env->SetLongField(font, AwtFont::pDataID,
reinterpret_cast<jlong>(awtFont));
return awtFont;
}
@@ -270,10 +278,13 @@
awtFont->awScale = awScale;
if (cfnum > 0) {
// Ask peer class for the text component font name
jstring jTextComponentFontName = GetTextComponentFontName(env, font);
+ if (jTextComponentFontName == NULL) {
+ return NULL;
+ }
LPCWSTR textComponentFontName = JNU_GetStringPlatformChars(env, jTextComponentFontName, NULL);
awtFont->m_textInput = -1;
for (int i = 0; i < cfnum; i++) {
// nativeName is a pair of platform fontname and its charset
@@ -283,10 +294,13 @@
jstring nativeName =
(jstring)env->GetObjectField(fontDescriptor,
AwtFont::nativeNameID);
wName = JNU_GetStringPlatformChars(env, nativeName, NULL);
DASSERT(wName);
+ if (wName == NULL) {
+ wName = L"Arial";
+ }
//On NT platforms, if the font is not Symbol or Dingbats
//use "W" version of Win32 APIs directly, info the FontDescription
//no need to convert characters from Unicode to locale encodings.
if (GetNativeCharset(wName) != SYMBOL_CHARSET) {
@@ -319,11 +333,16 @@
env->DeleteLocalRef(jTextComponentFontName);
} else {
// Instantiation for English version.
jstring fontName = (jstring)env->GetObjectField(font,
AwtFont::nameID);
+ if (fontName != NULL) {
wName = JNU_GetStringPlatformChars(env, fontName, NULL);
+ }
+ if (wName == NULL) {
+ wName = L"Arial";
+ }
WCHAR* wEName;
if (!wcscmp(wName, L"Helvetica") || !wcscmp(wName, L"SansSerif")) {
wEName = L"Arial";
} else if (!wcscmp(wName, L"TimesRoman") ||
@@ -645,10 +664,13 @@
//Init AwtFont object, which will "create" a AwtFont object if necessry,
//before calling makeconvertedMultiFontString(), otherwise, the FontDescriptor's
//"useUnicode" field might not be initialized correctly (font in Menu Component,
//for example").
AwtFont* awtFont = AwtFont::GetFont(env, font);
+ if (awtFont == NULL) {
+ return size;
+ }
if (IsMultiFont(env, font)) {
jobject peer = env->CallObjectMethod(font, AwtFont::peerMID);
array = (jobjectArray)(env->CallObjectMethod(
peer, AwtFont::makeConvertedMultiFontStringMID, str));
@@ -666,10 +688,13 @@
HFONT oldFont = (HFONT)::SelectObject(hDC, awtFont->GetHFont());
if (arrayLength == 0) {
int length = env->GetStringLength(str);
LPCWSTR strW = JNU_GetStringPlatformChars(env, str, NULL);
+ if (strW == NULL) {
+ return size;
+ }
VERIFY(::SelectObject(hDC, awtFont->GetHFont()));
if (AwtComponent::GetRTLReadingOrder()){
VERIFY(!draw || ::ExtTextOut(hDC, x, y, ETO_RTLREADING, NULL,
strW, length, NULL));
} else {
@@ -690,10 +715,13 @@
env->DeleteLocalRef(fontDescriptor);
break;
}
int fdIndex = getFontDescriptorNumber(env, font, fontDescriptor);
+ if (env->ExceptionCheck()) {
+ return size; //fdIndex==0 return could be exception or not.
+ }
VERIFY(::SelectObject(hDC, awtFont->GetHFont(fdIndex)));
/*
* The strange-looking code that follows this comment is
* the result of upstream optimizations. In the array of
@@ -703,14 +731,18 @@
*
* Note: the buffer MUST be unsigned, or VC++ will sign
* extend buflen and bad things will happen.
*/
unsigned char* buffer = NULL;
- jboolean unicodeUsed = env->GetBooleanField(fontDescriptor, AwtFont::useUnicodeID);
+ jboolean unicodeUsed =
+ env->GetBooleanField(fontDescriptor, AwtFont::useUnicodeID);
try {
buffer = (unsigned char *)
env->GetPrimitiveArrayCritical(convertedBytes, 0);
+ if (buffer == NULL) {
+ return size;
+ }
int buflen = (buffer[0] << 24) | (buffer[1] << 16) |
(buffer[2] << 8) | buffer[3];
DASSERT(buflen >= 0);
@@ -814,12 +846,15 @@
}
jchar *strp = new jchar[len];
env->GetCharArrayRegion(str, off, len, strp);
jstring jstr = env->NewString(strp, len);
- jint result = Java_sun_awt_windows_WFontMetrics_stringWidth(env, self,
+ jint result = 0;
+ if (jstr != NULL) {
+ result = Java_sun_awt_windows_WFontMetrics_stringWidth(env, self,
jstr);
+ }
delete [] strp;
return result;
CATCH_BAD_ALLOC_RET(0);
}
@@ -848,17 +883,29 @@
char *pStrBody = NULL;
jint result = 0;
try {
jintArray array = (jintArray)env->GetObjectField(self,
AwtFont::widthsID);
+ if (array == NULL) {
+ JNU_ThrowNullPointerException(env, "Can't access widths array.");
+ return NULL;
+ }
pStrBody = (char *)env->GetPrimitiveArrayCritical(str, 0);
+ if (pStrBody == NULL) {
+ JNU_ThrowNullPointerException(env, "Can't access str bytes.");
+ return NULL;
+ }
char *pStr = pStrBody + off;
jint *widths = NULL;
try {
widths = (jint *)env->GetPrimitiveArrayCritical(array, 0);
-
+ if (widths == NULL) {
+ env->ReleasePrimitiveArrayCritical(str, pStrBody, 0);
+ JNU_ThrowNullPointerException(env, "Can't access widths.");
+ return NULL;
+ }
for (; len; len--) {
result += widths[*pStr++];
}
} catch (...) {
if (widths != NULL) {
@@ -913,33 +960,19 @@
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WFontMetrics_initIDs(JNIEnv *env, jclass cls)
{
- TRY;
-
- AwtFont::widthsID = env->GetFieldID(cls, "widths", "[I");
- AwtFont::ascentID = env->GetFieldID(cls, "ascent", "I");
- AwtFont::descentID = env->GetFieldID(cls, "descent", "I");
- AwtFont::leadingID = env->GetFieldID(cls, "leading", "I");
- AwtFont::heightID = env->GetFieldID(cls, "height", "I");
- AwtFont::maxAscentID = env->GetFieldID(cls, "maxAscent", "I");
- AwtFont::maxDescentID = env->GetFieldID(cls, "maxDescent", "I");
- AwtFont::maxHeightID = env->GetFieldID(cls, "maxHeight", "I");
+ CHECK_NULL(AwtFont::widthsID = env->GetFieldID(cls, "widths", "[I"));
+ CHECK_NULL(AwtFont::ascentID = env->GetFieldID(cls, "ascent", "I"));
+ CHECK_NULL(AwtFont::descentID = env->GetFieldID(cls, "descent", "I"));
+ CHECK_NULL(AwtFont::leadingID = env->GetFieldID(cls, "leading", "I"));
+ CHECK_NULL(AwtFont::heightID = env->GetFieldID(cls, "height", "I"));
+ CHECK_NULL(AwtFont::maxAscentID = env->GetFieldID(cls, "maxAscent", "I"));
+ CHECK_NULL(AwtFont::maxDescentID = env->GetFieldID(cls, "maxDescent", "I"));
+ CHECK_NULL(AwtFont::maxHeightID = env->GetFieldID(cls, "maxHeight", "I"));
AwtFont::maxAdvanceID = env->GetFieldID(cls, "maxAdvance", "I");
-
- DASSERT(AwtFont::widthsID != NULL);
- DASSERT(AwtFont::ascentID != NULL);
- DASSERT(AwtFont::descentID != NULL);
- DASSERT(AwtFont::leadingID != NULL);
- DASSERT(AwtFont::heightID != NULL);
- DASSERT(AwtFont::maxAscentID != NULL);
- DASSERT(AwtFont::maxDescentID != NULL);
- DASSERT(AwtFont::maxHeightID != NULL);
- DASSERT(AwtFont::maxAdvanceID != NULL);
-
- CATCH_BAD_ALLOC;
}
} /* extern "C" */
@@ -950,32 +983,20 @@
extern "C" {
JNIEXPORT void JNICALL
Java_java_awt_Font_initIDs(JNIEnv *env, jclass cls)
{
- TRY;
-
- AwtFont::peerMID = env->GetMethodID(cls, "getPeer",
- "()Ljava/awt/peer/FontPeer;");
- AwtFont::pDataID = env->GetFieldID(cls, "pData", "J");
- AwtFont::nameID = env->GetFieldID(cls, "name", "Ljava/lang/String;");
- AwtFont::sizeID = env->GetFieldID(cls, "size", "I");
- AwtFont::styleID = env->GetFieldID(cls, "style", "I");
-
+ CHECK_NULL(AwtFont::peerMID = env->GetMethodID(cls, "getPeer",
+ "()Ljava/awt/peer/FontPeer;"));
+ CHECK_NULL(AwtFont::pDataID = env->GetFieldID(cls, "pData", "J"));
+ CHECK_NULL(AwtFont::nameID =
+ env->GetFieldID(cls, "name", "Ljava/lang/String;"));
+ CHECK_NULL(AwtFont::sizeID = env->GetFieldID(cls, "size", "I"));
+ CHECK_NULL(AwtFont::styleID = env->GetFieldID(cls, "style", "I"));
AwtFont::getFontMID =
env->GetStaticMethodID(cls, "getFont",
"(Ljava/lang/String;)Ljava/awt/Font;");
-
- DASSERT(AwtFont::peerMID != NULL);
- DASSERT(AwtFont::pDataID != NULL);
- DASSERT(AwtFont::nameID != NULL);
- DASSERT(AwtFont::sizeID != NULL);
- DASSERT(AwtFont::styleID != NULL);
-
- DASSERT(AwtFont::getFontMID != NULL);
-
- CATCH_BAD_ALLOC;
}
} /* extern "C" */
@@ -986,19 +1007,13 @@
extern "C" {
JNIEXPORT void JNICALL
Java_java_awt_FontMetrics_initIDs(JNIEnv *env, jclass cls)
{
- TRY;
-
- AwtFont::fontID = env->GetFieldID(cls, "font", "Ljava/awt/Font;");
+ CHECK_NULL(AwtFont::fontID =
+ env->GetFieldID(cls, "font", "Ljava/awt/Font;"));
AwtFont::getHeightMID = env->GetMethodID(cls, "getHeight", "()I");
-
- DASSERT(AwtFont::fontID);
- DASSERT(AwtFont::getHeightMID);
-
- CATCH_BAD_ALLOC;
}
} /* extern "C" */
/************************************************************************
@@ -1008,20 +1023,14 @@
extern "C" {
JNIEXPORT void JNICALL
Java_sun_awt_FontDescriptor_initIDs(JNIEnv *env, jclass cls)
{
- TRY;
-
- AwtFont::nativeNameID = env->GetFieldID(cls, "nativeName",
- "Ljava/lang/String;");
+ CHECK_NULL(AwtFont::nativeNameID =
+ env->GetFieldID(cls, "nativeName", "Ljava/lang/String;"));
AwtFont::useUnicodeID = env->GetFieldID(cls, "useUnicode", "Z");
- DASSERT(AwtFont::nativeNameID != NULL);
- DASSERT(AwtFont::useUnicodeID != NULL);
-
- CATCH_BAD_ALLOC;
}
} /* extern "C" */
@@ -1032,24 +1041,17 @@
extern "C" {
JNIEXPORT void JNICALL
Java_sun_awt_PlatformFont_initIDs(JNIEnv *env, jclass cls)
{
- TRY;
-
- AwtFont::fontConfigID = env->GetFieldID(cls, "fontConfig", "Lsun/awt/FontConfiguration;");
- AwtFont::componentFontsID =
- env->GetFieldID(cls, "componentFonts", "[Lsun/awt/FontDescriptor;");
+ CHECK_NULL(AwtFont::fontConfigID =
+ env->GetFieldID(cls, "fontConfig", "Lsun/awt/FontConfiguration;"));
+ CHECK_NULL(AwtFont::componentFontsID =
+ env->GetFieldID(cls, "componentFonts", "[Lsun/awt/FontDescriptor;"));
AwtFont::makeConvertedMultiFontStringMID =
env->GetMethodID(cls, "makeConvertedMultiFontString",
"(Ljava/lang/String;)[Ljava/lang/Object;");
-
- DASSERT(AwtFont::makeConvertedMultiFontStringMID != NULL);
- DASSERT(AwtFont::componentFontsID != NULL);
- DASSERT(AwtFont::fontConfigID != NULL);
-
- CATCH_BAD_ALLOC;
}
} /* extern "C" */
@@ -1860,12 +1862,14 @@
TRY;
static CCombinedSegTableManager tableManager;
jstring fontName = (jstring)env->GetObjectField(self, AwtFont::fontNameID);
- DASSERT(fontName != NULL);
+ DASSERT(fontName != NULL); // leave in for debug mode.
+ CHECK_NULL_RETURN(fontName, FALSE); // in production, just return
LPCWSTR fontNameW = JNU_GetStringPlatformChars(env, fontName, NULL);
+ CHECK_NULL_RETURN(fontNameW, FALSE);
CCombinedSegTable* pTable = tableManager.GetTable(fontNameW);
JNU_ReleaseStringPlatformChars(env, fontName, fontNameW);
return (pTable->In((USHORT) ch) ? JNI_TRUE : JNI_FALSE);
CATCH_BAD_ALLOC_RET(FALSE);