Skip to content

Commit 9e58ebb

Browse files
Yulong Zhanggregkh
authored andcommitted
tools/iio/iio_utils:fix memory leak
[ Upstream commit f2edf0c ] 1. fopen sysfs without fclose. 2. asprintf filename without free. 3. if asprintf return error,do not need to free the buffer. Signed-off-by: Yulong Zhang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 5417927 commit 9e58ebb

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

tools/iio/iio_utils.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ int iioutils_get_param_float(float *output, const char *param_name,
264264
if (fscanf(sysfsfp, "%f", output) != 1)
265265
ret = errno ? -errno : -ENODATA;
266266

267+
fclose(sysfsfp);
267268
break;
268269
}
269270
error_free_filename:
@@ -345,9 +346,9 @@ int build_channel_array(const char *device_dir, int buffer_idx,
345346
}
346347

347348
sysfsfp = fopen(filename, "r");
349+
free(filename);
348350
if (!sysfsfp) {
349351
ret = -errno;
350-
free(filename);
351352
goto error_close_dir;
352353
}
353354

@@ -357,19 +358,16 @@ int build_channel_array(const char *device_dir, int buffer_idx,
357358
if (fclose(sysfsfp))
358359
perror("build_channel_array(): Failed to close file");
359360

360-
free(filename);
361361
goto error_close_dir;
362362
}
363363
if (ret == 1)
364364
(*counter)++;
365365

366366
if (fclose(sysfsfp)) {
367367
ret = -errno;
368-
free(filename);
369368
goto error_close_dir;
370369
}
371370

372-
free(filename);
373371
}
374372

375373
*ci_array = malloc(sizeof(**ci_array) * (*counter));
@@ -395,30 +393,27 @@ int build_channel_array(const char *device_dir, int buffer_idx,
395393
}
396394

397395
sysfsfp = fopen(filename, "r");
396+
free(filename);
398397
if (!sysfsfp) {
399398
ret = -errno;
400-
free(filename);
401399
count--;
402400
goto error_cleanup_array;
403401
}
404402

405403
errno = 0;
406404
if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
407405
ret = errno ? -errno : -ENODATA;
408-
free(filename);
409406
count--;
410407
goto error_cleanup_array;
411408
}
412409

413410
if (fclose(sysfsfp)) {
414411
ret = -errno;
415-
free(filename);
416412
count--;
417413
goto error_cleanup_array;
418414
}
419415

420416
if (!current_enabled) {
421-
free(filename);
422417
count--;
423418
continue;
424419
}
@@ -429,7 +424,6 @@ int build_channel_array(const char *device_dir, int buffer_idx,
429424
strlen(ent->d_name) -
430425
strlen("_en"));
431426
if (!current->name) {
432-
free(filename);
433427
ret = -ENOMEM;
434428
count--;
435429
goto error_cleanup_array;
@@ -439,7 +433,6 @@ int build_channel_array(const char *device_dir, int buffer_idx,
439433
ret = iioutils_break_up_name(current->name,
440434
&current->generic_name);
441435
if (ret) {
442-
free(filename);
443436
free(current->name);
444437
count--;
445438
goto error_cleanup_array;
@@ -450,17 +443,16 @@ int build_channel_array(const char *device_dir, int buffer_idx,
450443
scan_el_dir,
451444
current->name);
452445
if (ret < 0) {
453-
free(filename);
454446
ret = -ENOMEM;
455447
goto error_cleanup_array;
456448
}
457449

458450
sysfsfp = fopen(filename, "r");
451+
free(filename);
459452
if (!sysfsfp) {
460453
ret = -errno;
461-
fprintf(stderr, "failed to open %s\n",
462-
filename);
463-
free(filename);
454+
fprintf(stderr, "failed to open %s/%s_index\n",
455+
scan_el_dir, current->name);
464456
goto error_cleanup_array;
465457
}
466458

@@ -470,17 +462,14 @@ int build_channel_array(const char *device_dir, int buffer_idx,
470462
if (fclose(sysfsfp))
471463
perror("build_channel_array(): Failed to close file");
472464

473-
free(filename);
474465
goto error_cleanup_array;
475466
}
476467

477468
if (fclose(sysfsfp)) {
478469
ret = -errno;
479-
free(filename);
480470
goto error_cleanup_array;
481471
}
482472

483-
free(filename);
484473
/* Find the scale */
485474
ret = iioutils_get_param_float(&current->scale,
486475
"scale",

0 commit comments

Comments
 (0)