@@ -678,6 +678,11 @@ static void imageio_abort(JNIEnv *env, jobject this,
678
678
data -> abortFlag = JNI_TRUE ;
679
679
}
680
680
681
+ static void disposeIIO (JNIEnv * env , imageIODataPtr data ) {
682
+ j_common_ptr info = destroyImageioData (env , data );
683
+ imageio_dispose (info );
684
+ }
685
+
681
686
/*************** end of shared utility code ****************/
682
687
683
688
/********************** Loader Support **************************/
@@ -1267,9 +1272,7 @@ JNIEXPORT void JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initJPEGMeth
1267
1272
JNIEXPORT void JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_disposeNative
1268
1273
(JNIEnv * env , jclass cls , jlong ptr ) {
1269
1274
imageIODataPtr data = (imageIODataPtr ) jlong_to_ptr (ptr );
1270
- j_common_ptr info = destroyImageioData (env , data );
1271
-
1272
- imageio_dispose (info );
1275
+ disposeIIO (env , data );
1273
1276
}
1274
1277
1275
1278
#define JPEG_APP1 (JPEG_APP0 + 1) /* EXIF APP1 marker code */
@@ -1330,6 +1333,8 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1330
1333
char buffer [JMSG_LENGTH_MAX ];
1331
1334
(* cinfo -> err -> format_message ) ((struct jpeg_common_struct * ) cinfo ,
1332
1335
buffer );
1336
+ free (cinfo -> err );
1337
+ free (cinfo );
1333
1338
ThrowByName (env , "java/io/IOException" , buffer );
1334
1339
return 0 ;
1335
1340
}
@@ -1347,6 +1352,7 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1347
1352
cinfo -> src =
1348
1353
(struct jpeg_source_mgr * ) malloc (sizeof (struct jpeg_source_mgr ));
1349
1354
if (cinfo -> src == NULL ) {
1355
+ imageio_dispose ((j_common_ptr ) cinfo );
1350
1356
ThrowByName (env ,
1351
1357
"java/lang/OutOfMemoryError" ,
1352
1358
"Initializing Reader" );
@@ -1363,6 +1369,7 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1363
1369
/* set up the association to persist for future calls */
1364
1370
data = initImageioData (env , (j_common_ptr ) cinfo , this );
1365
1371
if (data == NULL ) {
1372
+ imageio_dispose ((j_common_ptr ) cinfo );
1366
1373
ThrowByName (env ,
1367
1374
"java/lang/OutOfMemoryError" ,
1368
1375
"Initializing Reader" );
@@ -1371,7 +1378,10 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1371
1378
1372
1379
imageio_set_stream (env , (j_common_ptr ) cinfo , data , stream );
1373
1380
1374
- if ((* env )-> ExceptionCheck (env )) return 0 ;
1381
+ if ((* env )-> ExceptionCheck (env )) {
1382
+ disposeIIO (env , data );
1383
+ return 0 ;
1384
+ }
1375
1385
1376
1386
imageio_init_source ((j_decompress_ptr ) cinfo );
1377
1387
@@ -1382,20 +1392,22 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1382
1392
if (setjmp (jerr -> setjmp_buffer )) {
1383
1393
/* If we get here, the JPEG code has signaled an error
1384
1394
while reading the header. */
1385
- RELEASE_ARRAYS (env , data , src -> next_input_byte );
1395
+ disposeIIO (env , data );
1386
1396
if (!(* env )-> ExceptionOccurred (env )) {
1387
1397
char buffer [JMSG_LENGTH_MAX ];
1388
1398
(* cinfo -> err -> format_message ) ((struct jpeg_common_struct * ) cinfo ,
1389
1399
buffer );
1390
1400
ThrowByName (env , "java/io/IOException" , buffer );
1391
1401
}
1402
+
1392
1403
return 0 ;
1393
1404
}
1394
1405
1395
1406
if (GET_ARRAYS (env , data , & src -> next_input_byte ) == NOT_OK ) {
1396
1407
ThrowByName (env ,
1397
1408
"java/io/IOException" ,
1398
1409
"Array pin failed" );
1410
+ disposeIIO (env , data );
1399
1411
return 0 ;
1400
1412
}
1401
1413
@@ -1492,6 +1504,7 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1492
1504
profileData = read_icc_profile (env , cinfo );
1493
1505
1494
1506
if ((* env )-> ExceptionCheck (env )) {
1507
+ disposeIIO (env , data );
1495
1508
return 0 ;
1496
1509
}
1497
1510
@@ -1504,6 +1517,7 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_initDecompr
1504
1517
cinfo -> num_components ,
1505
1518
profileData );
1506
1519
if ((* env )-> ExceptionCheck (env )) {
1520
+ disposeIIO (env , data );
1507
1521
return 0 ;
1508
1522
}
1509
1523
}
@@ -1598,8 +1612,8 @@ JNIEXPORT jboolean JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_decompre
1598
1612
struct jpeg_source_mgr * src = cinfo -> src ;
1599
1613
sun_jpeg_error_ptr jerr ;
1600
1614
int bytes_per_row = cinfo -> output_width * cinfo -> output_components ;
1601
- JSAMPROW scanline_ptr = (JSAMPROW ) malloc (bytes_per_row * sizeof (JSAMPLE ));
1602
1615
int offset = 0 ;
1616
+ JSAMPROW scanline_ptr = NULL ;
1603
1617
1604
1618
if (!SAFE_TO_MULT (cinfo -> output_width , cinfo -> output_components ) ||
1605
1619
!SAFE_TO_MULT (bytes_per_row , cinfo -> output_height ) ||
@@ -1619,14 +1633,6 @@ JNIEXPORT jboolean JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_decompre
1619
1633
return JNI_FALSE ;
1620
1634
}
1621
1635
1622
- if (scanline_ptr == NULL ) {
1623
- ThrowByName (env ,
1624
- "java/lang/OutOfMemoryError" ,
1625
- "Reading JPEG Stream" );
1626
- RELEASE_ARRAYS (env , data , cinfo -> src -> next_input_byte );
1627
- return JNI_FALSE ;
1628
- }
1629
-
1630
1636
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */
1631
1637
jerr = (sun_jpeg_error_ptr ) cinfo -> err ;
1632
1638
@@ -1639,26 +1645,35 @@ JNIEXPORT jboolean JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_decompre
1639
1645
buffer );
1640
1646
ThrowByName (env , "java/io/IOException" , buffer );
1641
1647
}
1642
- if (scanline_ptr != NULL ) {
1643
- free (scanline_ptr );
1644
- }
1645
1648
RELEASE_ARRAYS (env , data , cinfo -> src -> next_input_byte );
1646
1649
return JNI_FALSE ;
1647
1650
}
1648
1651
1652
+ scanline_ptr = (JSAMPROW ) malloc (bytes_per_row * sizeof (JSAMPLE ));
1653
+ if (scanline_ptr == NULL ) {
1654
+ ThrowByName (env ,
1655
+ "java/lang/OutOfMemoryError" ,
1656
+ "Reading JPEG Stream" );
1657
+ return JNI_FALSE ;
1658
+ }
1659
+
1649
1660
while (cinfo -> output_scanline < cinfo -> output_height ) {
1650
1661
int num_scanlines ;
1651
1662
if (report_progress == JNI_TRUE ) {
1652
1663
RELEASE_ARRAYS (env , data , cinfo -> src -> next_input_byte );
1653
1664
(* env )-> CallVoidMethod (env , this ,
1654
1665
JPEGImageLoader_updateImageProgressID ,
1655
1666
cinfo -> output_scanline );
1656
- if ((* env )-> ExceptionCheck (env )) return JNI_FALSE ;
1667
+ if ((* env )-> ExceptionCheck (env )) {
1668
+ free (scanline_ptr );
1669
+ return JNI_FALSE ;
1670
+ }
1657
1671
if (GET_ARRAYS (env , data , & cinfo -> src -> next_input_byte ) == NOT_OK ) {
1658
- ThrowByName (env ,
1672
+ free (scanline_ptr );
1673
+ ThrowByName (env ,
1659
1674
"java/io/IOException" ,
1660
1675
"Array pin failed" );
1661
- return JNI_FALSE ;
1676
+ return JNI_FALSE ;
1662
1677
}
1663
1678
}
1664
1679
@@ -1667,30 +1682,33 @@ JNIEXPORT jboolean JNICALL Java_com_sun_javafx_iio_jpeg_JPEGImageLoader_decompre
1667
1682
jbyte * body = (* env )-> GetPrimitiveArrayCritical (env , barray , NULL );
1668
1683
if (body == NULL ) {
1669
1684
fprintf (stderr , "decompressIndirect: GetPrimitiveArrayCritical returns NULL: out of memory\n" );
1685
+ free (scanline_ptr );
1670
1686
return JNI_FALSE ;
1671
1687
}
1672
1688
memcpy (body + offset ,scanline_ptr , bytes_per_row );
1673
1689
(* env )-> ReleasePrimitiveArrayCritical (env , barray , body , JNI_ABORT );
1674
1690
offset += bytes_per_row ;
1675
1691
}
1676
1692
}
1693
+ free (scanline_ptr );
1677
1694
1678
1695
if (report_progress == JNI_TRUE ) {
1679
1696
RELEASE_ARRAYS (env , data , cinfo -> src -> next_input_byte );
1680
1697
(* env )-> CallVoidMethod (env , this ,
1681
1698
JPEGImageLoader_updateImageProgressID ,
1682
1699
cinfo -> output_height );
1683
- if ((* env )-> ExceptionCheck (env )) return JNI_FALSE ;
1684
- if (GET_ARRAYS (env , data , & cinfo -> src -> next_input_byte ) == NOT_OK ) {
1685
- ThrowByName (env ,
1700
+ if ((* env )-> ExceptionCheck (env )) {
1701
+ return JNI_FALSE ;
1702
+ }
1703
+ if (GET_ARRAYS (env , data , & cinfo -> src -> next_input_byte ) == NOT_OK ) {
1704
+ ThrowByName (env ,
1686
1705
"java/io/IOException" ,
1687
1706
"Array pin failed" );
1688
- return JNI_FALSE ;
1689
- }
1707
+ return JNI_FALSE ;
1708
+ }
1690
1709
}
1691
1710
1692
1711
jpeg_finish_decompress (cinfo );
1693
- free (scanline_ptr );
1694
1712
1695
1713
RELEASE_ARRAYS (env , data , cinfo -> src -> next_input_byte );
1696
1714
return JNI_TRUE ;
0 commit comments