Skip to content

Commit

Permalink
fixed enable/disable logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bef committed Aug 8, 2021
1 parent 5e99a90 commit dce966f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 36 deletions.
49 changes: 14 additions & 35 deletions src/sp_config_keywords.c
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/upload_validation/upload_validation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dce966f

Please sign in to comment.