From 40010e3eb823ad6a36df076839b150d01d7a412d Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Fri, 25 Oct 2024 00:21:49 +0200 Subject: [PATCH] clang: close file on memory alloc error path --- libdm/dm-tools/dmsetup.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libdm/dm-tools/dmsetup.c b/libdm/dm-tools/dmsetup.c index 987058196..b2cf8009f 100644 --- a/libdm/dm-tools/dmsetup.c +++ b/libdm/dm-tools/dmsetup.c @@ -406,7 +406,7 @@ static int _parse_file(struct dm_task *dmt, const char *file) buffer_size = LINE_SIZE; if (!(buffer = malloc(buffer_size))) { log_error("Failed to malloc line buffer."); - return 0; + goto out; } while (fgets(buffer, (int) buffer_size, fp)) @@ -419,12 +419,11 @@ static int _parse_file(struct dm_task *dmt, const char *file) r = 1; out: - memset(buffer, 0, buffer_size); -#ifndef HAVE_GETLINE - free(buffer); -#else - free(buffer); -#endif + if (buffer) { + memset(buffer, 0, buffer_size); + free(buffer); + } + if (file && fclose(fp)) log_sys_debug("fclose", file);