From 653e2172e03590a1c7f5ef6f2dd7daf10a72ef86 Mon Sep 17 00:00:00 2001 From: Ananth Date: Sat, 17 Apr 2021 20:48:22 -0700 Subject: [PATCH 3/8] scrub private history when exiting private mode --- bashhist.h | 1 + builtins/set.def | 11 +++++++---- lib/readline/history.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/bashhist.h b/bashhist.h index 8061bac..7eeaa88 100644 --- a/bashhist.h +++ b/bashhist.h @@ -71,6 +71,7 @@ extern void bash_history_reinit PARAMS((int)); extern void bash_history_disable PARAMS((void)); extern void bash_history_enable PARAMS((void)); extern void bash_clear_history PARAMS((void)); +extern void cleanup_private_history PARAMS((void)); extern int bash_delete_histent PARAMS((int)); extern int bash_delete_history_range PARAMS((int, int)); extern int bash_delete_last_history PARAMS((void)); diff --git a/builtins/set.def b/builtins/set.def index 150e153..589b9d9 100644 --- a/builtins/set.def +++ b/builtins/set.def @@ -469,15 +469,18 @@ bash_set_history (on_or_off, option_name) if (on_or_off == FLAG_ON) { private_history_mode = 0; + cleanup_private_history(); } else { private_history_mode = 1; } - enable_history_list = 1; - bash_history_enable (); - if (history_lines_this_session == 0) - load_history (); + + enable_history_list = 1; + bash_history_enable (); + if (history_lines_this_session == 0) + load_history (); + return (1 - enable_history_list); } #endif diff --git a/lib/readline/history.c b/lib/readline/history.c index 409daac..04f1276 100644 --- a/lib/readline/history.c +++ b/lib/readline/history.c @@ -621,3 +621,31 @@ clear_history (void) history_offset = history_length = 0; history_base = 1; /* reset history base to default */ } + +extern int history_lines_this_session; + +void +cleanup_private_history (void) +{ + register int i; + + /* This loses because we cannot free the data. */ + for (i = history_length - 1; i > 0; i--) + { + HIST_ENTRY *hist_entry = the_history[i]; + if(hist_entry) + { + if(hist_entry->private) + { + HIST_ENTRY *discard = remove_history(i); + if (discard) + { + free_history_entry (discard); + history_lines_this_session--; + } + } + } + } + + history_offset = history_length; +} -- 2.31.1