summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorNir Soffer <nirsof@gmail.com>2017-07-07 06:10:46 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2017-07-07 06:10:46 (GMT)
commitfae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b (patch)
treee94fb9176f0309f41cb51fa3f16ba616d94d7ebd /Modules
parent25a4206c243e3b1fa6f5b1c72a11b409b007694d (diff)
downloadcpython-fae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b.zip
cpython-fae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b.tar.gz
cpython-fae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b.tar.bz2
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.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/readline.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/Modules/readline.c b/Modules/readline.c
index dfbbb29..6ba1247 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -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);