mirror of
https://github.com/dvorka/hstr.git
synced 2024-12-28 18:50:54 +08:00
Fixing various memory leaks #370
This commit is contained in:
parent
4976bab73f
commit
463dcdd9ba
11 changed files with 80 additions and 51 deletions
|
@ -1,3 +1,8 @@
|
|||
2019-12-21 Martin Dvorak <martin.dvorak@mindforger.com>
|
||||
|
||||
* Released v2.2.0 - toggle visibility/move help lines (Fabian Saldias),
|
||||
zsh support fixes, several memory leaks fixed.
|
||||
|
||||
2019-12-08 Martin Dvorak <martin.dvorak@mindforger.com>
|
||||
|
||||
* Released v2.1.0 - significant zsh support improvements and stabilization.
|
||||
|
|
2
PKGBUILD
2
PKGBUILD
|
@ -4,7 +4,7 @@
|
|||
# Contributor: Busindre <busilezas at busindre.com>
|
||||
|
||||
pkgname=hstr
|
||||
pkgver=2.1
|
||||
pkgver=2.2
|
||||
pkgrel=1
|
||||
pkgdesc="A command line utility that brings improved BASH command completion from the history. It aims to make completion easier and more efficient than Ctrl-r."
|
||||
arch=('any')
|
||||
|
|
|
@ -103,7 +103,7 @@ function buildDebPackage() {
|
|||
#
|
||||
# 2) create source deb
|
||||
#
|
||||
# 2.1) add Debian control files
|
||||
# 2.a) add Debian control files
|
||||
cp -rvf ${HSTRSRC}/build/debian/debian .
|
||||
createChangelog ./debian/changelog
|
||||
|
||||
|
@ -119,7 +119,7 @@ function buildDebPackage() {
|
|||
DEBFULLNAME="Martin Dvorak"
|
||||
export DEBEMAIL DEBFULLNAME
|
||||
|
||||
# 2.2) build BINARY deb
|
||||
# 2.b) build BINARY deb
|
||||
# https://www.debian.org/doc/manuals/maint-guide/build.en.html
|
||||
#debuild -us -uc
|
||||
dpkg-buildpackage -us -uc
|
||||
|
@ -127,7 +127,7 @@ function buildDebPackage() {
|
|||
|
||||
# 3) create source deb
|
||||
#
|
||||
# 3.1) build deb
|
||||
# 3.a) build deb
|
||||
# build SOURCE deb package
|
||||
#debuild -S
|
||||
dpkg-buildpackage -S
|
||||
|
@ -147,8 +147,8 @@ then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
export ARG_BAZAAR_MSG="HSTR 2.1.0"
|
||||
export ARG_VERSION="2.1.0"
|
||||
export ARG_BAZAAR_MSG="HSTR 2.2.0"
|
||||
export ARG_VERSION="2.2.0"
|
||||
|
||||
# Debian releases: https://www.debian.org/releases/
|
||||
# 6/7/8/9/10: squeeze wheezy jessie stretch buster
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
# This script is available from http://www.mindforger.com/fedora/fedora-rpm-from-deb.sh
|
||||
# to be easily available in VMs
|
||||
|
||||
export MFVERSION="2.1.0"
|
||||
export MFVERSION="2.2.0"
|
||||
export MFPRJNAME="hstr-${MFVERSION}"
|
||||
export AMD64NAME="hstr_${MFVERSION}-1_amd64"
|
||||
export I386NAME="hstr_${MFVERSION}-1_i386"
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
export SCRIPT_HOME=`pwd`
|
||||
|
||||
export HSTR_VERSION="2.1.0"
|
||||
export HSTR_VERSION="2.2.0"
|
||||
|
||||
export NOW=`date +%Y-%m-%d--%H-%M-%S`
|
||||
export GH_RELEASE_DIR=~/p/hstr/release
|
||||
|
|
|
@ -16,8 +16,7 @@
|
|||
|
||||
# Run HSTR w/ 1 history entry to hunt memleaks w/ valgrind
|
||||
|
||||
# true or ""
|
||||
export OPT_ALL_SCENARIOS=true
|
||||
#export OPT_ALL_SCENARIOS=true
|
||||
|
||||
# build
|
||||
cd .. && qmake CONFIG+=hstrdebug hstr.pro && make clean && make -j 8
|
||||
|
@ -31,18 +30,20 @@ export FILE_SCENARIOS="/tmp/hstr-scenarios.txt"
|
|||
if [ ${OPT_ALL_SCENARIOS} ]
|
||||
then
|
||||
echo "./hstr --help" > ${FILE_SCENARIOS}
|
||||
echo "./hstr --show-configuration" >> ${FILE_SCENARIOS}
|
||||
echo "./hstr --version" >> ${FILE_SCENARIOS}
|
||||
echo "./hstr --kill-last-command" > ${FILE_SCENARIOS}
|
||||
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 -n log" >> ${FILE_SCENARIOS}
|
||||
else
|
||||
echo "./hstr --help" > ${FILE_SCENARIOS}
|
||||
echo "./hstr a" > ${FILE_SCENARIOS}
|
||||
fi
|
||||
|
||||
# test history file - comment ALL HISTFILE exports below for test w/ production
|
||||
#export HISTFILE=`pwd`/test/resources/.bash_history_valgrind_empty
|
||||
export HISTFILE=`pwd`/test/resources/.bash_history_valgrind_1_entry
|
||||
#export HISTFILE=`pwd`/../test/resources/.bash_history_valgrind_empty
|
||||
#export HISTFILE=`pwd`/../test/resources/.bash_history_valgrind_1_entry
|
||||
|
||||
# run tests w/ Valgrind
|
||||
cat ${FILE_SCENARIOS} | while read SCENARIO
|
||||
|
|
|
@ -163,8 +163,8 @@ then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
export ARG_BAZAAR_MSG="Release 2.1"
|
||||
export ARG_MAJOR_VERSION=2.1.
|
||||
export ARG_BAZAAR_MSG="Release 2.2"
|
||||
export ARG_MAJOR_VERSION=2.2.
|
||||
export ARG_MINOR_VERSION=0 # minor version is incremented for every Ubuntu version
|
||||
|
||||
# https://wiki.ubuntu.com/Releases
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
AC_PREREQ([2.69])
|
||||
|
||||
AC_INIT(hstr, 2.1.0, martin.dvorak@mindforger.com)
|
||||
AC_INIT(hstr, 2.2.0, martin.dvorak@mindforger.com)
|
||||
AC_CONFIG_FILES([Makefile src/Makefile man/Makefile])
|
||||
|
||||
# Check src dir existence.
|
||||
|
|
41
src/hstr.c
41
src/hstr.c
|
@ -137,7 +137,7 @@
|
|||
|
||||
// major.minor.revision
|
||||
static const char* VERSION_STRING=
|
||||
"hstr version \"2.1.0\" (2019-12-08T13:30:00)"
|
||||
"hstr version \"2.2.0\" (2019-12-21T08:47:00)"
|
||||
"\n";
|
||||
|
||||
static const char* HSTR_VIEW_LABELS[]={
|
||||
|
@ -389,18 +389,18 @@ void hstr_destroy(void)
|
|||
free(hstr);
|
||||
}
|
||||
|
||||
void hstr_on_exit(void)
|
||||
void hstr_exit(int status)
|
||||
{
|
||||
history_mgmt_flush();
|
||||
hstr_destroy();
|
||||
exit(status);
|
||||
}
|
||||
|
||||
void signal_callback_handler_ctrl_c(int signum)
|
||||
{
|
||||
if(signum==SIGINT) {
|
||||
history_mgmt_flush();
|
||||
hstr_curses_stop(false);
|
||||
hstr_on_exit();
|
||||
exit(signum);
|
||||
hstr_exit(signum);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1659,10 +1659,12 @@ void hstr_interactive(void)
|
|||
} else {
|
||||
stdout_history_and_return();
|
||||
}
|
||||
hstr_on_exit();
|
||||
history_mgmt_flush();
|
||||
} else {
|
||||
printf("No history - nothing to suggest...\n");
|
||||
}
|
||||
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void hstr_getopt(int argc, char **argv)
|
||||
|
@ -1679,23 +1681,29 @@ void hstr_getopt(int argc, char **argv)
|
|||
break;
|
||||
case 'k':
|
||||
if(history_mgmt_remove_last_history_entry(hstr->verboseKill)) {
|
||||
exit(EXIT_SUCCESS);
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
break;
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
hstr_exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
case 'b':
|
||||
blacklist_load(&hstr->blacklist);
|
||||
blacklist_dump(&hstr->blacklist);
|
||||
exit(EXIT_SUCCESS);
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
break;
|
||||
case 'V':
|
||||
printf("%s", VERSION_STRING);
|
||||
exit(EXIT_SUCCESS);
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
break;
|
||||
case 'h':
|
||||
printf("%s", HELP_STRING);
|
||||
exit(EXIT_SUCCESS);
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
break;
|
||||
case 'z':
|
||||
printf("%s", INSTALL_ZSH_STRING);
|
||||
exit(EXIT_SUCCESS);
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
break;
|
||||
case 's':
|
||||
// ZSH_VERSION is not exported by zsh > detected by parent process name
|
||||
if(isZshParentShell()) {
|
||||
|
@ -1703,12 +1711,12 @@ void hstr_getopt(int argc, char **argv)
|
|||
} else {
|
||||
printf("%s", INSTALL_BASH_STRING);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
break;
|
||||
case '?':
|
||||
default:
|
||||
printf("%s", HELP_STRING);
|
||||
exit(EXIT_SUCCESS);
|
||||
hstr_exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1728,9 +1736,8 @@ int hstr_main(int argc, char* argv[])
|
|||
hstr_getopt(argc, argv);
|
||||
favorites_get(hstr->favorites);
|
||||
blacklist_load(&hstr->blacklist);
|
||||
// hstr cleanup is handled by hstr_exit()
|
||||
hstr_interactive();
|
||||
|
||||
// hstr cleanup is handled by hstr_on_exit()
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -118,6 +118,10 @@ void blacklist_dump(Blacklist *blacklist)
|
|||
char **keys=hashset_keys(blacklist->set);
|
||||
for(i=0; i<size; i++) {
|
||||
printf(" '%s'\n",keys[i]);
|
||||
free(keys[i]);
|
||||
}
|
||||
if(keys) {
|
||||
free(keys);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -104,8 +104,11 @@ HistoryItems* prioritized_history_create(int optionBigKeys, HashSet *blacklist)
|
|||
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);
|
||||
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);
|
||||
|
@ -241,24 +244,26 @@ HistoryItems* prioritized_history_create(int optionBigKeys, HashSet *blacklist)
|
|||
|
||||
void prioritized_history_destroy(HistoryItems* h)
|
||||
{
|
||||
if(h->items) {
|
||||
if(h->count) {
|
||||
unsigned i;
|
||||
for(i=0; i<h->count; i++) {
|
||||
free(h->items[i]);
|
||||
if(h) {
|
||||
if(h->items) {
|
||||
if(h->count) {
|
||||
unsigned i;
|
||||
for(i=0; i<h->count; i++) {
|
||||
free(h->items[i]);
|
||||
}
|
||||
}
|
||||
free(h->items);
|
||||
}
|
||||
free(h->items);
|
||||
|
||||
if(h->rawItems) {
|
||||
free(h->rawItems);
|
||||
}
|
||||
|
||||
free(h);
|
||||
|
||||
// readline/history cleanup
|
||||
free(history_get_history_state());
|
||||
}
|
||||
|
||||
if(h->rawItems) {
|
||||
free(h->rawItems);
|
||||
}
|
||||
|
||||
free(h);
|
||||
|
||||
// readline/history cleanup
|
||||
free(history_get_history_state());
|
||||
clear_history();
|
||||
}
|
||||
|
||||
|
@ -316,10 +321,15 @@ 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);
|
||||
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);
|
||||
|
||||
HISTORY_STATE *historyState=history_get_history_state();
|
||||
// delete the last command + the command that was used to run HSTR
|
||||
if(historyState->length > 1) {
|
||||
|
@ -332,6 +342,8 @@ bool history_mgmt_remove_last_history_entry(bool verbose)
|
|||
write_history(get_history_file_name());
|
||||
return true;
|
||||
}
|
||||
free(historyState);
|
||||
|
||||
if(verbose) {
|
||||
fprintf(stderr, "Unable to delete the last command from history.\n");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue