From dce966ffec7dfdae2b701d581d71df6a5a542db9 Mon Sep 17 00:00:00 2001 From: Ben Fuhrmannek Date: Sun, 8 Aug 2021 15:55:06 +0200 Subject: [PATCH] fixed enable/disable logic --- src/sp_config_keywords.c | 49 ++++++------------- .../upload_validation/upload_validation.phpt | 2 +- 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/sp_config_keywords.c b/src/sp_config_keywords.c index c547f107..b6271229 100644 --- a/src/sp_config_keywords.c +++ b/src/sp_config_keywords.c @@ -1,5 +1,14 @@ #include "php_snuffleupagus.h" +#define SP_SET_ENABLE_DISABLE(enable, disable, varname) \ + if (enable && disable) { \ + sp_log_err("config", "A rule can't be enabled and disabled on line %zu", sp_line_no); \ + return -1; \ + } \ + if (enable || disable) { \ + (varname) = (enable || !disable); \ + } + static int parse_enable(char *line, bool *restrict retval, bool *restrict simulation) { bool enable = false, disable = false; @@ -15,13 +24,7 @@ static int parse_enable(char *line, bool *restrict retval, return ret; } - if (!(enable ^ disable)) { - sp_log_err("config", "A rule can't be enabled and disabled on line %zu", - sp_line_no); - return -1; - } - - *retval = enable; + SP_SET_ENABLE_DISABLE(enable, disable, *retval); return ret; } @@ -141,13 +144,7 @@ int parse_unserialize(char *line) { return ret; } - if (!(enable ^ disable)) { - sp_log_err("config", "A rule can't be enabled and disabled on line %zu", - sp_line_no); - return -1; - } - - SNUFFLEUPAGUS_G(config).config_unserialize->enable = enable; + SP_SET_ENABLE_DISABLE(enable, disable, SNUFFLEUPAGUS_G(config).config_unserialize->enable); return ret; } @@ -172,13 +169,7 @@ int parse_readonly_exec(char *line) { return ret; } - if (!(enable ^ disable)) { - sp_log_err("config", "A rule can't be enabled and disabled on line %zu", - sp_line_no); - return -1; - } - - SNUFFLEUPAGUS_G(config).config_readonly_exec->enable = enable; + SP_SET_ENABLE_DISABLE(enable, disable, SNUFFLEUPAGUS_G(config).config_readonly_exec->enable); return ret; } @@ -535,12 +526,7 @@ int parse_upload_validation(char *line) { return ret; } - if (!(enable ^ disable)) { - sp_log_err("config", "A rule can't be enabled and disabled on line %zu", - sp_line_no); - return -1; - } - SNUFFLEUPAGUS_G(config).config_upload_validation->enable = enable; + SP_SET_ENABLE_DISABLE(enable, disable, SNUFFLEUPAGUS_G(config).config_upload_validation->enable); zend_string const *script = SNUFFLEUPAGUS_G(config).config_upload_validation->script; @@ -584,14 +570,7 @@ int parse_ini_protection(char *line) { int ret = parse_keywords(sp_config_ini_protection, line); if (ret) { return ret; } - if (enable && disable) { - sp_log_err("config", "A rule can't be enabled and disabled on line %zu", - sp_line_no); - return -1; - } - if (enable || disable) { - cfg->enable = (enable || !disable); - } + SP_SET_ENABLE_DISABLE(enable, disable, cfg->enable); if (ro && rw) { sp_log_err("config", "rule cannot be both read-write and read-only on line %zu", sp_line_no); diff --git a/src/tests/upload_validation/upload_validation.phpt b/src/tests/upload_validation/upload_validation.phpt index 965d3aab..810c23d2 100644 --- a/src/tests/upload_validation/upload_validation.phpt +++ b/src/tests/upload_validation/upload_validation.phpt @@ -15,4 +15,4 @@ echo 1; --EXPECTF-- Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0 -Fatal error: [snuffleupagus][0.0.0.0][config][log] A rule can't be enabled and disabled on line 1 in Unknown on line 0 +Fatal error: [snuffleupagus][0.0.0.0][config][log] The `script` (tests/upload_ko.sh) doesn't exist on line 1 in Unknown on line 0