Skip to content

Improve expression filter stack depth limits, plus fix a precedence bug - #2188

Open
jkbonfield wants to merge 5 commits into
samtools:developfrom
jkbonfield:expr_depth
Open

jkbonfield wants to merge 5 commits into
samtools:developfrom
jkbonfield:expr_depth

Conversation

@jkbonfield

Copy link
Copy Markdown
Contributor
  • Better enforce the expression filtering recursion depth limits with some looping methods such as !!!!!a. We could rewrite this to be non-recursive, but it's a nonsensical scenario anyway.
  • Fix expression parser left-to-right parsing. Specifically cmp_expr and eq_exprcould parse right to left, meaning e.g.3>2>1` gives the wrong result.

@jkbonfield
jkbonfield force-pushed the expr_depth branch 2 times, most recently from 2121c2a to c0840aa Compare November 30, 2026 11:29
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 <jkb@sanger.ac.uk>
This should fail until the next commit.

Signed-off-by: James Bonfield <jkb@sanger.ac.uk>
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 <jkb@sanger.ac.uk>
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 <jkb@sanger.ac.uk>
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 <jkb@sanger.ac.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant