Skip to content

Commit 6a232c3

Browse files
committed
Fix #68716: possible resource leaks in _php_image_convert()
We properly clean up after ourselves wrt. to closing opened file pointers and created images.
1 parent d65adac commit 6a232c3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818
images). (cmb)
1919
. Fixed bug #72913 (imagecopy() loses single-color transparency on palette
2020
images). (cmb)
21+
. Fixed bug #68716 (possible resource leaks in _php_image_convert()). (cmb)
2122

2223
- JSON:
2324
. Fixed bug #72787 (json_decode reads out of bounds). (Jakub Zelenka)

ext/gd/gd.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4530,6 +4530,7 @@ static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
45304530
dest = VCWD_FOPEN(fn_dest, "wb");
45314531
if (!dest) {
45324532
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn_dest);
4533+
fclose(org);
45334534
RETURN_FALSE;
45344535
}
45354536

@@ -4538,6 +4539,8 @@ static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
45384539
im_org = gdImageCreateFromGif(org);
45394540
if (im_org == NULL) {
45404541
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid GIF file", fn_dest);
4542+
fclose(org);
4543+
fclose(dest);
45414544
RETURN_FALSE;
45424545
}
45434546
break;
@@ -4548,6 +4551,8 @@ static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
45484551
im_org = gdImageCreateFromJpegEx(org, ignore_warning);
45494552
if (im_org == NULL) {
45504553
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid JPEG file", fn_dest);
4554+
fclose(org);
4555+
fclose(dest);
45514556
RETURN_FALSE;
45524557
}
45534558
break;
@@ -4558,17 +4563,23 @@ static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
45584563
im_org = gdImageCreateFromPng(org);
45594564
if (im_org == NULL) {
45604565
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid PNG file", fn_dest);
4566+
fclose(org);
4567+
fclose(dest);
45614568
RETURN_FALSE;
45624569
}
45634570
break;
45644571
#endif /* HAVE_GD_PNG */
45654572

45664573
default:
45674574
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Format not supported");
4575+
fclose(org);
4576+
fclose(dest);
45684577
RETURN_FALSE;
45694578
break;
45704579
}
45714580

4581+
fclose(org);
4582+
45724583
org_width = gdImageSX (im_org);
45734584
org_height = gdImageSY (im_org);
45744585

@@ -4599,30 +4610,38 @@ static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
45994610
im_tmp = gdImageCreate (dest_width, dest_height);
46004611
if (im_tmp == NULL ) {
46014612
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate temporary buffer");
4613+
fclose(dest);
4614+
gdImageDestroy(im_org);
46024615
RETURN_FALSE;
46034616
}
46044617

46054618
gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, dest_height, org_width, org_height);
46064619

46074620
gdImageDestroy(im_org);
46084621

4609-
fclose(org);
4610-
46114622
im_dest = gdImageCreate(dest_width, dest_height);
46124623
if (im_dest == NULL) {
46134624
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate destination buffer");
4625+
fclose(dest);
4626+
gdImageDestroy(im_tmp);
46144627
RETURN_FALSE;
46154628
}
46164629

46174630
white = gdImageColorAllocate(im_dest, 255, 255, 255);
46184631
if (white == -1) {
46194632
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
4633+
fclose(dest);
4634+
gdImageDestroy(im_tmp);
4635+
gdImageDestroy(im_dest);
46204636
RETURN_FALSE;
46214637
}
46224638

46234639
black = gdImageColorAllocate(im_dest, 0, 0, 0);
46244640
if (black == -1) {
46254641
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
4642+
fclose(dest);
4643+
gdImageDestroy(im_tmp);
4644+
gdImageDestroy(im_dest);
46264645
RETURN_FALSE;
46274646
}
46284647

0 commit comments

Comments
 (0)