Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/iio/iio_utils:fix memory leak

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 <yulong.zhang@metoak.net>
Link: https://lore.kernel.org/r/20230117025147.69890-1-yulong.zhang@metoak.net
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Yulong Zhang and committed by
Jonathan Cameron
f2edf0c8 05f59aba

+6 -17
+6 -17
tools/iio/iio_utils.c
··· 264 264 if (fscanf(sysfsfp, "%f", output) != 1) 265 265 ret = errno ? -errno : -ENODATA; 266 266 267 + fclose(sysfsfp); 267 268 break; 268 269 } 269 270 error_free_filename: ··· 346 345 } 347 346 348 347 sysfsfp = fopen(filename, "r"); 348 + free(filename); 349 349 if (!sysfsfp) { 350 350 ret = -errno; 351 - free(filename); 352 351 goto error_close_dir; 353 352 } 354 353 ··· 358 357 if (fclose(sysfsfp)) 359 358 perror("build_channel_array(): Failed to close file"); 360 359 361 - free(filename); 362 360 goto error_close_dir; 363 361 } 364 362 if (ret == 1) ··· 365 365 366 366 if (fclose(sysfsfp)) { 367 367 ret = -errno; 368 - free(filename); 369 368 goto error_close_dir; 370 369 } 371 370 372 - free(filename); 373 371 } 374 372 375 373 *ci_array = malloc(sizeof(**ci_array) * (*counter)); ··· 393 395 } 394 396 395 397 sysfsfp = fopen(filename, "r"); 398 + free(filename); 396 399 if (!sysfsfp) { 397 400 ret = -errno; 398 - free(filename); 399 401 count--; 400 402 goto error_cleanup_array; 401 403 } ··· 403 405 errno = 0; 404 406 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) { 405 407 ret = errno ? -errno : -ENODATA; 406 - free(filename); 407 408 count--; 408 409 goto error_cleanup_array; 409 410 } 410 411 411 412 if (fclose(sysfsfp)) { 412 413 ret = -errno; 413 - free(filename); 414 414 count--; 415 415 goto error_cleanup_array; 416 416 } 417 417 418 418 if (!current_enabled) { 419 - free(filename); 420 419 count--; 421 420 continue; 422 421 } ··· 424 429 strlen(ent->d_name) - 425 430 strlen("_en")); 426 431 if (!current->name) { 427 - free(filename); 428 432 ret = -ENOMEM; 429 433 count--; 430 434 goto error_cleanup_array; ··· 433 439 ret = iioutils_break_up_name(current->name, 434 440 &current->generic_name); 435 441 if (ret) { 436 - free(filename); 437 442 free(current->name); 438 443 count--; 439 444 goto error_cleanup_array; ··· 443 450 scan_el_dir, 444 451 current->name); 445 452 if (ret < 0) { 446 - free(filename); 447 453 ret = -ENOMEM; 448 454 goto error_cleanup_array; 449 455 } 450 456 451 457 sysfsfp = fopen(filename, "r"); 458 + free(filename); 452 459 if (!sysfsfp) { 453 460 ret = -errno; 454 - fprintf(stderr, "failed to open %s\n", 455 - filename); 456 - free(filename); 461 + fprintf(stderr, "failed to open %s/%s_index\n", 462 + scan_el_dir, current->name); 457 463 goto error_cleanup_array; 458 464 } 459 465 ··· 462 470 if (fclose(sysfsfp)) 463 471 perror("build_channel_array(): Failed to close file"); 464 472 465 - free(filename); 466 473 goto error_cleanup_array; 467 474 } 468 475 469 476 if (fclose(sysfsfp)) { 470 477 ret = -errno; 471 - free(filename); 472 478 goto error_cleanup_array; 473 479 } 474 480 475 - free(filename); 476 481 /* Find the scale */ 477 482 ret = iioutils_get_param_float(&current->scale, 478 483 "scale",