Skip to content

Commit e5edbd0

Browse files
committed
Fix uninitializde heredoc_tag use in readline
Could happen if "<<<" is directly followed by a newline.
1 parent ce40850 commit e5edbd0

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Diff for: ext/readline/readline_cli.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int cli_is_valid_code(char *code, size_t len, zend_string **prompt) /* {{
210210
int brace_count = 0;
211211
size_t i;
212212
php_code_type code_type = body;
213-
char *heredoc_tag;
213+
char *heredoc_tag = NULL;
214214
size_t heredoc_len;
215215

216216
for (i = 0; i < len; ++i) {
@@ -282,6 +282,7 @@ static int cli_is_valid_code(char *code, size_t len, zend_string **prompt) /* {{
282282
if (i + 2 < len && code[i+1] == '<' && code[i+2] == '<') {
283283
i += 2;
284284
code_type = heredoc_start;
285+
heredoc_tag = NULL;
285286
heredoc_len = 0;
286287
}
287288
break;
@@ -333,17 +334,23 @@ static int cli_is_valid_code(char *code, size_t len, zend_string **prompt) /* {{
333334
break;
334335
case '\r':
335336
case '\n':
336-
code_type = heredoc;
337+
if (heredoc_tag) {
338+
code_type = heredoc;
339+
} else {
340+
/* Malformed heredoc without label */
341+
code_type = body;
342+
}
337343
break;
338344
default:
339-
if (!heredoc_len) {
345+
if (!heredoc_tag) {
340346
heredoc_tag = code+i;
341347
}
342348
heredoc_len++;
343349
break;
344350
}
345351
break;
346352
case heredoc:
353+
ZEND_ASSERT(heredoc_tag);
347354
if (code[i - (heredoc_len + 1)] == '\n' && !strncmp(code + i - heredoc_len, heredoc_tag, heredoc_len) && code[i] == '\n') {
348355
code_type = body;
349356
} else if (code[i - (heredoc_len + 2)] == '\n' && !strncmp(code + i - heredoc_len - 1, heredoc_tag, heredoc_len) && code[i-1] == ';' && code[i] == '\n') {

0 commit comments

Comments
 (0)