From 495d19952d896d3c49426ac438639255ae516eb3 Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Thu, 26 Nov 2026 16:53:42 +0000 Subject: [PATCH 1/5] Better enforce the expression filtering recursion depth limits Note however there are also some grammar issues when it comes to computation within a single level of precedence. Eg 1<=2>1 is computed as 1<=(2>1) which is at odds with bc -l which does (1<=2)>1. <= and > are the same precedence, so it should be strictly left to right. Signed-off-by: James Bonfield --- hts_expr.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/hts_expr.c b/hts_expr.c index bbd369ed8..5a647281f 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 { @@ -681,7 +701,9 @@ static int cmp_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, if (*str == '>' && str[1] == '=') { cmp_done = 1; + INCR_MAX_DEPTH; err = cmp_expr(filt, data, fn, str+2, end, &val); + DECR_MAX_DEPTH; if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { @@ -693,7 +715,9 @@ static int cmp_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } } else if (*str == '>') { cmp_done = 1; + INCR_MAX_DEPTH; err = cmp_expr(filt, data, fn, str+1, end, &val); + DECR_MAX_DEPTH; if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { @@ -705,7 +729,9 @@ static int cmp_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } } else if (*str == '<' && str[1] == '=') { cmp_done = 1; + INCR_MAX_DEPTH; err = cmp_expr(filt, data, fn, str+2, end, &val); + DECR_MAX_DEPTH; if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { @@ -717,7 +743,9 @@ static int cmp_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } } else if (*str == '<') { cmp_done = 1; + INCR_MAX_DEPTH; err = cmp_expr(filt, data, fn, str+1, end, &val); + DECR_MAX_DEPTH; if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { @@ -758,7 +786,10 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, // numeric vs string is false if (str[0] == '=' && str[1] == '=') { eq_done = 1; - if ((err = eq_expr(filt, data, fn, str+2, end, &val))) { + INCR_MAX_DEPTH; + err = eq_expr(filt, data, fn, str+2, end, &val); + DECR_MAX_DEPTH; + if (err) { res->is_true = res->d = 0; } else { if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { @@ -773,7 +804,10 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } else if (str[0] == '!' && str[1] == '=') { eq_done = 1; - if ((err = eq_expr(filt, data, fn, str+2, end, &val))) { + INCR_MAX_DEPTH; + err = eq_expr(filt, data, fn, str+2, end, &val); + DECR_MAX_DEPTH; + if (err) { res->is_true = res->d = 0; } else { if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { @@ -789,7 +823,9 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } else if ((str[0] == '=' && str[1] == '~') || (str[0] == '!' && str[1] == '~')) { eq_done = 1; + INCR_MAX_DEPTH; err = eq_expr(filt, data, fn, str+2, end, &val); + DECR_MAX_DEPTH; if (!val.is_str || !res->is_str) { hts_expr_val_free(&val); return -1; @@ -930,13 +966,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; } From a1244871cf1a1ed7191f2dd42cf4462c3de2e9d9 Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Thu, 26 Nov 2026 17:30:53 +0000 Subject: [PATCH 2/5] Add more testing of the expression filter grammar. This should fail until the next commit. Signed-off-by: James Bonfield --- test/test_expr.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_expr.c b/test/test_expr.c index 693a87c53..112f896d9 100644 --- a/test/test_expr.c +++ b/test/test_expr.c @@ -298,6 +298,11 @@ 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"}, }; int i, res = 0; From c2bcd9a9a98b81f682b6e861bbe2e1578d6e9ccb Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Thu, 26 Nov 2026 17:20:39 +0000 Subject: [PATCH 3/5] Fix expression parser left-to-right parsing. Usually with expression operators that are grouped into the same precedence level we process them with a loop (either "for (;;)" or "while (*str)", so we explicitly combine in left to right order. However cmp_expr and eq_expr didn't do this. Instead they implemented the next level down combined with the a self loop. Eg 3>2 in cmp_expr was bitor_expr (3) > cmp_expr(2). That however means 3>2>1 becomes 3>(2>1) which is right to left instead of left to right, yielding the wrong answer. Signed-off-by: James Bonfield --- hts_expr.c | 327 ++++++++++++++++++++++++++--------------------------- 1 file changed, 162 insertions(+), 165 deletions(-) diff --git a/hts_expr.c b/hts_expr.c index 5a647281f..d04d3182f 100644 --- a/hts_expr.c +++ b/hts_expr.c @@ -695,71 +695,69 @@ 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; + int err = 0; + for (;;) { + str = ws(*end); + hts_expr_val_t val = HTS_EXPR_VAL_INIT; + int cmp_done = 0; - if (*str == '>' && str[1] == '=') { - cmp_done = 1; - INCR_MAX_DEPTH; - err = cmp_expr(filt, data, fn, str+2, end, &val); - DECR_MAX_DEPTH; - 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; - INCR_MAX_DEPTH; - err = cmp_expr(filt, data, fn, str+1, end, &val); - DECR_MAX_DEPTH; - 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; - INCR_MAX_DEPTH; - err = cmp_expr(filt, data, fn, str+2, end, &val); - DECR_MAX_DEPTH; - 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; + if (*str == '>' && str[1] == '=') { + cmp_done = 1; + err = bitor_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 = bitor_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 = bitor_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 = bitor_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 == '<') { - cmp_done = 1; - INCR_MAX_DEPTH; - err = cmp_expr(filt, data, fn, str+1, end, &val); - DECR_MAX_DEPTH; - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { + + if (cmp_done && (!hts_expr_val_exists(&val) || !hts_expr_val_exists(res))) 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_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); + if (!cmp_done || err) + break; + } return err ? -1 : 0; } @@ -776,129 +774,128 @@ 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); - - int err = 0, eq_done = 0; - hts_expr_val_t val = HTS_EXPR_VAL_INIT; - - // 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; - INCR_MAX_DEPTH; - err = eq_expr(filt, data, fn, str+2, end, &val); - DECR_MAX_DEPTH; - if (err) { - res->is_true = res->d = 0; - } else { - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); + int err = 0; + for (;;) { + str = ws(*end); + hts_expr_val_t val = HTS_EXPR_VAL_INIT; + int eq_done = 0; + + // 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; + err = cmp_expr(filt, data, fn, str+2, end, &val); + if (err) { + res->is_true = res->d = 0; } 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; + 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; + } } - } - res->is_str = 0; + res->is_str = 0; - } else if (str[0] == '!' && str[1] == '=') { - eq_done = 1; - INCR_MAX_DEPTH; - err = eq_expr(filt, data, fn, str+2, end, &val); - DECR_MAX_DEPTH; - if (err) { - res->is_true = res->d = 0; - } else { - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); + } else if (str[0] == '!' && str[1] == '=') { + eq_done = 1; + err = cmp_expr(filt, data, fn, str+2, end, &val); + if (err) { + res->is_true = res->d = 0; } 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 (!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; + } } - } - res->is_str = 0; + res->is_str = 0; - } else if ((str[0] == '=' && str[1] == '~') || - (str[0] == '!' && str[1] == '~')) { - eq_done = 1; - INCR_MAX_DEPTH; - err = eq_expr(filt, data, fn, str+2, end, &val); - DECR_MAX_DEPTH; - if (!val.is_str || !res->is_str) { - hts_expr_val_free(&val); - return -1; - } + } else if ((str[0] == '=' && str[1] == '~') || + (str[0] == '!' && str[1] == '~')) { + eq_done = 1; + err = cmp_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 == '=' // matcn + : *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 (eq_done && ((!hts_expr_val_exists(&val)) || !hts_expr_val_exists(res))) + hts_expr_val_undef(res); + hts_expr_val_free(&val); + + if (!eq_done || err) + break; + } return err ? -1 : 0; } From 573aea96201d7db5ee1726dcea27beedc440bd9c Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Thu, 26 Nov 2026 17:35:14 +0000 Subject: [PATCH 4/5] Refactor of expression cmp_expr and eq_expr In testing, Claude suggested a far more concise way of coding this which is a lot less tedious to read through. This commit is a mixture of Claude's rewrite and my rephrasing of it. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: James Bonfield --- hts_expr.c | 160 ++++++++++++++++++++--------------------------------- 1 file changed, 61 insertions(+), 99 deletions(-) diff --git a/hts_expr.c b/hts_expr.c index d04d3182f..3ceeadf3d 100644 --- a/hts_expr.c +++ b/hts_expr.c @@ -695,71 +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; - int err = 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; + hts_expr_val_t val = HTS_EXPR_VAL_INIT; - int cmp_done = 0; + if (bitor_expr(filt, data, fn, str+len, end, &val)) { + hts_expr_val_free(&val); + return -1; + } - if (*str == '>' && str[1] == '=') { - cmp_done = 1; - err = bitor_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 = bitor_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 = bitor_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 = bitor_expr(filt, data, fn, str+1, end, &val); - if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { - hts_expr_val_undef(res); + if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { + hts_expr_val_undef(res); + } else { + 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 { - 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; + r = 0; // string vs number } + res->is_true = res->d = r; + res->is_str = 0; } - - if (cmp_done && (!hts_expr_val_exists(&val) || !hts_expr_val_exists(res))) - hts_expr_val_undef(res); hts_expr_val_free(&val); - - if (!cmp_done || err) - break; } - return err ? -1 : 0; + return 0; } /* @@ -774,51 +756,34 @@ 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; - int err = 0; for (;;) { str = ws(*end); + if (!((str[0] == '=' || str[0] == '!') && + (str[1] == '=' || str[1] == '~'))) + break; + hts_expr_val_t val = HTS_EXPR_VAL_INIT; - int eq_done = 0; - - // 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; - err = cmp_expr(filt, data, fn, str+2, end, &val); - if (err) { - res->is_true = res->d = 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 :0) - : !res->is_str && !val.is_str && res->d == val.d; - } - } - res->is_str = 0; + if (cmp_expr(filt, data, fn, str+2, end, &val)) { + hts_expr_val_free(&val); + return -1; + } - } else if (str[0] == '!' && str[1] == '=') { - eq_done = 1; - err = cmp_expr(filt, data, fn, str+2, end, &val); - if (err) { - res->is_true = res->d = 0; + 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 { - 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; - } + int eq = res->is_str + ? (res->s.s && val.s.s ? strcmp(res->s.s, val.s.s) == 0 : 0) + : !val.is_str && res->d == val.d; + res->is_true = res->d = (str[0] == '=') ? eq : !eq; } res->is_str = 0; - - } else if ((str[0] == '=' && str[1] == '~') || - (str[0] == '!' && str[1] == '~')) { - eq_done = 1; - err = cmp_expr(filt, data, fn, str+2, end, &val); + } else { + // =~, !~ if (!val.is_str || !res->is_str) { hts_expr_val_free(&val); return -1; @@ -878,7 +843,7 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, } } res->is_true = res->d = regexec(preg, res->s.s, 0, NULL, 0) == 0 - ? *str == '=' // matcn + ? *str == '=' // match : *str == '!'; // no-match if (preg == &preg_) regfree(preg); @@ -889,15 +854,12 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, res->is_str = 0; } - if (eq_done && ((!hts_expr_val_exists(&val)) || !hts_expr_val_exists(res))) + if (!hts_expr_val_exists(&val) || !hts_expr_val_exists(res)) hts_expr_val_undef(res); hts_expr_val_free(&val); - - if (!eq_done || err) - break; } - return err ? -1 : 0; + return 0; } /* From a15aa208cc2aee425d65ac84a63094cb3b794b86 Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Mon, 30 Nov 2026 11:23:08 +0000 Subject: [PATCH 5/5] Fix incorrect result of string vs number equality checking. The expression "abc" == ("abc" < "b") should return 0 as it's comparing a string ("abc") against a number (the boolean result of "abc" < "b"). However the distinction was done on val.s.s and not on val.is_str, which is this case is wrong as val.s.s is still "abc". Assisted-by: Claude:claude-opus-5-5 Signed-off-by: James Bonfield --- hts_expr.c | 10 +++++++--- test/test_expr.c | 3 +++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hts_expr.c b/hts_expr.c index 3ceeadf3d..cee0c7f19 100644 --- a/hts_expr.c +++ b/hts_expr.c @@ -776,9 +776,13 @@ static int eq_expr(hts_filter_t *filt, void *data, hts_expr_sym_func *fn, if (!hts_expr_val_exists(res) || !hts_expr_val_exists(&val)) { hts_expr_val_undef(res); } else { - int eq = res->is_str - ? (res->s.s && val.s.s ? strcmp(res->s.s, val.s.s) == 0 : 0) - : !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; diff --git a/test/test_expr.c b/test/test_expr.c index 112f896d9..26de5cbac 100644 --- a/test/test_expr.c +++ b/test/test_expr.c @@ -303,6 +303,9 @@ int test(void) { { 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;