From 973f07db7982e78c634c29f6139dea547a111141 Mon Sep 17 00:00:00 2001
From: Armin Weatherwax
Date: Wed, 3 Feb 2010 17:10:57 +0100
Subject: Fixed a rare crash from textures with too many components.

Only seen in lbsa plaza on OSGrid, so far.
---
 ChangeLog.txt                                    | 10 ++++++++
 linden/indra/llimagej2coj/llimagej2coj.cpp       | 29 +++++++++++++++++-------
 linden/indra/llrender/llimagegl.cpp              |  4 +++-
 linden/indra/newview/app_settings/logcontrol.xml |  2 +-
 4 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/ChangeLog.txt b/ChangeLog.txt
index a6dbd4d..2e1096f 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -243,6 +243,16 @@
 	  modified:   linden/indra/newview/llviewercontrol.cpp
 
 
+2010-02-03  Armin Weatherwax  <Armin.Weatherwax@gmail.com>
+
+	* Fixed a rare crash from textures with too many components.
+	  Only seen in lbsa plaza on OSGrid, so far.
+
+	  modified:   linden/indra/llimagej2coj/llimagej2coj.cpp
+	  modified:   linden/indra/llrender/llimagegl.cpp
+	  modified:   linden/indra/newview/app_settings/logcontrol.xml
+
+
 2010-01-30  Jacek Antonelli  <jacek.antonelli@gmail.com>
 
 	* Improved "Pay Object" floater layout and XML.
diff --git a/linden/indra/llimagej2coj/llimagej2coj.cpp b/linden/indra/llimagej2coj/llimagej2coj.cpp
index be6b6c8..b7a1b82 100644
--- a/linden/indra/llimagej2coj/llimagej2coj.cpp
+++ b/linden/indra/llimagej2coj/llimagej2coj.cpp
@@ -99,6 +99,7 @@ void info_callback(const char* msg, void*)
 
 LLImageJ2COJ::LLImageJ2COJ() : LLImageJ2CImpl()
 {
+	mRawImagep=NULL;
 }
 
 
@@ -166,9 +167,17 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
 	// The image decode failed if the return was NULL or the component
 	// count was zero.  The latter is just a sanity check before we
 	// dereference the array.
-	if(!image || !image->numcomps)
+	if(!image) 
 	{
-		llwarns << "ERROR -> decodeImpl: failed to decode image!" << llendl;
+		LL_DEBUGS("Openjpeg")  << "ERROR -> decodeImpl: failed to decode image - no image" << LL_ENDL;
+		return TRUE; // done
+	}
+
+	S32 img_components = image->numcomps;
+
+	if( !img_components ) // < 1 ||img_components > 4 )
+	{
+		LL_DEBUGS("Openjpeg") << "ERROR -> decodeImpl: failed to decode image wrong number of components: " << img_components << LL_ENDL;
 		if (image)
 		{
 			opj_image_destroy(image);
@@ -178,20 +187,21 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
 	}
 
 	// sometimes we get bad data out of the cache - check to see if the decode succeeded
-	for (S32 i = 0; i < image->numcomps; i++)
+	for (S32 i = 0; i < img_components; i++)
 	{
 		if (image->comps[i].factor != base.getRawDiscardLevel())
 		{
 			// if we didn't get the discard level we're expecting, fail
-			opj_image_destroy(image);
+			if (image) //anyway somthing odd with the image, better check than crash
+				opj_image_destroy(image);
 			base.mDecoding = FALSE;
 			return TRUE;
 		}
 	}
 	
-	if(image->numcomps <= first_channel)
+	if(img_components <= first_channel)
 	{
-		llwarns << "trying to decode more channels than are present in image: numcomps: " << image->numcomps << " first_channel: " << first_channel << llendl;
+		LL_DEBUGS("Openjpeg") << "trying to decode more channels than are present in image: numcomps: " << img_components << " first_channel: " << first_channel << LL_ENDL;
 		if (image)
 		{
 			opj_image_destroy(image);
@@ -202,7 +212,7 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
 
 	// Copy image data into our raw image format (instead of the separate channel format
 
-	S32 img_components = image->numcomps;
+
 	S32 channels = img_components - first_channel;
 	if( channels > max_channel_count )
 		channels = max_channel_count;
@@ -249,7 +259,10 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
 	}
 
 	/* free image data structure */
-	opj_image_destroy(image);
+	if (image)
+	{
+		opj_image_destroy(image);
+	}
 
 	return TRUE; // done
 }
diff --git a/linden/indra/llrender/llimagegl.cpp b/linden/indra/llrender/llimagegl.cpp
index 27b1d55..544eb80 100644
--- a/linden/indra/llrender/llimagegl.cpp
+++ b/linden/indra/llrender/llimagegl.cpp
@@ -1014,7 +1014,9 @@ BOOL LLImageGL::createGLTexture(S32 discard_level, const LLImageRaw* imageraw, S
 			mFormatType = GL_UNSIGNED_BYTE;
 			break;
 		  default:
-			llerrs << "Bad number of components for texture: " << (U32)getComponents() << llendl;
+			LL_DEBUGS("Openjpeg") << "Bad number of components for texture: " << (U32)getComponents() << LL_ENDL;
+			to_create = false;
+			break;
 		}
 	}
 
diff --git a/linden/indra/newview/app_settings/logcontrol.xml b/linden/indra/newview/app_settings/logcontrol.xml
index 60f78b7..401b145 100644
--- a/linden/indra/newview/app_settings/logcontrol.xml
+++ b/linden/indra/newview/app_settings/logcontrol.xml
@@ -51,7 +51,7 @@
 							<string>MediaState</string>
 							<string>ShaderLoading</string>
               <string>XMLNode</string>
-              
+							<string>Openjpeg</string>
               <!--Unused debug messages below-->
               
               <!--<string>RLV</string>
-- 
cgit v1.1