Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8298093: improve cleanup and error handling of awt_parseColorModel in…
… awt_parseImage.c

Backport-of: 98fa48c330941efe6588a907b383802a11ed0e6b
  • Loading branch information
MBaesken committed Jan 18, 2023
1 parent 014c5ae commit 465b4f2
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/java.desktop/share/native/libawt/awt/image/awt_parseImage.c
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, 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
Expand Down Expand Up @@ -454,6 +454,7 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,

int i;
static jobject s_jdefCM = NULL;
cmP->nBits = NULL;

if (JNU_IsNull(env, jcmodel)) {
JNU_ThrowNullPointerException(env, "null ColorModel object");
Expand Down Expand Up @@ -485,7 +486,6 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
return -1;
}

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

cmP->cmType = getColorModelType(env, jcmodel);
JNU_CHECK_EXCEPTION_RETURN(env, -1);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}

cmP->isDefaultCM = FALSE;
cmP->isDefaultCompatCM = FALSE;
Expand All @@ -530,14 +532,21 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
if (s_jdefCM == NULL) {
jobject defCM;
jclass jcm = (*env)->FindClass(env, "java/awt/image/ColorModel");
CHECK_NULL_RETURN(jcm, -1);
if (jcm == NULL) {
goto cleanup;
}
defCM = (*env)->CallStaticObjectMethod(env, jcm,
g_CMgetRGBdefaultMID, NULL);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
JNU_ThrowNullPointerException(env, "Unable to find default CM");
goto cleanup;
}
s_jdefCM = (*env)->NewGlobalRef(env, defCM);
if (defCM == NULL || s_jdefCM == NULL) {
(*env)->ExceptionClear(env);
JNU_ThrowNullPointerException(env, "Unable to find default CM");
return -1;
goto cleanup;
}
}
cmP->isDefaultCM = ((*env)->IsSameObject(env, s_jdefCM, jcmodel));
Expand All @@ -549,12 +558,12 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
if (cmP->csType != java_awt_color_ColorSpace_TYPE_RGB ||
!cmP->is_sRGB)
{
return -1;
goto cleanup;
}

for (i = 0; i < cmP->numComponents; i++) {
if (cmP->nBits[i] != 8) {
return -1;
goto cleanup;
}
}
}
Expand All @@ -572,7 +581,7 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
cmP->jrgb,
NULL);
if (rgb == NULL) {
return -1;
goto cleanup;
}
for (i=0; i < cmP->mapSize; i++) {
if ((rgb[i]&0xff000000) == 0) {
Expand All @@ -590,6 +599,10 @@ int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
}

return 1;

cleanup:
free(cmP->nBits);
return -1;
}

void awt_freeParsedRaster(RasterS_t *rasterP, int freeRasterP) {
Expand Down

1 comment on commit 465b4f2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.