iio: Fix filename variable leak in build_channel_array()

From Coverity:
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:289: leaked_storage: Variable "filename" going out of scope leaks the storage it points to.
This commit is contained in:
Bastien Nocera
2022-03-25 12:36:32 +01:00
parent d87732b64c
commit 62bb35960e

View File

@@ -244,22 +244,20 @@ build_channel_array (const char *device_dir,
while ((name = g_dir_read_name (dp)) != NULL) { while ((name = g_dir_read_name (dp)) != NULL) {
if (g_str_has_suffix (name, "_en")) { if (g_str_has_suffix (name, "_en")) {
g_autoptr(FILE) sysfsfp = NULL; g_autoptr(FILE) sysfsfp = NULL;
char *filename, *index_name; g_autofree char *filename = NULL;
g_autofree char *index_name = NULL;
g_autoptr(iio_channel_info) current = NULL; g_autoptr(iio_channel_info) current = NULL;
filename = g_build_filename (scan_el_dir, name, NULL); filename = g_build_filename (scan_el_dir, name, NULL);
sysfsfp = fopen (filename, "r"); sysfsfp = fopen (filename, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
g_debug ("Could not open scan_elements file '%s'", filename); g_debug ("Could not open scan_elements file '%s'", filename);
g_free (filename);
continue; continue;
} }
if (fscanf (sysfsfp, "%d", &ret) != 1) { if (fscanf (sysfsfp, "%d", &ret) != 1) {
g_debug ("Could not read from scan_elements file '%s'", filename); g_debug ("Could not read from scan_elements file '%s'", filename);
g_free (filename);
continue; continue;
} }
g_free (filename);
g_clear_pointer (&sysfsfp, fclose); g_clear_pointer (&sysfsfp, fclose);
current = g_new0 (iio_channel_info, 1); current = g_new0 (iio_channel_info, 1);
@@ -273,14 +271,13 @@ build_channel_array (const char *device_dir,
} }
index_name = g_strdup_printf ("%s_index", current->name); index_name = g_strdup_printf ("%s_index", current->name);
g_clear_pointer (&filename, g_free);
filename = g_build_filename (scan_el_dir, index_name, NULL); filename = g_build_filename (scan_el_dir, index_name, NULL);
g_free (index_name);
sysfsfp = fopen (filename, "r"); sysfsfp = fopen (filename, "r");
if (sysfsfp == NULL) if (sysfsfp == NULL)
goto error; goto error;
ret = fscanf (sysfsfp, "%u", &current->index); ret = fscanf (sysfsfp, "%u", &current->index);
g_free (filename);
if (ret != 1) if (ret != 1)
goto error; goto error;