1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

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

rl_initialise is only called when readline() is used so the global
 buffer might not be initialised yet.
This commit is contained in:
David CARLIER
2024-07-31 11:04:44 +01:00
committed by GitHub
parent 3c68661ec9
commit b7e43bd2b7
2 changed files with 39 additions and 3 deletions

View File

@@ -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) {
@@ -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));

View File

@@ -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
"