Skip to content

Commit 98fa48c

Browse files
committed
8298093: improve cleanup and error handling of awt_parseColorModel in awt_parseImage.c
Reviewed-by: lucy, clanger
1 parent 5f63f7a commit 98fa48c

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/java.desktop/share/native/libawt/awt/image/awt_parseImage.c

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -454,6 +454,7 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
454454

455455
int i;
456456
static jobject s_jdefCM = NULL;
457+
cmP->nBits = NULL;
457458

458459
if (JNU_IsNull(env, jcmodel)) {
459460
JNU_ThrowNullPointerException(env, "null ColorModel object");
@@ -485,7 +486,6 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
485486
return -1;
486487
}
487488

488-
cmP->nBits = NULL;
489489
if (SAFE_TO_ALLOC_2(cmP->numComponents, sizeof(jint))) {
490490
cmP->nBits = (jint *)malloc(cmP->numComponents * sizeof(jint));
491491
}
@@ -508,7 +508,9 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
508508
cmP->csType = (*env)->GetIntField(env, cmP->jcmodel, g_CMcsTypeID);
509509

510510
cmP->cmType = getColorModelType(env, jcmodel);
511-
JNU_CHECK_EXCEPTION_RETURN(env, -1);
511+
if ((*env)->ExceptionCheck(env)) {
512+
goto cleanup;
513+
}
512514

513515
cmP->isDefaultCM = FALSE;
514516
cmP->isDefaultCompatCM = FALSE;
@@ -530,14 +532,21 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
530532
if (s_jdefCM == NULL) {
531533
jobject defCM;
532534
jclass jcm = (*env)->FindClass(env, "java/awt/image/ColorModel");
533-
CHECK_NULL_RETURN(jcm, -1);
535+
if (jcm == NULL) {
536+
goto cleanup;
537+
}
534538
defCM = (*env)->CallStaticObjectMethod(env, jcm,
535539
g_CMgetRGBdefaultMID, NULL);
540+
if ((*env)->ExceptionCheck(env)) {
541+
(*env)->ExceptionClear(env);
542+
JNU_ThrowNullPointerException(env, "Unable to find default CM");
543+
goto cleanup;
544+
}
536545
s_jdefCM = (*env)->NewGlobalRef(env, defCM);
537546
if (defCM == NULL || s_jdefCM == NULL) {
538547
(*env)->ExceptionClear(env);
539548
JNU_ThrowNullPointerException(env, "Unable to find default CM");
540-
return -1;
549+
goto cleanup;
541550
}
542551
}
543552
cmP->isDefaultCM = ((*env)->IsSameObject(env, s_jdefCM, jcmodel));
@@ -549,12 +558,12 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
549558
if (cmP->csType != java_awt_color_ColorSpace_TYPE_RGB ||
550559
!cmP->is_sRGB)
551560
{
552-
return -1;
561+
goto cleanup;
553562
}
554563

555564
for (i = 0; i < cmP->numComponents; i++) {
556565
if (cmP->nBits[i] != 8) {
557-
return -1;
566+
goto cleanup;
558567
}
559568
}
560569
}
@@ -572,7 +581,7 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
572581
cmP->jrgb,
573582
NULL);
574583
if (rgb == NULL) {
575-
return -1;
584+
goto cleanup;
576585
}
577586
for (i=0; i < cmP->mapSize; i++) {
578587
if ((rgb[i]&0xff000000) == 0) {
@@ -590,6 +599,10 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
590599
}
591600

592601
return 1;
602+
603+
cleanup:
604+
free(cmP->nBits);
605+
return -1;
593606
}
594607

595608
void awt_freeParsedRaster(RasterS_t *rasterP, int freeRasterP) {

0 commit comments

Comments
 (0)