Skip to content

ext/readline: readline_info fix usage when the buffer is not initialised #15139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions ext/readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@ PHP_FUNCTION(readline_info)
if (!try_convert_to_string(value)) {
RETURN_THROWS();
}
#ifndef PHP_WIN32
if (strlen(oldstr) < Z_STRLEN_P(value)) {
#if !defined(PHP_WIN32) && !HAVE_LIBEDIT
if (!rl_line_buffer) {
rl_line_buffer = malloc(Z_STRLEN_P(value) + 1);
} else if (strlen(oldstr) < Z_STRLEN_P(value)) {
rl_extend_line_buffer(Z_STRLEN_P(value) + 1);
oldstr = rl_line_buffer;
}
memcpy(rl_line_buffer, Z_STRVAL_P(value), Z_STRLEN_P(value) + 1);
rl_end = Z_STRLEN_P(value);
#else
char *tmp = strdup(Z_STRVAL_P(value));
if (tmp) {
Expand All @@ -200,6 +202,9 @@ PHP_FUNCTION(readline_info)
}
rl_line_buffer = tmp;
}
#endif
#if !defined(PHP_WIN32)
rl_end = Z_STRLEN_P(value);
#endif
}
RETVAL_STRING(SAFE_STRING(oldstr));
Expand Down
31 changes: 31 additions & 0 deletions ext/readline/tests/readline_info_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
readline_info(): using line_buffer before rl_line_buffer is initialised.
--EXTENSIONS--
readline
--SKIPIF--
<?php if (READLINE_LIB == "libedit") die("skip readline only");
if (getenv('SKIP_REPEAT')) die("skip readline has global state");
?>
--FILE--
<?php

$name = tempnam('/tmp', 'readline.tmp');

var_dump(readline_info('line_buffer'));
readline_info('line_buffer', 'abcdef');
var_dump(readline_info('line_buffer'));
readline_add_history('123');
readline_write_history($name);
readline_info('line_buffer', 'abcdefghijkl');
var_dump(readline_info('line_buffer'));

var_dump(file_get_contents($name));

unlink($name);
?>
--EXPECTF--
string(0) ""
string(6) "abcdef"
string(12) "abcdefghijkl"
string(4) "123
"
Loading