diff -durN lindenlab-3p-openjpeg/openjpeg_v1_4_sources_r697/libopenjpeg/j2k.c lindenlab-3p-openjpeg-patched/openjpeg_v1_4_sources_r697/libopenjpeg/j2k.c
--- lindenlab-3p-openjpeg/openjpeg_v1_4_sources_r697/libopenjpeg/j2k.c	2012-01-23 23:09:10.000000000 +0100
+++ lindenlab-3p-openjpeg-patched/openjpeg_v1_4_sources_r697/libopenjpeg/j2k.c	2014-03-21 11:16:31.867320569 +0100
@@ -1216,7 +1216,7 @@
 		static int backup_tileno = 0;
 
 		/* tileno is negative or larger than the number of tiles!!! */
-		if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
+		if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
 			opj_event_msg(j2k->cinfo, EVT_ERROR,
 				"JPWL: bad tile number (%d out of a maximum of %d)\n",
 				tileno, (cp->tw * cp->th));
@@ -1233,8 +1233,18 @@
 
 		/* keep your private count of tiles */
 		backup_tileno++;
-	};
+	}
+	else
 #endif /* USE_JPWL */
+	{
+		/* tileno is negative or larger than the number of tiles!!! */
+		if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
+			opj_event_msg(j2k->cinfo, EVT_ERROR,
+				"JPWL: bad tile number (%d out of a maximum of %d)\n",
+				tileno, (cp->tw * cp->th));
+			return;
+		}
+	}
 	
 	if (cp->tileno_size == 0) {
 		cp->tileno[cp->tileno_size] = tileno;
@@ -1272,7 +1282,28 @@
 				totlen);
 		}
 
-	};
+	}
+	else
+	{
+		/* totlen is negative or larger than the bytes left!!! */
+		if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
+			opj_event_msg(j2k->cinfo, EVT_ERROR,
+				"JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
+				totlen, cio_numbytesleft(cio) + 8);
+			return;
+		}
+	}
+#else
+		/* totlen is negative ! */
+		/*  NOTE: it CAN be rightfully larger than the bytes left when decoding
+				  at a non-zero discard level. Using the same test as in
+				  USE_JPWL case above breaks OpenJPEG for SecondLife... HB */
+		if (totlen < 0) {
+			opj_event_msg(j2k->cinfo, EVT_ERROR,
+				"JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
+				totlen, cio_numbytesleft(cio) + 8);
+			return;
+		}
 #endif /* USE_JPWL */
 
 	if (!totlen)
