Do not allow empty items to be added to history

Empty items are used as sentinels to indicate that we've reached the end of
history, so they should not be added as actual items. Enforce this.

Fixes #6032
This commit is contained in:
ridiculousfish
2020-01-05 12:38:49 -08:00
parent 24970bb549
commit f1ce967dfa
2 changed files with 15 additions and 0 deletions

View File

@@ -3654,6 +3654,10 @@ void history_tests_t::test_history() {
}
history.save();
// Empty items should just be dropped (#6032).
history.add(L"");
do_test(!history.item_at_index(1).contents.empty());
// Read items back in reverse order and ensure they're the same.
for (i = 100; i >= 1; i--) {
history_item_t item = history.item_at_index(i);

View File

@@ -366,6 +366,11 @@ struct history_impl_t {
};
void history_impl_t::add(const history_item_t &item, bool pending, bool do_save) {
// We use empty items as sentinels to indicate the end of history.
// Do not allow them to be added (#6032).
if (item.contents.empty()) {
return;
}
// Try merging with the last item.
if (!new_items.empty() && new_items.back().merge(item)) {
// We merged, so we don't have to add anything. Maybe this item was pending, but it just got
@@ -1256,6 +1261,12 @@ void history_t::remove(const wcstring &str) { impl()->remove(str); }
void history_t::add_pending_with_file_detection(const wcstring &str,
const wcstring &working_dir_slash) {
// We use empty items as sentinels to indicate the end of history.
// Do not allow them to be added (#6032).
if (str.empty()) {
return;
}
// Find all arguments that look like they could be file paths.
bool needs_sync_write = false;
parse_node_tree_t tree;