diff --git a/java/TJUnitTest.java b/java/TJUnitTest.java index afba17c0..b8f33786 100644 --- a/java/TJUnitTest.java +++ b/java/TJUnitTest.java @@ -857,6 +857,43 @@ final class TJUnitTest { if (tjd != null) tjd.close(); } + static void overflowTest() throws Exception { + /* Ensure that the various buffer size methods don't overflow */ + int size = 0; + boolean exception = false; + + try { + exception = false; + size = TJ.bufSize(26755, 26755, TJ.SAMP_444); + } catch (Exception e) { exception = true; } + if (!exception || size != 0) + throw new Exception("TJ.bufSize() overflow"); + try { + exception = false; + size = TJ.bufSizeYUV(37838, 1, 37838, TJ.SAMP_444); + } catch (Exception e) { exception = true; } + if (!exception || size != 0) + throw new Exception("TJ.bufSizeYUV() overflow"); + try { + exception = false; + size = TJ.bufSizeYUV(37837, 3, 37837, TJ.SAMP_444); + } catch (Exception e) { exception = true; } + if (!exception || size != 0) + throw new Exception("TJ.bufSizeYUV() overflow"); + try { + exception = false; + size = TJ.bufSizeYUV(37837, -1, 37837, TJ.SAMP_444); + } catch (Exception e) { exception = true; } + if (!exception || size != 0) + throw new Exception("TJ.bufSizeYUV() overflow"); + try { + exception = false; + size = TJ.planeSizeYUV(0, 65536, 0, 65536, TJ.SAMP_444); + } catch (Exception e) { exception = true; } + if (!exception || size != 0) + throw new Exception("TJ.planeSizeYUV() overflow"); + } + static void bufSizeTest() throws Exception { int w, h, i, subsamp, flags = 0, quality = 100, numSamp = TJ.NUMSAMP; byte[] srcBuf, dstBuf = null; @@ -942,6 +979,7 @@ final class TJUnitTest { throw new Exception("Lossless JPEG and YUV encoding/decoding are incompatible."); if (doYUV) FORMATS_4BYTE[4] = -1; + overflowTest(); doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_444, testName); doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_444, diff --git a/java/doc/index-all.html b/java/doc/index-all.html index 1a06aadc..b770db6a 100644 --- a/java/doc/index-all.html +++ b/java/doc/index-all.html @@ -166,9 +166,10 @@
BufferedImage
- instance containing the packed-pixel decompressed/decoded image.BufferedImage instance containing the packed-pixel
+ decompressed/decoded image.BufferedImage
- instance containing the packed-pixel decompressed/decoded image.BufferedImage instance containing the packed-pixel
+ decompressed/decoded image.BufferedImage
- instance containing the packed-pixel decompressed/decoded image.BufferedImage instance containing the packed-pixel
+ decompressed/decoded image.desiredWidth - see
decompress(byte[], int, int, int, int, int, int, int) for
descriptiondesiredHeight - see
diff --git a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java
index d5b9afd9..d0d1e476 100644
--- a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java
+++ b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java
@@ -803,9 +803,10 @@ public class TJDecompressor implements Closeable {
}
/**
- * Decompress the JPEG source image or decode the YUV source image associated
- * with this decompressor instance and return a BufferedImage
- * instance containing the packed-pixel decompressed/decoded image.
+ * Decompress the JPEG source image or decode the planar YUV source image
+ * associated with this decompressor instance and return a
+ * BufferedImage instance containing the packed-pixel
+ * decompressed/decoded image.
*
* @param desiredWidth see
* {@link #decompress(byte[], int, int, int, int, int, int, int)} for
diff --git a/turbojpeg-jni.c b/turbojpeg-jni.c
index e533c8f7..01eb312d 100644
--- a/turbojpeg-jni.c
+++ b/turbojpeg-jni.c
@@ -132,24 +132,28 @@ bailout:
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
(JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
{
- jint retval = (jint)tjBufSize(width, height, jpegSubsamp);
+ unsigned long retval = tjBufSize(width, height, jpegSubsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
+ if (retval > (unsigned long)((unsigned int)-1))
+ THROW_ARG("Image is too large");
bailout:
- return retval;
+ return (jint)retval;
}
/* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
(JNIEnv *env, jclass cls, jint width, jint align, jint height, jint subsamp)
{
- jint retval = (jint)tjBufSizeYUV2(width, align, height, subsamp);
+ unsigned long retval = tjBufSizeYUV2(width, align, height, subsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
+ if (retval > (unsigned long)((unsigned int)-1))
+ THROW_ARG("Image is too large");
bailout:
- return retval;
+ return (jint)retval;
}
/* TurboJPEG 1.4.x: TJ::planeSizeYUV() */
@@ -157,13 +161,15 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
(JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
jint height, jint subsamp)
{
- jint retval = (jint)tjPlaneSizeYUV(componentID, width, stride, height,
- subsamp);
+ unsigned long retval = tjPlaneSizeYUV(componentID, width, stride, height,
+ subsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
+ if (retval > (unsigned long)((unsigned int)-1))
+ THROW_ARG("Image is too large");
bailout:
- return retval;
+ return (jint)retval;
}
/* TurboJPEG 1.4.x: TJ::planeWidth() */