From d87732b64c4b0b2c202854fa36f89acc1c346a65 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Fri, 25 Mar 2022 12:32:38 +0100 Subject: [PATCH] iio: Fix channel info leak in build_channel_array() On error, the channel info would just be left unfreed. From Coverity: iio-sensor-proxy-3.3/src/iio-buffer-utils.c:330: leaked_storage: Variable "current" going out of scope leaks the storage it points to. iio-sensor-proxy-3.3/src/iio-buffer-utils.c:312: leaked_storage: Variable "current" going out of scope leaks the storage it points to. iio-sensor-proxy-3.3/src/iio-buffer-utils.c:304: leaked_storage: Variable "current" going out of scope leaks the storage it points to. iio-sensor-proxy-3.3/src/iio-buffer-utils.c:302: leaked_storage: Variable "current" going out of scope leaks the storage it points to. iio-sensor-proxy-3.3/src/iio-buffer-utils.c:293: leaked_storage: Variable "current" going out of scope leaks the storage it points to. iio-sensor-proxy-3.3/src/iio-buffer-utils.c:289: leaked_storage: Variable "current" going out of scope leaks the storage it points to. iio-sensor-proxy-3.3/src/iio-buffer-utils.c:350:2: warning[unix.Malloc]: Potential leak of memory pointed to by 'current' --- src/iio-buffer-utils.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/iio-buffer-utils.c b/src/iio-buffer-utils.c index 8a6a779..04ca32f 100644 --- a/src/iio-buffer-utils.c +++ b/src/iio-buffer-utils.c @@ -213,6 +213,8 @@ compare_channel_index (gconstpointer a, gconstpointer b) return (int) (info_1->index - info_2->index); } +G_DEFINE_AUTOPTR_CLEANUP_FUNC(iio_channel_info, channel_info_free) + /* build_channel_array() - function to figure out what channels are present */ static iio_channel_info ** build_channel_array (const char *device_dir, @@ -243,7 +245,7 @@ build_channel_array (const char *device_dir, if (g_str_has_suffix (name, "_en")) { g_autoptr(FILE) sysfsfp = NULL; char *filename, *index_name; - iio_channel_info *current; + g_autoptr(iio_channel_info) current = NULL; filename = g_build_filename (scan_el_dir, name, NULL); sysfsfp = fopen (filename, "r"); @@ -315,7 +317,7 @@ build_channel_array (const char *device_dir, g_warning ("Could not parse name %s, generic name %s", current->name, current->generic_name); } else { - g_ptr_array_add (array, current); + g_ptr_array_add (array, g_steal_pointer (¤t)); } } }