From 538169fb5ba12220c52ecd2c551438f977ca0bf4 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Wed, 21 Sep 2011 22:42:18 +0000 Subject: [PATCH] Fix some memory leaks (thanks to okozina). git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@610 36d66b0a-2a48-0410-832c-cd162a569da5 --- lib/utils_devpath.c | 8 ++++++-- src/cryptsetup.c | 6 +++++- tests/api-test.c | 6 ++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/utils_devpath.c b/lib/utils_devpath.c index 2844f35..5fd7981 100644 --- a/lib/utils_devpath.c +++ b/lib/utils_devpath.c @@ -169,7 +169,7 @@ char *crypt_lookup_dev(const char *dev_id) static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor) { - char path[PATH_MAX], tmp[64]; + char path[PATH_MAX], tmp[64] = {0}; int fd, r = 0; if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0) @@ -180,7 +180,11 @@ static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor r = read(fd, tmp, sizeof(tmp)); close(fd); - if (r <= 0 || sscanf(tmp, "%d:%d", major, minor) != 2) + if (r <= 0) + return 0; + + tmp[63] = '\0'; + if (sscanf(tmp, "%d:%d", major, minor) != 2) return 0; return 1; diff --git a/src/cryptsetup.c b/src/cryptsetup.c index 29e6e6f..400ed96 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -1046,6 +1046,7 @@ static __attribute__ ((noreturn)) void usage(poptContext popt_context, poptPrintUsage(popt_context, stderr, 0); if (error) log_err("%s: %s\n", more, error); + poptFreeContext(popt_context); exit(exitcode); } @@ -1234,6 +1235,7 @@ int main(int argc, const char **argv) poptBadOption(popt_context, POPT_BADOPTION_NOALIAS)); if (opt_version_mode) { log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION); + poptFreeContext(popt_context); exit(EXIT_SUCCESS); } @@ -1345,5 +1347,7 @@ int main(int argc, const char **argv) _dbg_version_and_cmd(argc, argv); } - return run_action(action); + r = run_action(action); + poptFreeContext(popt_context); + return r; } diff --git a/tests/api-test.c b/tests/api-test.c index 5d7dd22..81e36a9 100644 --- a/tests/api-test.c +++ b/tests/api-test.c @@ -355,6 +355,12 @@ static void _cleanup(void) remove(BACKUP_FILE); _remove_keyfiles(); + + free(tmp_file_1); + free(test_loop_file); + free(THE_LOOP_DEV); + free(DEVICE_1); + free(DEVICE_2); } static int _setup(void) -- 2.7.4