Skip to content

Commit 9fcda61

Browse files
committed
bpo-29854: Fix segfault in call_readline()
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.
1 parent e6ea330 commit 9fcda61

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

Lib/test/test_readline.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ def display(substitution, matches, longest_match_length):
212212

213213
@unittest.skipIf(is_editline,
214214
"editline history size configuration is broken")
215-
@unittest.expectedFailure
216215
def test_history_size(self):
217216
history_size = 10
218217
with temp_dir() as test_dir:
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
@@ -1347,15 +1347,17 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
13471347
if (should_auto_add_history && n > 0) {
13481348
const char *line;
13491349
int length = _py_get_history_length();
1350-
if (length > 0)
1350+
if (length > 0) {
1351+
HIST_ENTRY *hist_ent;
13511352
#ifdef __APPLE__
13521353
if (using_libedit_emulation) {
13531354
/* handle older 0-based or newer 1-based indexing */
1354-
line = (const char *)history_get(length + libedit_history_start - 1)->line;
1355+
hist_ent = history_get(length + libedit_history_start - 1);
13551356
} else
13561357
#endif /* __APPLE__ */
1357-
line = (const char *)history_get(length)->line;
1358-
else
1358+
hist_ent = history_get(length);
1359+
line = hist_ent ? hist_ent->line : "";
1360+
} else
13591361
line = "";
13601362
if (strcmp(p, line))
13611363
add_history(p);

0 commit comments

Comments
 (0)