Close files and initialize variable as per cppcheck errors (#28)

This commit is contained in:
Yu-Jie Lin 2017-01-08 10:54:10 +08:00 committed by Thomas Jensen
parent 4576f966af
commit d5795402d5

View File

@ -231,16 +231,20 @@ static int get_config_file()
return 1; return 1;
} }
rc = is_dir (s); rc = is_dir (s);
if (rc == -1) if (rc == -1) {
fclose (new_yyin);
return 1; return 1;
}
else if (rc) { else if (rc) {
fprintf (stderr, "%s: Alleged config file '%s' is a directory " fprintf (stderr, "%s: Alleged config file '%s' is a directory "
"(taken from $BOXES)\n", PROJECT, s); "(taken from $BOXES)\n", PROJECT, s);
fclose (new_yyin);
return 1; return 1;
} }
yyfilename = (char *) strdup (s); yyfilename = (char *) strdup (s);
if (yyfilename == NULL) { if (yyfilename == NULL) {
perror (PROJECT); perror (PROJECT);
fclose (new_yyin);
return 1; return 1;
} }
yyin = new_yyin; yyin = new_yyin;
@ -260,13 +264,16 @@ static int get_config_file()
new_yyin = fopen (BOXES_CONFIG, "r"); new_yyin = fopen (BOXES_CONFIG, "r");
if (new_yyin) { if (new_yyin) {
rc = is_dir (BOXES_CONFIG); rc = is_dir (BOXES_CONFIG);
if (rc == -1) if (rc == -1) {
fclose (new_yyin);
return 1; return 1;
}
else { else {
if (rc == 0) { if (rc == 0) {
yyfilename = (char *) strdup (BOXES_CONFIG); yyfilename = (char *) strdup (BOXES_CONFIG);
if (yyfilename == NULL) { if (yyfilename == NULL) {
perror (PROJECT); perror (PROJECT);
fclose (new_yyin);
return 1; return 1;
} }
yyin = new_yyin; yyin = new_yyin;
@ -320,17 +327,21 @@ static int get_config_file()
yyfilename = (char *) strdup (exepath); yyfilename = (char *) strdup (exepath);
#else #else
rc = is_dir (GLOBALCONF); rc = is_dir (GLOBALCONF);
if (rc == -1) if (rc == -1) {
fclose (new_yyin);
return 1; return 1;
}
else if (rc) { else if (rc) {
fprintf (stderr, "%s: Alleged system-wide config file '%s' " fprintf (stderr, "%s: Alleged system-wide config file '%s' "
"is a directory\n", PROJECT, GLOBALCONF); "is a directory\n", PROJECT, GLOBALCONF);
fclose (new_yyin);
return 1; return 1;
} }
yyfilename = (char *) strdup (GLOBALCONF); yyfilename = (char *) strdup (GLOBALCONF);
#endif #endif
if (yyfilename == NULL) { if (yyfilename == NULL) {
perror (PROJECT); perror (PROJECT);
fclose (new_yyin);
return 1; return 1;
} }
yyin = new_yyin; yyin = new_yyin;
@ -477,7 +488,7 @@ static int process_commandline (int argc, char *argv[])
return 1; return 1;
} }
else { else {
line_t templine; line_t templine = {0};
templine.len = strlen (opt.cld); templine.len = strlen (opt.cld);
templine.text = opt.cld; templine.text = opt.cld;
if (empty_line(&templine)) { if (empty_line(&templine)) {
@ -513,16 +524,20 @@ static int process_commandline (int argc, char *argv[])
return 1; return 1;
} }
rc = is_dir (optarg); rc = is_dir (optarg);
if (rc == -1) if (rc == -1) {
fclose (f);
return 1; return 1;
}
else if (rc) { else if (rc) {
fprintf (stderr, "%s: Alleged config file '%s' is a " fprintf (stderr, "%s: Alleged config file '%s' is a "
"directory\n", PROJECT, optarg); "directory\n", PROJECT, optarg);
fclose (f);
return 1; return 1;
} }
yyfilename = (char *) strdup (optarg); yyfilename = (char *) strdup (optarg);
if (yyfilename == NULL) { if (yyfilename == NULL) {
perror (PROJECT); perror (PROJECT);
fclose (f);
return 1; return 1;
} }
yyin = f; yyin = f;