Skip to content

Commit bfa4fe4

Browse files
nirsberkerpeksag
authored andcommitted
[2.7] 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 is a backport of the actual fix from master without the test, since the test depends on new run_pty() helper which is not available in 2.7.
1 parent 5d1554a commit bfa4fe4

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix segfault in readline when using readline's history-size option. Patch
2+
by Nir Soffer.

Modules/readline.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,15 +1161,17 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
11611161
if (n > 0) {
11621162
const char *line;
11631163
int length = _py_get_history_length();
1164-
if (length > 0)
1164+
if (length > 0) {
1165+
HIST_ENTRY *hist_ent;
11651166
#ifdef __APPLE__
11661167
if (using_libedit_emulation) {
11671168
/* handle older 0-based or newer 1-based indexing */
1168-
line = history_get(length + libedit_history_start - 1)->line;
1169+
hist_ent = history_get(length + libedit_history_start - 1);
11691170
} else
11701171
#endif /* __APPLE__ */
1171-
line = history_get(length)->line;
1172-
else
1172+
hist_ent = history_get(length);
1173+
line = hist_ent ? hist_ent->line : "";
1174+
} else
11731175
line = "";
11741176
if (strcmp(p, line))
11751177
add_history(p);

0 commit comments

Comments
 (0)