Skip to content

Commit e35095d

Browse files
committed
Fix crash in replxx due to overlap of history entries
Refs: ClickHouse/replxx#31 Refs: AmokHuginnsson/replxx#160
1 parent 239aa25 commit e35095d

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

contrib/replxx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/expect -f
2+
3+
# Regression test for heap-use-after-free in replxx::History::erase
4+
# https://github.com/AmokHuginnsson/replxx/pull/160
5+
6+
set basedir [file dirname $argv0]
7+
set basename [file tail $argv0]
8+
if {[info exists env(CLICKHOUSE_TMP)]} {
9+
set CLICKHOUSE_TMP $env(CLICKHOUSE_TMP)
10+
} else {
11+
set CLICKHOUSE_TMP "."
12+
}
13+
exp_internal -f $CLICKHOUSE_TMP/$basename.debuglog 0
14+
set history_file $CLICKHOUSE_TMP/$basename.history
15+
16+
log_user 0
17+
set timeout 60
18+
match_max 100000
19+
20+
expect_after {
21+
# Do not ignore eof from expect
22+
-i $any_spawn_id eof { exp_continue }
23+
# A default timeout action is to do nothing, change it to fail
24+
-i $any_spawn_id timeout { exit 1 }
25+
}
26+
27+
spawn bash -c "source $basedir/../shell_config.sh ; \$CLICKHOUSE_CLIENT_BINARY \$CLICKHOUSE_CLIENT_EXPECT_OPT --history_file=$history_file"
28+
expect ":) "
29+
30+
send -- "SELECT 1\r"
31+
expect ":) "
32+
send -- "SELECT 2\r"
33+
expect ":) "
34+
35+
send -- "SELECT '1'\r"
36+
expect ":) "
37+
send -- "SELECT '2'\r"
38+
expect ":) "
39+
40+
# Now rewrite:
41+
# - SELECT 2 -> SELECT '2'
42+
# - SELECT 1 -> SELECT '1'
43+
# This is needed to overlap the entries via scratch
44+
45+
# Used key codes:
46+
# - "\033\[A" -> UP
47+
# - "\x7f" -> backspace
48+
49+
# SELECT 2 -> SELECT '2'
50+
send -- "\033\[A"
51+
send -- "\033\[A"
52+
send -- "\033\[A"
53+
expect "SELECT 2"
54+
send -- "\x7f"
55+
send -- "'2'"
56+
# SELECT 1 -> SELECT '1'
57+
send -- "\033\[A"
58+
expect "SELECT 1"
59+
send -- "\x7f"
60+
send -- "'1'"
61+
# Commit
62+
send -- "\r"
63+
64+
send -- "SELECT '2'\r"
65+
expect ":) "
66+
send -- "SELECT 2\r"
67+
expect ":) "
68+
send -- "SELECT '2'\r"
69+
expect ":) "
70+
send -- "\033\[A"
71+
send -- "\033\[A"
72+
expect "SELECT 2"
73+
# This should trigger heap-use-after-free in replxx::History::erase()
74+
send -- "\r"
75+
expect ":) "
76+
77+
send -- "exit\r"
78+
expect eof

tests/queries/0_stateless/03520_replxx_history_crash.reference

Whitespace-only changes.

0 commit comments

Comments
 (0)