Skip to content

Commit 27f4ff8

Browse files
committed
Java: Guard against int overflow in size methods
Because Java array sizes are ints, the various size methods in the TJ class have int return values. Thus, we have to guard against signed int overflow at the JNI level, because the C functions can return sizes greater than INT_MAX. This also adds a test for TJ.planeWidth() and TJ.planeHeight(), in order to validate 8a1526a in Java.
1 parent 1485bea commit 27f4ff8

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

java/TJUnitTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,34 +844,46 @@ static void overflowTest() throws Exception {
844844

845845
try {
846846
exception = false;
847-
size = TJ.bufSize(26755, 26755, TJ.SAMP_444);
847+
size = TJ.bufSize(18919, 18919, TJ.SAMP_444);
848848
} catch (Exception e) { exception = true; }
849849
if (!exception || size != 0)
850850
throw new Exception("TJ.bufSize() overflow");
851851
try {
852852
exception = false;
853-
size = TJ.bufSizeYUV(37838, 1, 37838, TJ.SAMP_444);
853+
size = TJ.bufSizeYUV(26755, 1, 26755, TJ.SAMP_444);
854854
} catch (Exception e) { exception = true; }
855855
if (!exception || size != 0)
856856
throw new Exception("TJ.bufSizeYUV() overflow");
857857
try {
858858
exception = false;
859-
size = TJ.bufSizeYUV(37837, 3, 37837, TJ.SAMP_444);
859+
size = TJ.bufSizeYUV(26754, 3, 26754, TJ.SAMP_444);
860860
} catch (Exception e) { exception = true; }
861861
if (!exception || size != 0)
862862
throw new Exception("TJ.bufSizeYUV() overflow");
863863
try {
864864
exception = false;
865-
size = TJ.bufSizeYUV(37837, -1, 37837, TJ.SAMP_444);
865+
size = TJ.bufSizeYUV(26754, -1, 26754, TJ.SAMP_444);
866866
} catch (Exception e) { exception = true; }
867867
if (!exception || size != 0)
868868
throw new Exception("TJ.bufSizeYUV() overflow");
869869
try {
870870
exception = false;
871-
size = TJ.planeSizeYUV(0, 65536, 0, 65536, TJ.SAMP_444);
871+
size = TJ.planeSizeYUV(0, 46341, 0, 46341, TJ.SAMP_444);
872872
} catch (Exception e) { exception = true; }
873873
if (!exception || size != 0)
874874
throw new Exception("TJ.planeSizeYUV() overflow");
875+
try {
876+
exception = false;
877+
size = TJ.planeWidth(0, Integer.MAX_VALUE, TJ.SAMP_420);
878+
} catch (Exception e) { exception = true; }
879+
if (!exception || size != 0)
880+
throw new Exception("TJ.planeWidth() overflow");
881+
try {
882+
exception = false;
883+
size = TJ.planeHeight(0, Integer.MAX_VALUE, TJ.SAMP_420);
884+
} catch (Exception e) { exception = true; }
885+
if (!exception || size != 0)
886+
throw new Exception("TJ.planeHeight() overflow");
875887
}
876888

877889
static void bufSizeTest() throws Exception {

turbojpeg-jni.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29+
#include <limits.h>
2930
#include "turbojpeg.h"
3031
#include "jinclude.h"
3132
#include <jni.h>
@@ -135,7 +136,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
135136
unsigned long retval = tjBufSize(width, height, jpegSubsamp);
136137

137138
if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
138-
if (retval > (unsigned long)((unsigned int)-1))
139+
if (retval > (unsigned long)INT_MAX)
139140
THROW_ARG("Image is too large");
140141

141142
bailout:
@@ -149,7 +150,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
149150
unsigned long retval = tjBufSizeYUV2(width, align, height, subsamp);
150151

151152
if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
152-
if (retval > (unsigned long)((unsigned int)-1))
153+
if (retval > (unsigned long)INT_MAX)
153154
THROW_ARG("Image is too large");
154155

155156
bailout:
@@ -174,7 +175,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
174175
subsamp);
175176

176177
if (retval == (unsigned long)-1) THROW_ARG(tjGetErrorStr());
177-
if (retval > (unsigned long)((unsigned int)-1))
178+
if (retval > (unsigned long)INT_MAX)
178179
THROW_ARG("Image is too large");
179180

180181
bailout:

0 commit comments

Comments
 (0)