iio: Fix filename leak in iioutils_get_param_float()
From Coverity: iio-sensor-proxy-3.3/src/iio-buffer-utils.c:173: leaked_storage: Variable "filename" going out of scope leaks the storage it points to.
This commit is contained in:
@@ -147,16 +147,15 @@ iioutils_get_param_float (float *output,
|
|||||||
const char *name,
|
const char *name,
|
||||||
const char *generic_name)
|
const char *generic_name)
|
||||||
{
|
{
|
||||||
char *builtname, *filename;
|
g_autofree char *builtname = NULL;
|
||||||
|
g_autofree char *filename = NULL;
|
||||||
g_autofree char *contents = NULL;
|
g_autofree char *contents = NULL;
|
||||||
g_autoptr(GError) error = NULL;
|
g_autoptr(GError) error = NULL;
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
g_debug ("Trying to read '%s_%s' (name) from dir '%s'", name, param_name, device_dir);
|
g_debug ("Trying to read '%s_%s' (name) from dir '%s'", name, param_name, device_dir);
|
||||||
|
|
||||||
builtname = g_strdup_printf ("%s_%s", name, param_name);
|
builtname = g_strdup_printf ("%s_%s", name, param_name);
|
||||||
filename = g_build_filename (device_dir, builtname, NULL);
|
filename = g_build_filename (device_dir, builtname, NULL);
|
||||||
g_free (builtname);
|
|
||||||
|
|
||||||
if (g_file_get_contents (filename, &contents, NULL, &error)) {
|
if (g_file_get_contents (filename, &contents, NULL, &error)) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
@@ -164,38 +163,37 @@ iioutils_get_param_float (float *output,
|
|||||||
if (*output != 0.0 || endptr != contents)
|
if (*output != 0.0 || endptr != contents)
|
||||||
return 0;
|
return 0;
|
||||||
g_warning ("Couldn't convert '%s' from %s to float", g_strchomp (contents), filename);
|
g_warning ("Couldn't convert '%s' from %s to float", g_strchomp (contents), filename);
|
||||||
g_clear_pointer (&contents, g_free);
|
|
||||||
} else {
|
} else {
|
||||||
g_debug ("Failed to read float from %s: %s", filename, error->message);
|
g_debug ("Failed to read float from %s: %s", filename, error->message);
|
||||||
g_clear_error (&error);
|
g_clear_error (&error);
|
||||||
}
|
}
|
||||||
g_free (filename);
|
|
||||||
|
|
||||||
g_debug ("Trying to read '%s_%s' (generic name) from dir '%s'", generic_name, param_name, device_dir);
|
g_debug ("Trying to read '%s_%s' (generic name) from dir '%s'", generic_name, param_name, device_dir);
|
||||||
|
|
||||||
|
g_clear_pointer (&builtname, g_free);
|
||||||
|
g_clear_pointer (&filename, g_free);
|
||||||
builtname = g_strdup_printf ("%s_%s", generic_name, param_name);
|
builtname = g_strdup_printf ("%s_%s", generic_name, param_name);
|
||||||
filename = g_build_filename (device_dir, builtname, NULL);
|
filename = g_build_filename (device_dir, builtname, NULL);
|
||||||
g_free (builtname);
|
|
||||||
|
|
||||||
|
g_clear_pointer (&contents, g_free);
|
||||||
if (g_file_get_contents (filename, &contents, NULL, &error)) {
|
if (g_file_get_contents (filename, &contents, NULL, &error)) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
*output = g_ascii_strtod (contents, &endptr);
|
*output = g_ascii_strtod (contents, &endptr);
|
||||||
if (*output == 0.0 && endptr == contents) {
|
if (*output == 0.0 && endptr == contents) {
|
||||||
g_warning ("Couldn't convert '%s' from %s to float", g_strchomp (contents), filename);
|
g_warning ("Couldn't convert '%s' from %s to float", g_strchomp (contents), filename);
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
|
if (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
|
||||||
ret = -ENOENT;
|
|
||||||
g_debug ("Failed to read float from non-existent %s", filename);
|
g_debug ("Failed to read float from non-existent %s", filename);
|
||||||
|
return -ENOENT;
|
||||||
} else {
|
} else {
|
||||||
ret = -EINVAL;
|
|
||||||
g_warning ("Failed to read float from %s: %s", filename, error->message);
|
g_warning ("Failed to read float from %s: %s", filename, error->message);
|
||||||
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g_free (filename);
|
|
||||||
|
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Reference in New Issue
Block a user