Skip to content

Commit 12c59f6

Browse files
committed
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.
1 parent ceae816 commit 12c59f6

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ PHP NEWS
1414
. Fixed bug #79664 (PDOStatement::getColumnMeta fails on empty result set).
1515
(cmb)
1616

17+
- Standard:
18+
. Fixed bug #74267 (segfault with streams and invalid data). (cmb)
19+
1720
?? ??? ????, PHP 7.3.19
1821

1922
- Core:

ext/standard/filters.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
788788
lb_ptr = inst->lb_ptr;
789789
lb_cnt = inst->lb_cnt;
790790

791-
if ((in_pp == NULL || in_left_p == NULL) && (lb_ptr >=lb_cnt)) {
791+
if (in_pp == NULL || in_left_p == NULL) {
792792
return PHP_CONV_ERR_SUCCESS;
793793
}
794794

@@ -1016,7 +1016,7 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
10161016
lb_ptr = inst->lb_ptr;
10171017
lb_cnt = inst->lb_cnt;
10181018

1019-
if ((in_pp == NULL || in_left_p == NULL) && lb_cnt == lb_ptr) {
1019+
if (in_pp == NULL || in_left_p == NULL) {
10201020
if (inst->scan_stat != 0) {
10211021
return PHP_CONV_ERR_UNEXPECTED_EOS;
10221022
}
@@ -1113,8 +1113,7 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
11131113
*ps == (unsigned char)inst->lbchars[lb_cnt]) {
11141114
lb_cnt++;
11151115
scan_stat = 5;
1116-
}
1117-
if (*ps != '\t' && *ps != ' ') {
1116+
} else if (*ps != '\t' && *ps != ' ') {
11181117
err = PHP_CONV_ERR_INVALID_SEQ;
11191118
goto out;
11201119
}
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)