From 784166ad0bcf49bc03fca52127d63ad319802ef3 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Thu, 25 Feb 2016 20:10:37 +0200 Subject: [PATCH] Added missing check for `fopen` return value This would have led to a crash when a NULL pointer would have been used as a valid FILE handle. --- server/modules/filter/dbfwfilter/dbfwfilter.c | 68 +++++++++++-------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/server/modules/filter/dbfwfilter/dbfwfilter.c b/server/modules/filter/dbfwfilter/dbfwfilter.c index f0c023dfe..6016de4d3 100644 --- a/server/modules/filter/dbfwfilter/dbfwfilter.c +++ b/server/modules/filter/dbfwfilter/dbfwfilter.c @@ -1286,41 +1286,53 @@ static bool process_user_templates(FW_INSTANCE *instance, user_template_t *templ */ static bool process_rule_file(const char* filename, FW_INSTANCE* instance) { + int rc = 1; FILE *file = fopen(filename, "r"); - yyscan_t scanner; - struct parser_stack pstack; - pstack.rule = NULL; - pstack.user = NULL; - pstack.active_rules = NULL; - pstack.templates = NULL; - - dbfw_yylex_init(&scanner); - YY_BUFFER_STATE buf = dbfw_yy_create_buffer(file, YY_BUF_SIZE, scanner); - dbfw_yyset_extra(&pstack, scanner); - dbfw_yy_switch_to_buffer(buf, scanner); - - /** Parse the rule file */ - int rc = dbfw_yyparse(scanner); - - dbfw_yy_delete_buffer(buf, scanner); - dbfw_yylex_destroy(scanner); - fclose(file); - - if (rc == 0 && process_user_templates(instance, pstack.templates, pstack.rule)) + if (file) { - instance->rules = pstack.rule; + yyscan_t scanner; + struct parser_stack pstack; + + pstack.rule = NULL; + pstack.user = NULL; + pstack.active_rules = NULL; + pstack.templates = NULL; + + dbfw_yylex_init(&scanner); + YY_BUFFER_STATE buf = dbfw_yy_create_buffer(file, YY_BUF_SIZE, scanner); + dbfw_yyset_extra(&pstack, scanner); + dbfw_yy_switch_to_buffer(buf, scanner); + + /** Parse the rule file */ + rc = dbfw_yyparse(scanner); + + dbfw_yy_delete_buffer(buf, scanner); + dbfw_yylex_destroy(scanner); + fclose(file); + + if (rc == 0 && process_user_templates(instance, pstack.templates, pstack.rule)) + { + instance->rules = pstack.rule; + } + else + { + rc = 1; + free_rules(pstack.rule); + MXS_ERROR("Failed to process rule file '%s'.", filename); + } + + free_user_templates(pstack.templates); + strlink_free(pstack.active_rules); + strlink_free(pstack.user); } else { - rc = 1; - free_rules(pstack.rule); - MXS_ERROR("Failed to process rule file '%s'.", filename); - } + char errbuf[STRERROR_BUFLEN]; + MXS_ERROR("Failed to open rule file '%s': %d, %s", filename, errno, + strerror_r(errno, errbuf, sizeof(errbuf))); - free_user_templates(pstack.templates); - strlink_free(pstack.active_rules); - strlink_free(pstack.user); + } return rc == 0; }