iio: Remove fscanf usage in _write_sysfs_string()
From Coverity: iio-sensor-proxy-3.3/src/iio-buffer-utils.c:435: dont_call: "fscanf" assumes an arbitrarily long string, so callers must use correct precision specifiers or never use "fscanf". This also fixes the temp variable being reused to parse the contents of the sysfs file before being freed. How this behaves depends on the length of the sysfs file path and the length of the string inside the file.
This commit is contained in:
@@ -402,6 +402,7 @@ _write_sysfs_string (const char *filename,
|
|||||||
{
|
{
|
||||||
g_autoptr(FILE) sysfsfp = NULL;
|
g_autoptr(FILE) sysfsfp = NULL;
|
||||||
g_autofree char *temp = NULL;
|
g_autofree char *temp = NULL;
|
||||||
|
g_autofree char *contents = NULL;
|
||||||
|
|
||||||
temp = g_build_filename (basedir, filename, NULL);
|
temp = g_build_filename (basedir, filename, NULL);
|
||||||
sysfsfp = fopen (temp, "w");
|
sysfsfp = fopen (temp, "w");
|
||||||
@@ -409,19 +410,16 @@ _write_sysfs_string (const char *filename,
|
|||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
fprintf(sysfsfp, "%s", val);
|
fprintf(sysfsfp, "%s", val);
|
||||||
g_clear_pointer (&sysfsfp, fclose);
|
|
||||||
|
|
||||||
/* Verify? */
|
/* Verify? */
|
||||||
if (!verify)
|
if (!verify)
|
||||||
return 0;
|
return 0;
|
||||||
sysfsfp = fopen(temp, "r");
|
g_clear_pointer (&sysfsfp, fclose);
|
||||||
if (sysfsfp == NULL) {
|
if (!g_file_get_contents (temp, &contents, NULL, NULL) ||
|
||||||
return -errno;
|
g_strcmp0 (contents, val) != 0) {
|
||||||
}
|
g_message ("WTA");
|
||||||
if (fscanf(sysfsfp, "%s", temp) != 1 ||
|
g_warning ("Possible failure in string write of '%s' Should be '%s' written to %s\n",
|
||||||
strcmp(temp, val) != 0) {
|
contents ?: "(null)", val, temp);
|
||||||
g_warning ("Possible failure in string write of %s Should be %s written to %s\\%s\n",
|
|
||||||
temp, val, basedir, filename);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user