Skip to content

Commit 60a9f7a

Browse files
committed
Fix #77195: Incorrect error handling of imagecreatefromjpeg()
The broken JPEG image triggers a notice, two warnings and outputs a message to stderr directly. The additional notice is pretty useless, and the direct output to stderr is bad. Therefore, we port the relevant differences from upstream to our bundled libgd. This leaves us with two warnings; the first one is triggered by libjpeg and shows the actual problem, the second one is triggered by our libgd wrapper whenever an image can't be read, what may not have necessarily triggered a warning before.
1 parent 115ee49 commit 60a9f7a

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PHP NEWS
99
- COM:
1010
. Fixed bug #77177 (Serializing or unserializing COM objects crashes). (cmb)
1111

12+
- GD:
13+
. Fixed bug #77195 (Incorrect error handling of imagecreatefromjpeg()). (cmb)
14+
1215
- Sockets:
1316
. Fixed bug #77136 (Unsupported IPV6_RECVPKTINFO constants on macOS).
1417
(Mizunashi Mana)

ext/gd/libgd/gd_jpeg.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ static long php_jpeg_emit_message(j_common_ptr jpeg_info, int level)
6767
* unless strace_level >= 3
6868
*/
6969
if ((jpeg_info->err->num_warnings == 0) || (jpeg_info->err->trace_level >= 3)) {
70-
gd_error_ex(ignore_warning ? GD_NOTICE : GD_WARNING, "gd-jpeg, libjpeg: recoverable error: %s\n", message);
70+
if (!ignore_warning) {
71+
gd_error("gd-jpeg, libjpeg: recoverable error: %s\n", message);
72+
}
7173
}
7274

7375
jpeg_info->err->num_warnings++;
7476
} else {
7577
/* strace msg, Show it if trace_level >= level. */
7678
if (jpeg_info->err->trace_level >= level) {
77-
gd_error_ex(GD_NOTICE, "gd-jpeg, libjpeg: strace message: %s\n", message);
79+
if (!ignore_warning) {
80+
gd_error("gd-jpeg, libjpeg: strace message: %s\n", message);
81+
}
7882
}
7983
}
8084
return 1;
@@ -86,9 +90,10 @@ static long php_jpeg_emit_message(j_common_ptr jpeg_info, int level)
8690
static void fatal_jpeg_error (j_common_ptr cinfo)
8791
{
8892
jmpbuf_wrapper *jmpbufw;
93+
char buffer[JMSG_LENGTH_MAX];
8994

90-
gd_error("gd-jpeg: JPEG library reports unrecoverable error: ");
91-
(*cinfo->err->output_message) (cinfo);
95+
(*cinfo->err->format_message)(cinfo, buffer);
96+
gd_error_ex(GD_WARNING, "gd-jpeg: JPEG library reports unrecoverable error: %s", buffer);
9297

9398
jmpbufw = (jmpbuf_wrapper *) cinfo->client_data;
9499
jpeg_destroy (cinfo);

ext/gd/tests/bug77195.jpeg

1.01 KB
Loading

ext/gd/tests/bug77195.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #77195 (Incorrect error handling of imagecreatefromjpeg())
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
if (!gd_info()['JPEG Support']) die('skip JPEG support not available');
7+
?>
8+
--FILE--
9+
<?php
10+
$filename = __DIR__ . '/bug77195.jpeg';
11+
@imagecreatefromjpeg($filename);
12+
imagecreatefromjpeg($filename);
13+
?>
14+
===DONE===
15+
--EXPECTF--
16+
Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: JPEG datastream contains no image in %s on line %d
17+
18+
Warning: imagecreatefromjpeg(): '/mnt/c/Users/cmb/php-dev/php-src/ext/gd/tests/bug77195.jpeg' is not a valid JPEG file in %s on line %d
19+
===DONE===

0 commit comments

Comments
 (0)