Skip to content

Commit ec9ab85

Browse files
mikhailnovstevegrubb
authored andcommitted
Fix HANDLE_LEAK.EX in aulast.c
Close file handle 'f' before returning error in argument parsing to prevent resource leak when --extract option is followed by an invalid argument. Svace report: Handle 'f' is created at aulast.c:506 by calling function 'fopen' and lost at aulast.c:506. (CWE404, CWE775) This leaks happens in the end of main() and could be not fixed, but let's make static analyzers happy... Example scaniors of leaks: aulast --extract -f file -f file2 aulast -f file --extract --stdin aulast --extract --user u1 --user u2 aulast --extract --tty t1 --tty t2 aulast --extract --invalid Co-authored-by: Z.AI GLM-5
1 parent 828b5b2 commit ec9ab85

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

tools/aulast/aulast.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ int main(int argc, char *argv[])
497497
file = argv[i];
498498
} else {
499499
fprintf(stderr,"stdin already given\n");
500-
return 1;
500+
goto arg_error;
501501
}
502502
} else if (strcmp(argv[i], "--bad") == 0) {
503503
bad = 1;
@@ -510,29 +510,29 @@ int main(int argc, char *argv[])
510510
use_stdin = 1;
511511
else {
512512
fprintf(stderr, "file already given\n");
513-
return 1;
513+
goto arg_error;
514514
}
515515
} else if (strcmp(argv[i], "--user") == 0) {
516516
if (user == NULL) {
517517
i++;
518518
user = argv[i];
519519
} else {
520520
usage();
521-
return 1;
521+
goto arg_error;
522522
}
523523
} else if (strcmp(argv[i], "--tty") == 0) {
524524
if (cterm == NULL) {
525525
i++;
526526
cterm = argv[i];
527527
} else {
528528
usage();
529-
return 1;
529+
goto arg_error;
530530
}
531531
} else if (strcmp(argv[i], "--debug") == 0) {
532532
debug = 1;
533533
} else {
534534
usage();
535-
return 1;
535+
goto arg_error;
536536
}
537537
}
538538
list_create(&l);
@@ -612,6 +612,11 @@ int main(int argc, char *argv[])
612612
fclose(f);
613613
return 0;
614614

615+
arg_error:
616+
if (f)
617+
fclose(f);
618+
return 1;
619+
615620
error_exit_1:
616621
list_clear(&l);
617622
if (f)

0 commit comments

Comments
 (0)