[3.6] bpo-29854: Fix segfault in call_readline() (GH-728)

If history-length is set in .inputrc, and the history file is double the
history size (or more), history_get(N) returns NULL, and python
segfaults. Fix that by checking for NULL return value.

It seems that the root cause is incorrect handling of bigger history in
readline, but Python should not segfault even if readline returns
unexpected value.

This issue affects only GNU readline. When using libedit emulation
system history size option does not work.
This commit is contained in:
Nir Soffer 2017-07-08 21:51:21 +03:00 committed by Berker Peksag
parent 03e0df66b8
commit 04f77d4677
3 changed files with 55 additions and 7 deletions

View file

@ -1347,15 +1347,17 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
if (should_auto_add_history && n > 0) {
const char *line;
int length = _py_get_history_length();
if (length > 0)
if (length > 0) {
HIST_ENTRY *hist_ent;
#ifdef __APPLE__
if (using_libedit_emulation) {
/* handle older 0-based or newer 1-based indexing */
line = (const char *)history_get(length + libedit_history_start - 1)->line;
hist_ent = history_get(length + libedit_history_start - 1);
} else
#endif /* __APPLE__ */
line = (const char *)history_get(length)->line;
else
hist_ent = history_get(length);
line = hist_ent ? hist_ent->line : "";
} else
line = "";
if (strcmp(p, line))
add_history(p);