From b1e37ba0fb6fe83e823140a57cbbe79fd485d86e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 8 Mar 2012 17:10:55 +0000 Subject: Fix memory leaks. git-svn-id: http://svn.drobilla.net/lad/trunk/lilv@4028 a436a847-0d15-0410-975c-d299462d15a1 --- src/util.c | 70 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 33 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index ff881da..9e55393 100644 --- a/src/util.c +++ b/src/util.c @@ -539,6 +539,17 @@ lilv_mkdir_p(const char* dir_path) return 0; } +static off_t +lilv_file_size(const char* path) +{ + struct stat buf; + if (stat(path, &buf)) { + LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno)); + return 0; + } + return buf.st_size; +} + bool lilv_file_equals(const char* a_path, const char* b_path) { @@ -546,46 +557,39 @@ lilv_file_equals(const char* a_path, const char* b_path) return true; // Paths match } + bool match = false; + FILE* a_file = NULL; + FILE* b_file = NULL; char* const a_real = lilv_realpath(a_path); char* const b_real = lilv_realpath(b_path); if (!a_real || !b_real) { - return false; // Missing file matches nothing - } - - if (!strcmp(a_real, b_real)) { - return true; // Real paths match - } - - off_t a_size; - off_t b_size; - lilv_size_mtime(a_path, &a_size, NULL); - lilv_size_mtime(b_path, &b_size, NULL); - if (a_size != b_size) { - return false; // Sizes differ - } - - FILE* a_file = fopen(a_real, "rb"); - if (!a_file) { - return false; // Missing file matches nothing + match = false; // Missing file matches nothing + } else if (!strcmp(a_real, b_real)) { + match = true; // Real paths match + } else if (lilv_file_size(a_path) != lilv_file_size(b_path)) { + match = false; // Sizes differ + } else if (!(a_file = fopen(a_real, "rb"))) { + match = false; // Missing file matches nothing + } else if (!(b_file = fopen(b_real, "rb"))) { + match = false; // Missing file matches nothing + } else { + match = true; + // TODO: Improve performance by reading chunks + while (!feof(a_file) && !feof(b_file)) { + if (fgetc(a_file) != fgetc(b_file)) { + match = false; + break; + } + } } - FILE* b_file = fopen(b_real, "rb"); - if (!b_file) { + if (a_file) { fclose(a_file); - return false; // Missing file matches nothing } - - bool match = true; - - // TODO: Improve performance by reading chunks - while (!feof(a_file) && !feof(b_file)) { - if (fgetc(a_file) != fgetc(b_file)) { - match = false; - break; - } + if (b_file) { + fclose(b_file); } - - fclose(a_file); - fclose(b_file); + free(a_real); + free(b_real); return match; } -- cgit v1.2.1