Skip to content

Commit

Permalink
checkpolicy: cleanup resources on parse error
Browse files Browse the repository at this point in the history
Close the input file and free all memory by the queue and lexer on a
syntax or parse error.

Signed-off-by: Christian Göttsche <[email protected]>
Acked-by: James Carter <[email protected]>
  • Loading branch information
cgzones authored and jwcart2 committed Mar 4, 2024
1 parent 595c416 commit c2fc48b
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions checkpolicy/parse_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern FILE *yyin;
extern void init_parser(int);
extern int yyparse(void);
extern void yyrestart(FILE *);
extern int yylex_destroy(void);
extern queue_t id_queue;
extern unsigned int policydb_errors;
extern policydb_t *policydbp;
Expand All @@ -34,28 +35,35 @@ extern void set_source_file(const char *name);

int read_source_policy(policydb_t * p, const char *file, const char *progname)
{
int rc = -1;

yyin = fopen(file, "r");
if (!yyin) {
fprintf(stderr, "%s: unable to open %s: %s\n", progname, file, strerror(errno));
return -1;
}
set_source_file(file);

if ((id_queue = queue_create()) == NULL) {
id_queue = queue_create();
if (id_queue == NULL) {
fprintf(stderr, "%s: out of memory!\n", progname);
return -1;
goto cleanup;
}

mlspol = p->mls;
policydbp = p;
policydbp->name = strdup(file);
mlspol = p->mls;
if (!policydbp->name) {
fprintf(stderr, "%s: out of memory!\n", progname);
goto cleanup;
}

init_parser(1);
if (yyparse() || policydb_errors) {
fprintf(stderr,
"%s: error(s) encountered while parsing configuration\n",
progname);
return -1;
goto cleanup;
}
rewind(yyin);
init_parser(2);
Expand All @@ -65,11 +73,15 @@ int read_source_policy(policydb_t * p, const char *file, const char *progname)
fprintf(stderr,
"%s: error(s) encountered while parsing configuration\n",
progname);
return -1;
goto cleanup;
}
queue_destroy(id_queue);

rc = 0;

cleanup:
queue_destroy(id_queue);
fclose(yyin);
yylex_destroy();

return 0;
return rc;
}

0 comments on commit c2fc48b

Please sign in to comment.