From c0d14f854ffedd29cc6d53c8033c72573a63273e Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 9 Feb 2017 18:33:38 -0500 Subject: parse: Factor out all "looks like icmp" checks --- parse.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/parse.c b/parse.c index 2d24dda..22fa57b 100644 --- a/parse.c +++ b/parse.c @@ -475,6 +475,16 @@ static const char *parse_icmp(const struct parser_state *state, const char *str, return parse_int(state, str, &expr->idata, flags | IF_LONG_LONG | IF_UNSIGNED); } +/** + * Check if a string could be an integer comparison. + */ +static bool looks_like_icmp(const char *str) { + if (str[0] == '-' || str[0] == '+') { + ++str; + } + return str[0] >= '0' && str[0] <= '9'; +} + /** * Parse a single flag. */ @@ -843,16 +853,11 @@ static struct expr *parse_depth(struct parser_state *state, int arg1, int arg2) */ static struct expr *parse_depth_n(struct parser_state *state, int arg1, int arg2) { const char *arg = state->argv[1]; - if (arg) { - while (*arg == '-' || *arg == '+') { - ++arg; - } - if (*arg >= '0' && *arg <= '9') { - return parse_test_icmp(state, eval_depth); - } + if (arg && looks_like_icmp(arg)) { + return parse_test_icmp(state, eval_depth); + } else { + return parse_depth(state, arg1, arg2); } - - return parse_depth(state, arg1, arg2); } /** @@ -1033,7 +1038,7 @@ static struct expr *parse_group(struct parser_state *state, int arg1, int arg2) if (grp) { expr->idata = grp->gr_gid; expr->cmp_flag = CMP_EXACT; - } else if (isdigit(expr->sdata[0]) || expr->sdata[0] == '+' || expr->sdata[0] == '-') { + } else if (looks_like_icmp(expr->sdata)) { if (!parse_icmp(state, expr->sdata, expr, 0)) { goto fail; } @@ -1072,7 +1077,7 @@ static struct expr *parse_user(struct parser_state *state, int arg1, int arg2) { if (pwd) { expr->idata = pwd->pw_uid; expr->cmp_flag = CMP_EXACT; - } else if (isdigit(expr->sdata[0]) || expr->sdata[0] == '+' || expr->sdata[0] == '-') { + } else if (looks_like_icmp(expr->sdata)) { if (!parse_icmp(state, expr->sdata, expr, 0)) { goto fail; } -- cgit v1.2.3