Skip to content

Commit

Permalink
dfilter: Add infinity and NaNs to the float token
Browse files Browse the repository at this point in the history
Parse INF and INFINITY (case insensitively) and NAN and
NAN(char_sequence) as strtod does, bringing back support for
floating point infinity and nan literals which was accidentally
removed in 881dec9

Also add a note about NaN comparisons (right now we make NaNs
compare equal with everything, including non-NaNs.)

Add some tests with infinity and NaN
  • Loading branch information
johnthacker committed Nov 21, 2024
1 parent 3264bbd commit f70ead9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions epan/dfilter/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,18 @@ HexExponent ([pP][+-]?[[:digit:]]+)
return set_lval_float(yyextra, yytext, yytext);
}

(?i:inf)(?i:inity)? {
/* Infinity. */
update_location(yyextra, yytext);
return set_lval_float(yyextra, yytext, yytext);
}

(?i:nan)(\([[:alnum:]_]*\))? {
/* NaNs (including quiet NaNs). */
update_location(yyextra, yytext);
return set_lval_float(yyextra, yytext, yytext);
}

:[[:xdigit:]]+ {
/* Numeric prefixed with ':'. */
update_location(yyextra, yytext);
Expand Down
6 changes: 6 additions & 0 deletions epan/ftypes/ftype-double.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ val_divide(fvalue_t * dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr
static enum ft_result
cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
{
/* In C, NaNs compare unordered with everything, including the same NaN.
* The below makes all NaNs compare equal with everything. We could make
* NaNs ordered by having negative NaNs below -inf and positive NaNs above
* inf. C23 adds the totalorder function (which distinguishes NaNs from
* each other by payload and also distinguishes -0 from 0.)
*/
if (a->value.floating < b->value.floating)
*cmp = -1;
else if (a->value.floating > b->value.floating)
Expand Down
1 change: 1 addition & 0 deletions epan/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ static const char *reserved_filter_names[] = {
"false",
"nan",
"inf",
"infinity",
NULL
};

Expand Down
19 changes: 19 additions & 0 deletions test/suite_dfilter/group_double.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,22 @@ def test_le_2(self, checkDFilterCount):
def test_le_3(self, checkDFilterCount):
dfilter = "icmp.resptime <= 492"
checkDFilterCount(dfilter, 0)

def test_inf_1(self, checkDFilterCount):
dfilter = "icmp.resptime < inf"
checkDFilterCount(dfilter, 1)

def test_inf_2(self, checkDFilterCount):
dfilter = "icmp.resptime > -infinity"
checkDFilterCount(dfilter, 1)

def test_inf_3(self, checkDFilterCount):
# A protocol can't have the name inf or infinity, but a field can
# This is just to check that the filter compiles without error
dfilter = "dvmrp.infinity == 255"
checkDFilterCount(dfilter, 0)

def test_nan(self, checkDFilterCount):
# XXX - We compare NaNs oddly
dfilter = "icmp.resptime == nan"
checkDFilterCount(dfilter, 1)

0 comments on commit f70ead9

Please sign in to comment.