diff --git a/hts_expr.c b/hts_expr.c index bbd369ed8..cee0c7f19 100644 --- a/hts_expr.c +++ b/hts_expr.c @@ -42,6 +42,9 @@ DEALINGS IN THE SOFTWARE. */ #include "htslib/hts_log.h" #include "textutils_internal.h" +// Maximum recursion depth +#define MAX_EXPR_DEPTH 100 + // Could also cache hts_expr_val_t stack here for kstring reuse? #define MAX_REGEX 10 struct hts_filter_t { @@ -53,6 +56,19 @@ struct hts_filter_t { int depth; }; +/* Utility macro for checking max depth */ +#define INCR_MAX_DEPTH \ +do { \ + if (++filt->depth > MAX_EXPR_DEPTH) { \ + hts_log_error("Expression depth too high"); \ + hts_expr_val_undef(res); \ + filt->depth--; \ + return -1; \ + } \ +} while (0) + +#define DECR_MAX_DEPTH filt->depth-- + /* * Valid ranges for int64 arithmetic when operating on doubles. * The mantissa is 53 bits, and we only accept 'normal' floating point too @@ -395,7 +411,9 @@ static int unary_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, res->is_true = res->d != 0; } } else if (*str == '!') { + INCR_MAX_DEPTH; err = unary_expr(filt, data, fn, str+1, end, res); + DECR_MAX_DEPTH; if (res->is_true) { // Any explicitly true value becomes false res->d = res->is_true = 0; @@ -415,7 +433,9 @@ static int unary_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } res->is_str = 0; } else if (*str == '~') { + INCR_MAX_DEPTH; err = unary_expr(filt, data, fn, str+1, end, res); + DECR_MAX_DEPTH; if (!hts_expr_val_exists(res)) { hts_expr_val_undef(res); } else { @@ -675,65 +695,53 @@ static int cmp_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, char *str, char **end, hts_expr_val_t *res) { if (bitor_expr(filt, data, fn, str, end, res)) return -1; - str = ws(*end); - hts_expr_val_t val = HTS_EXPR_VAL_INIT; - int err = 0, cmp_done = 0; + for (;;) { + str = ws(*end); + int op, len; + // Half-open syntax: [a,b) is >= a and < b. + if (str[0] == '>' && str[1] == '=') op = '[', len = 2; + else if (str[0] == '>') op = '(', len = 1; + else if (str[0] == '<' && str[1] == '=') op = ']', len = 2; + else if (str[0] == '<') op = ')', len = 1; + else break; - if (*str == '>' && str[1] == '=') { - cmp_done = 1; - err = cmp_expr(filt, data, fn, str+2, end, &val); - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); - } else { - res->is_true=res->d - = res->is_str && res->s.s && val.is_str && val.s.s - ? strcmp(res->s.s, val.s.s) >= 0 - : !res->is_str && !val.is_str && res->d >= val.d; - res->is_str = 0; - } - } else if (*str == '>') { - cmp_done = 1; - err = cmp_expr(filt, data, fn, str+1, end, &val); - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); - } else { - res->is_true=res->d - = res->is_str && res->s.s && val.is_str && val.s.s - ? strcmp(res->s.s, val.s.s) > 0 - : !res->is_str && !val.is_str && res->d > val.d; - res->is_str = 0; - } - } else if (*str == '<' && str[1] == '=') { - cmp_done = 1; - err = cmp_expr(filt, data, fn, str+2, end, &val); - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); - } else { - res->is_true=res->d - = res->is_str && res->s.s && val.is_str && val.s.s - ? strcmp(res->s.s, val.s.s) <= 0 - : !res->is_str && !val.is_str && res->d <= val.d; - res->is_str = 0; + hts_expr_val_t val = HTS_EXPR_VAL_INIT; + if (bitor_expr(filt, data, fn, str+len, end, &val)) { + hts_expr_val_free(&val); + return -1; } - } else if (*str == '<') { - cmp_done = 1; - err = cmp_expr(filt, data, fn, str+1, end, &val); + if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { - res->is_true=res->d - = res->is_str && res->s.s && val.is_str && val.s.s - ? strcmp(res->s.s, val.s.s) < 0 - : !res->is_str && !val.is_str && res->d < val.d; + int r; + if (res->is_str && res->s.s && val.is_str && val.s.s) { + int c = strcmp(res->s.s, val.s.s); + switch (op) { + case '[': r = c >= 0; break; + case '(': r = c > 0; break; + case ']': r = c <= 0; break; + default: + case ')': r = c < 0; break; + } + } else if (!res->is_str && !val.is_str) { + switch (op) { + case '[': r = res->d >= val.d; break; + case '(': r = res->d > val.d; break; + case ']': r = res->d <= val.d; break; + default: + case ')': r = res->d < val.d; break; + } + } else { + r = 0; // string vs number + } + res->is_true = res->d = r; res->is_str = 0; } + hts_expr_val_free(&val); } - if (cmp_done && (!hts_expr_val_exists(&val) || !hts_expr_val_exists(res))) - hts_expr_val_undef(res); - hts_expr_val_free(&val); - - return err ? -1 : 0; + return 0; } /* @@ -748,123 +756,114 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, char *str, char **end, hts_expr_val_t *res) { if (cmp_expr(filt, data, fn, str, end, res)) return -1; - str = ws(*end); + for (;;) { + str = ws(*end); + if (!((str[0] == '=' || str[0] == '!') && + (str[1] == '=' || str[1] == '~'))) + break; - int err = 0, eq_done = 0; - hts_expr_val_t val = HTS_EXPR_VAL_INIT; + hts_expr_val_t val = HTS_EXPR_VAL_INIT; + if (cmp_expr(filt, data, fn, str+2, end, &val)) { + hts_expr_val_free(&val); + return -1; + } - // numeric vs numeric comparison is as expected - // string vs string comparison is as expected - // numeric vs string is false - if (str[0] == '=' && str[1] == '=') { - eq_done = 1; - if ((err = eq_expr(filt, data, fn, str+2, end, &val))) { - res->is_true = res->d = 0; - } else { + if (str[1] == '=') { + // ==, != + // numeric vs numeric comparison is as expected + // string vs string comparison is as expected + // numeric vs string is false (==) / true (!=) if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { - res->is_true = res->d = res->is_str - ? (res->s.s && val.s.s ?strcmp(res->s.s, val.s.s)==0 :0) - : !res->is_str && !val.is_str && res->d == val.d; + int eq; + if (res->is_str && val.is_str) + eq = res->s.s && val.s.s && strcmp(res->s.s, val.s.s) == 0; + else if (!res->is_str && !val.is_str) + eq = res->d == val.d; + else + eq = 0; // string vs number + res->is_true = res->d = (str[0] == '=') ? eq : !eq; } - } - res->is_str = 0; - - } else if (str[0] == '!' && str[1] == '=') { - eq_done = 1; - if ((err = eq_expr(filt, data, fn, str+2, end, &val))) { - res->is_true = res->d = 0; + res->is_str = 0; } else { - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); - } else { - res->is_true = res->d = res->is_str - ? (res->s.s && val.s.s ?strcmp(res->s.s, val.s.s) != 0 :1) - : res->is_str != val.is_str || res->d != val.d; + // =~, !~ + if (!val.is_str || !res->is_str) { + hts_expr_val_free(&val); + return -1; } - } - res->is_str = 0; - } else if ((str[0] == '=' && str[1] == '~') || - (str[0] == '!' && str[1] == '~')) { - eq_done = 1; - err = eq_expr(filt, data, fn, str+2, end, &val); - if (!val.is_str || !res->is_str) { - hts_expr_val_free(&val); - return -1; - } + // One slot in the cache per match in the expression, even if this + // match is skipped below, so it doesn't depend on the data. + int idx = filt->curr_regex++; - // One slot in the cache per match in the expression, even if this - // match is skipped below, so it doesn't depend on the data. - int idx = filt->curr_regex++; + if (val.s.s && res->s.s && val.is_true >= 0 && res->is_true >= 0) { + regex_t preg_, *preg; + char **preg_str = idx < MAX_REGEX ? &filt->preg_str[idx] : NULL; - if (val.s.s && res->s.s && val.is_true >= 0 && res->is_true >= 0) { - regex_t preg_, *preg; - char **preg_str = idx < MAX_REGEX ? &filt->preg_str[idx] : NULL; + preg = preg_str ? &filt->preg[idx] : &preg_; - preg = preg_str ? &filt->preg[idx] : &preg_; - - // Compile regex if not seen before. The pattern may come from - // the data too, so it could also differ from the cached one. - if (!preg_str || !*preg_str || strcmp(*preg_str, val.s.s) != 0) { - if (preg_str && *preg_str) { - regfree(preg); - free(*preg_str); - *preg_str = NULL; - } + // Compile regex if not seen before. The pattern may come from + // the data too, so it could also differ from the cached one. + if (!preg_str || !*preg_str || strcmp(*preg_str, val.s.s) != 0) { + if (preg_str && *preg_str) { + regfree(preg); + free(*preg_str); + *preg_str = NULL; + } - // Sanity check for known cases that can cause memory - // exhaustion. - const char *cp = val.s.s, *cp_end = val.s.s + val.s.l; + // Sanity check for known cases that can cause memory + // exhaustion. + const char *cp = val.s.s, *cp_end = val.s.s + val.s.l; #define MAX_NQUANT 5 - int nquant = 0; - while (cp < cp_end) { - if (*cp == '+' || *cp == '*' || *cp == '?') { - if (++nquant >= MAX_NQUANT) - break; - } else if (*cp != ')') { - nquant = 0; + int nquant = 0; + while (cp < cp_end) { + if (*cp == '+' || *cp == '*' || *cp == '?') { + if (++nquant >= MAX_NQUANT) + break; + } else if (*cp != ')') { + nquant = 0; + } + cp++; } - cp++; - } - int ec = REG_BADRPT; - if (nquant < MAX_NQUANT) - ec = regcomp(preg, val.s.s, REG_EXTENDED | REG_NOSUB); - if (ec != 0) { - char errbuf[1024]; - regerror(ec, preg, errbuf, 1024); - fprintf(stderr, "Failed regex: %.1024s\n", errbuf); - hts_expr_val_undef(res); - hts_expr_val_free(&val); - return -1; - } + int ec = REG_BADRPT; + if (nquant < MAX_NQUANT) + ec = regcomp(preg, val.s.s, REG_EXTENDED | REG_NOSUB); + if (ec != 0) { + char errbuf[1024]; + regerror(ec, preg, errbuf, 1024); + fprintf(stderr, "Failed regex: %.1024s\n", errbuf); + hts_expr_val_undef(res); + hts_expr_val_free(&val); + return -1; + } - if (preg_str && !(*preg_str = strdup(val.s.s))) { - regfree(preg); - hts_expr_val_undef(res); - hts_expr_val_free(&val); - return -1; + if (preg_str && !(*preg_str = strdup(val.s.s))) { + regfree(preg); + hts_expr_val_undef(res); + hts_expr_val_free(&val); + return -1; + } } + res->is_true = res->d = regexec(preg, res->s.s, 0, NULL, 0) == 0 + ? *str == '=' // match + : *str == '!'; // no-match + if (preg == &preg_) + regfree(preg); + } else { + // nul regexp or input is considered false + res->is_true = 0; } - res->is_true = res->d = regexec(preg, res->s.s, 0, NULL, 0) == 0 - ? *str == '=' // matcn - : *str == '!'; // no-match - if (preg == &preg_) - regfree(preg); - } else { - // nul regexp or input is considered false - res->is_true = 0; + res->is_str = 0; } - res->is_str = 0; - } - if (eq_done && ((!hts_expr_val_exists(&val)) || !hts_expr_val_exists(res))) - hts_expr_val_undef(res); - hts_expr_val_free(&val); + if (!hts_expr_val_exists(&val) || !hts_expr_val_exists(res)) + hts_expr_val_undef(res); + hts_expr_val_free(&val); + } - return err ? -1 : 0; + return 0; } /* @@ -930,13 +929,9 @@ static int and_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, static int expression(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, char *str, char **end, hts_expr_val_t *res) { - if (++filt->depth > 100) { - hts_log_error("Expression depth too high"); - hts_expr_val_undef(res); - return -1; - } + INCR_MAX_DEPTH; int ret = and_expr(filt, data, fn, str, end, res); - filt->depth--; + DECR_MAX_DEPTH; return ret; } diff --git a/test/test_expr.c b/test/test_expr.c index 693a87c53..26de5cbac 100644 --- a/test/test_expr.c +++ b/test/test_expr.c @@ -298,6 +298,14 @@ int test(void) { { 1, 1, NULL, "exists(0)"}, { 0, 0, NULL, "exists(null)"}, { 1, 1, NULL, "exists(null-but-true)"}, + + // left-to-right processing check + { 0, 0, NULL, "1<=2>1"}, + { 0, 0, NULL, "3>2>1"}, + { 1, 1, NULL, "2==2==1"}, + + // string vs number comparisons + { 0, 0, NULL, "\"abc\" == (\"abc\" < \"b\")"}, }; int i, res = 0;