Skip to content

Commit c173da7

Browse files
cmb69remicollet
authored andcommitted
Fix #74267: segfault with streams and invalid data
If the current character is a line break character, it cannot be a tab or space character, so we would always fail with an invalid sequence error. Obviously, these `scan_stat == 4` conditions are meant to be exclusive. Furthermore, if `in_pp == NULL || in_left_p == NULL` is true, we hit a segfault if we are not returning right away. Obviously, the additional constraints don't make sense, so we remove them. (cherry picked from commit 12c59f6) (cherry picked from commit d149a44)
1 parent 0abb863 commit c173da7

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

ext/standard/filters.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
793793
lb_ptr = inst->lb_ptr;
794794
lb_cnt = inst->lb_cnt;
795795

796-
if ((in_pp == NULL || in_left_p == NULL) && (lb_ptr >=lb_cnt)) {
796+
if (in_pp == NULL || in_left_p == NULL) {
797797
return PHP_CONV_ERR_SUCCESS;
798798
}
799799

@@ -1021,7 +1021,7 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
10211021
lb_ptr = inst->lb_ptr;
10221022
lb_cnt = inst->lb_cnt;
10231023

1024-
if ((in_pp == NULL || in_left_p == NULL) && lb_cnt == lb_ptr) {
1024+
if (in_pp == NULL || in_left_p == NULL) {
10251025
if (inst->scan_stat != 0) {
10261026
return PHP_CONV_ERR_UNEXPECTED_EOS;
10271027
}
@@ -1118,8 +1118,7 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
11181118
*ps == (unsigned char)inst->lbchars[lb_cnt]) {
11191119
lb_cnt++;
11201120
scan_stat = 5;
1121-
}
1122-
if (*ps != '\t' && *ps != ' ') {
1121+
} else if (*ps != '\t' && *ps != ' ') {
11231122
err = PHP_CONV_ERR_INVALID_SEQ;
11241123
goto out;
11251124
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #74267 (segfault with streams and invalid data)
3+
--FILE--
4+
<?php
5+
$stream = fopen('php://memory', 'w');
6+
stream_filter_append($stream, 'convert.quoted-printable-decode', STREAM_FILTER_WRITE, ['line-break-chars' => "\r\n"]);
7+
8+
$lines = [
9+
"\r\n",
10+
" -=()\r\n",
11+
" -=\r\n",
12+
"\r\n"
13+
];
14+
15+
foreach ($lines as $line) {
16+
fwrite($stream, $line);
17+
}
18+
19+
fclose($stream);
20+
echo "done\n";
21+
?>
22+
--EXPECTF--
23+
Warning: fwrite(): stream filter (convert.quoted-printable-decode): invalid byte sequence in %s on line %d
24+
25+
Warning: fwrite(): stream filter (convert.quoted-printable-decode): invalid byte sequence in %s on line %d
26+
done

0 commit comments

Comments
 (0)