Fix tab completion segfault in reverse search

Backport of 6145e2246e8
for #8315