Skip to content

Commit

Permalink
More memory leak fixes - nonexistent HISTFILE in particular #379 #353 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorka committed Dec 21, 2019
1 parent cb431c6 commit e9452d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
11 changes: 9 additions & 2 deletions build/test-memleaks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

#export OPT_ALL_SCENARIOS=true

# both valid and INVALID history file to be tested
#export HISTFILE=
export HISTFILE="/tmp/invalid-history-file-01"
#export HISTFILE="~/.bash_history"
#export HISTFILE="~/.zhistory"
#export HISTFILE="~/.zshrc"

# build
cd .. && qmake CONFIG+=hstrdebug hstr.pro && make clean && make -j 8
if [ ${?} -ne 0 ]
Expand All @@ -35,10 +42,10 @@ then
echo "./hstr --show-configuration" >> ${FILE_SCENARIOS}
echo "./hstr --show-zsh-configuration" >> ${FILE_SCENARIOS}
echo "./hstr --show-blacklist" >> ${FILE_SCENARIOS}
echo "./hstr -n sudo" >> ${FILE_SCENARIOS}
echo "./hstr --non-interactive echo" >> ${FILE_SCENARIOS}
echo "./hstr -n log" >> ${FILE_SCENARIOS}
else
echo "./hstr -n log" > ${FILE_SCENARIOS}
echo "./hstr -k" > ${FILE_SCENARIOS}
fi
# ELSE following scenarios must be run MANUALLY from CLI
# valgrind --track-origins=yes --tool=memcheck --leak-check=full --show-leak-kinds=all ./hstr
Expand Down
6 changes: 2 additions & 4 deletions src/hstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ void loop_to_select(void)
case K_CTRL_G:
case K_ESC:
result=NULL;
history_clear_dirty();
history_mgmt_clear_dirty();
done=TRUE;
break;
case K_CTRL_X:
Expand Down Expand Up @@ -1660,9 +1660,7 @@ void hstr_interactive(void)
stdout_history_and_return();
}
history_mgmt_flush();
} else {
printf("No history - nothing to suggest...\n");
}
} // else (no history) handled in create() method

hstr_exit(EXIT_SUCCESS);
}
Expand Down
45 changes: 25 additions & 20 deletions src/hstr_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,25 @@ bool is_hist_timestamp(const char* line)
return (i >= 11);
}

HistoryItems* prioritized_history_create(int optionBigKeys, HashSet *blacklist)
bool history_mgmt_load_history_file(void)
{
using_history();

char *historyFile = get_history_file_name();
// TODO read_history() > add_history() w/ memory leak?
// TODO memleak in readline lib: read_history() > add_history()
if(read_history(historyFile)!=0) {
fprintf(stderr, "\nUnable to read history file from '%s'!\n", historyFile);
// TODO memory leaks on prompt exit
// TODO make this function (one other occurence in this file)
exit(EXIT_FAILURE);
free(historyFile);
return false;
}
free(historyFile);
return true;
}

HistoryItems* prioritized_history_create(int optionBigKeys, HashSet *blacklist)
{
using_history();
if(!history_mgmt_load_history_file()) {
return NULL;
}
HISTORY_STATE* historyState=history_get_history_state();

bool isZsh = isZshParentShell();
Expand Down Expand Up @@ -236,6 +242,7 @@ HistoryItems* prioritized_history_create(int optionBigKeys, HashSet *blacklist)
return prioritizedHistory;
} else {
// history/readline cleanup, clear_history() called on exit as entries are used by raw view
printf("No history - nothing to suggest...\n");
free(historyState);
return NULL;
}
Expand Down Expand Up @@ -272,7 +279,7 @@ void history_mgmt_open(void)
dirty=false;
}

void history_clear_dirty(void)
void history_mgmt_clear_dirty(void)
{
dirty=false;
}
Expand Down Expand Up @@ -319,18 +326,11 @@ int history_mgmt_remove_from_system_history(char *cmd)
bool history_mgmt_remove_last_history_entry(bool verbose)
{
using_history();

char *historyFile = get_history_file_name();
// TODO read_history() > add_history() w/ memory leak?
if(read_history(historyFile)!=0) {
fprintf(stderr, "\nUnable to read history file from '%s'!\n", historyFile);
// TODO memory leaks on prompt exit
// TODO make this function (one other occurence in this file)
exit(EXIT_FAILURE);
if(!history_mgmt_load_history_file()) {
return false;
}
free(historyFile);

HISTORY_STATE *historyState=history_get_history_state();

// delete the last command + the command that was used to run HSTR
if(historyState->length > 1) {
// length is NOT updated on history entry removal
Expand All @@ -339,14 +339,19 @@ bool history_mgmt_remove_last_history_entry(bool verbose)
}
free_history_entry(remove_history(historyState->length-1));
free_history_entry(remove_history(historyState->length-2));
write_history(get_history_file_name());
char *historyFile = get_history_file_name();
write_history(historyFile);
free(historyState);
free(historyFile);

return true;
}
free(historyState);

if(verbose) {
fprintf(stderr, "Unable to delete the last command from history.\n");
}

free(historyState);
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/include/hstr_history.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ HistoryItems* prioritized_history_create(int optionBigKeys, HashSet* blacklist);
void prioritized_history_destroy(HistoryItems* h);

void history_mgmt_open(void);
void history_clear_dirty(void);
void history_mgmt_clear_dirty(void);
bool history_mgmt_load_history_file(void);
int history_mgmt_remove_from_system_history(char* cmd);
bool history_mgmt_remove_last_history_entry(bool verbose);
int history_mgmt_remove_from_raw(char* cmd, HistoryItems* history);
Expand Down

0 comments on commit e9452d2

Please sign in to comment.