From: Zdenek Kabelac Date: Wed, 25 Jan 2012 21:47:18 +0000 (+0000) Subject: Fix resource leak of file handle X-Git-Tag: v2_02_91~81 X-Git-Url: https://sourceware.org/git/?a=commitdiff_plain;h=0e6cacbbb6bfaf2ca5e15b5d1c6d3b9013c23103;p=lvm2.git Fix resource leak of file handle Introduces when added dm_device_get_name. Close file handle in all error paths. --- diff --git a/libdm/libdm-common.c b/libdm/libdm-common.c index 15ab8643f..a4a351edb 100644 --- a/libdm/libdm-common.c +++ b/libdm/libdm-common.c @@ -1189,7 +1189,8 @@ const char *dm_uuid_prefix(void) static int _sysfs_get_dm_name(uint32_t major, uint32_t minor, char *buf, size_t buf_size) { char *sysfs_path, *temp_buf; - FILE *fp; + FILE *fp = NULL; + int r = 0; if (!(sysfs_path = dm_malloc(PATH_MAX)) || !(temp_buf = dm_malloc(PATH_MAX))) { @@ -1219,23 +1220,21 @@ static int _sysfs_get_dm_name(uint32_t major, uint32_t minor, char *buf, size_t } temp_buf[strlen(temp_buf) - 1] = '\0'; - if (fclose(fp)) - log_sys_error("fclose", sysfs_path); - if (buf_size < strlen(temp_buf) + 1) { log_error("_sysfs_get_dm_name: supplied buffer too small"); goto error; } strncpy(buf, temp_buf, buf_size); - dm_free(sysfs_path); - dm_free(temp_buf); - return 1; - + r = 1; error: + if (fp && fclose(fp)) + log_sys_error("fclose", sysfs_path); + dm_free(sysfs_path); dm_free(temp_buf); - return 0; + + return r; } static int _sysfs_get_kernel_name(uint32_t major, uint32_t minor, char *buf, size_t buf_size)