Commit Graph

123 Commits

Author SHA1 Message Date
João Valverde 07371d4557 dfilter: Split tostr() into debug and pretty print 2021-10-11 21:55:45 +00:00
João Valverde 5dd90e3b30 dfilter: Cache stnode_tostr()
This avoids having to save/free the pointer for each tostr()
invocation (or leak memory).
2021-10-11 21:55:45 +00:00
João Valverde 2c701ddf6f dfilter: Improve grammar to parse ranges
Do the integer conversion for ranges in the parser. This is more
conventional, I think, and allows removing the unnecessary integer
syntax tree node type.

Try to minimize the number and complexity of lexical rules for
ranges. But it seems we need to keep different states for integer
and punctuation because of the need to disambiguate the ranges
[-n-n] and [-n--n].
2021-10-08 19:18:56 +01:00
João Valverde 9d87c4712e dfilter: Fix parsing of value strings
If we have a STRING value in an expression and a numeric comparison
we must also check if it matches a value string before throwing
a type error.

Add appropriate tests to the test suite.

Fixes 4d2f469212.
2021-10-08 18:53:15 +01:00
João Valverde 541d45fc6f dfilter: Remove no-op statement 2021-10-08 04:01:24 +00:00
João Valverde 4d2f469212 dfilter: Use exceptions for error handling in semcheck
Instead of checking for an error return and throwing the exception
then do it where the errors occurs. This takes advantage of the nice
properties of error exceptions to reduce the amount of error
checking code.
2021-10-07 23:01:50 +00:00
João Valverde 5fcdf25697 dfilter: Generalize special case of one byte literal
Instead of only accepting a byte literal specification if the LHS is a
len-1 byte string, accept it everywhere bytes are wanted.

Before:
  $ dftest "frame[1] contains 0x01"
  Filter: frame[1] contains 0x01

  Constants:
  00000 PUT_FVALUE	01 <FT_BYTES> -> reg#2

  Instructions:
  (...)

  $ dftest "frame[1:4] contains 0x01"
  Filter: frame[1:4] contains 0x01
  dftest: "0x01" is not a valid byte string.

After:
  $ dftest "frame[1:4] contains 0x01"
  $ Filter: frame[1:4] contains 0x01

  Constants:
  00000 PUT_FVALUE	01 <FT_BYTES> -> reg#2

  Instructions:
  (...)
2021-10-07 23:01:50 +00:00
João Valverde 8c5a4f9100 dfilter: Replace node accessor macros with functions
Replace macro magic to improve ease of comprehension and maintenance.
2021-10-06 15:44:48 +00:00
João Valverde 4804c1224d dfilter: Use syntax tree node replacement semantics
Instead of using 3 operations (new + free + reassign_to_parent) to transform
the tree use a simpler single replace operation instead.

This also avoids having to manually copy token values.

The set search and replace method is now obsolete.
2021-10-06 10:34:21 +00:00
João Valverde d45ba348fd dfilter: Strengthen sanity check for range
Allow an entity in the grammar as range body. Perform a stronger
sanity check during semantic analysis everywhere a range is used.
This is both safer (unless we want to allow FIELD bodies only, but
functions are allowed too) and also provides better error messages.

Previously a range of range only compiled on the RHS. Now it can
appear on both sides of a relation.

This fixes a crash with STRING entities similar to #10690 for
UNPARSED.

This also adds back support for slicing functions that was removed
in f3f833ccec (by accident presumably).

Ping #10690
2021-10-05 16:39:41 +01:00
João Valverde db18865e55 dfilter: Save token value to syntax tree
When parsing we save the token value to the syntax tree. This is
useful for better error reporting. Use it to report an invalid
entity for the slice operation. Before only the memory location
was reported, which is not a good error message.

Before:
  % dftest '"01:02:03:04"[0:3] == foo'
  Filter: ""01:02:03:04"[0:3] == foo"
  dftest: Range is not supported for entity <0x7f6c84017740> of type STRING

After:
  % dftest '"01:02:03:04"[0:3] == foo'
  Filter: ""01:02:03:04"[0:3] == foo"
  dftest: Range is not supported for entity 01:02:03:04 of type STRING

When creating a new node from an old one we need to copy the token
value. Simple tokens such as RBRACKET, COMMA and COLON are
not part of the AST and don't have an associated semantic value.
2021-10-01 16:04:37 +00:00
João Valverde 487e2b6bc3 dfilter: Remove unnecessary log activation check
Use log_write_always_full() instead of ws_log() to avoid a useless
activation check.

Rename stnode_log() to log_stnode() for consistency.
2021-10-01 16:04:37 +00:00
João Valverde b4af7c52a5 dfilter: Add a flags member to the syntax tree node
Use it to record "inside parenthesis".
2021-09-30 17:03:55 +00:00
João Valverde 0e7ba54d98 dfilter: Clean up handling of "deprecated" tokens
Pass the deprecated data struture to the scanner and insert the deprecated
tokens there. This avoids having to keep a dedicated syntax node field
for this.

Pass the deprecated argument in dfwork_t instead of in a separate
argument. This is less cumbersome than adding an extra argument
to every level of the semantic checker.
2021-09-30 17:26:19 +01:00
João Valverde 3ea2a61f2a dfilter: Display syntax tree for debugging
Use wslog to output debug information. Being able to control
it at runtime is a big advantage.

We extend the syntax tree nodes with a method to return a
canonical string representation.

Add a routine to walk the tree and return an textual representation
for debugging purposes.
2021-09-30 16:29:11 +01:00
John Thacker a2120e823c dfilter: Allow generic unquoted strings that are protocol names on RHS
Protocol names can only be on the left hand side of filter expressions.
Commit ac0b1d42f3 (merge !4214) caused
unquoted strings that could be interpreted as either protocol names or
byte arrays to be parsed as the latter when on the RHS.  Further
relax by allowing unquoted strings on the RHS that can be interpreted as
protocol names to be treated as any generic unquoted string. (The
semantic checker will still prefer interpreting the string as a byte
array, if possible, to a generic string.)

This is useful for filter expressions of the sort "frame contains data",
where data should be interpreted as "data", i.e. "\x64\x61\x74\x61".
Long run this ideally should be fixed earlier, at the lex parser or
grammar checker.
2021-09-22 11:22:14 +00:00
John Thacker ac0b1d42f3 dfilter: Allow bytestrings that are also protocol names on RHS
Protocol names can only be on the left hand side of filter expressions.
If a protocol name with an even number of characters ("fc", "dc", "ff",
"fefd", etc.) is on the right hand side of a filter expressions and
can be interpreted as a byte string instead, do so.  Fix #12810.
2021-09-18 22:42:50 +00:00
João Valverde 0e50979b3f Replace g_assert() with ws_assert() 2021-06-19 01:23:31 +00:00
João Valverde 85c257431f dfilter: Add support for raw strings
Add support for a literal string specification copied from Python
raw strings[1].

Raw string literals are enclosed with r"..." or R"...". Double quotes
can be include in the string but they must be escaped with backslash.
In escape sequences backslashes are preserved in the final result.

So for example the string "a\\\"b" is the same as r"a\"b".

r"\\\a" is the same as "\\\\\\a".

Raw strings should be used for convenience wherever a regular expression
is used in a display filter expression.

[1]https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
2021-06-05 02:46:40 +01:00
João Valverde 9ba97d12d6 Add ws_debug() and use it
Replace most instances of ws_debug_printf() except in
epan/dissectors and dissector plugins.

Some replacements use printf(), some use ws_debug(), and
some were removed because they were dead or judged to be
temporary.
2021-05-24 01:13:19 +00:00
Guy Harris b61fd6d76a dfilter, ftypes: get rid of FT_PCRE.
It's not a valid field type, it's only a hack to support regular
expression matching in packet-matching expressions.

Instead, in the packet-matching code, have a separate syntax tree type
for Perl-compatible regular expressions, and a separate instruction to
load one into a register, and have the "matching" operator for field
types take a GRegex * as the second argument.
2021-03-21 03:27:44 -07:00
Guy Harris ce611792c3 dfilter: clean up indentation. 2021-03-21 02:15:00 -07:00
Guy Harris f9fc064255 Cast __LINE__ to unsigned int and print it with %u.
No version of ISO C I can find (from C90 to C18) says anything more
about __LINE__ than that it's an "integer constant", with no indication
whether it's signed or unsigned (or whether it's int or long or long
long), so we just cast it to "unsigned int" and print it with %u, as it
would make Absolutely No Sense if it were negative.

This should squelch some invalidPrintfArgType_uint warnings from
cppcheck.
2021-01-20 13:48:08 -08:00
Guy Harris 272502790b Add FT_STRINGZTRUNC.
FT_STRINGZPAD is for null-*padded* strings, where the field is in an
area of specified length, and, if the string is shorter than that
length, all bytes past the end of the string are NULs.

FT_STRINGZTRUNC is for null-*truncated* strings, where the field is in
an area of specified length and, if the string is shorter than that
length, there's a null character (which might be more than one byte, for
UCS-2, UTF-16, or UTF-32), and anything after that is not guaranteed to
have any particular value.

Use IS_FT_STRING() in some places rather than enumerating all the string
types, so that those places get automatically changed if the set of
string types changes.
2020-09-12 14:16:12 -07:00
Guy Harris 20800366dd HTTPS (almost) everywhere.
Change all wireshark.org URLs to use https.

Fix some broken links while we're at it.

Change-Id: I161bf8eeca43b8027605acea666032da86f5ea1c
Reviewed-on: https://code.wireshark.org/review/34089
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2019-07-26 18:44:40 +00:00
Guy Harris 333e915cff Better name for the routine.
It doesn't necessarily produce an FT_BYTES value any more.

Change-Id: I7bad1e328394a829400bd139c48a9538c4892818
Reviewed-on: https://code.wireshark.org/review/28318
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2018-06-18 00:30:37 +00:00
Guy Harris 078a53f994 For the contains operator, both sides have to have the same type.
Have charconst_to_bytes() take the desired type as an argument, and pass
it to dfilter_fvalue_from_unparsed().

Bug: 14084
Change-Id: I11db417311b9681b18c4a3fca2862b35837194d7
Reviewed-on: https://code.wireshark.org/review/28315
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2018-06-17 22:02:47 +00:00
Peter Wu 1ff82572ca dfilter: add range support to set membership operator ("f in {x .. y}")
Allow "tcp.srcport in {1662 1663 1664}" to be abbreviated to
"tcp.srcport in {1662 .. 1664}". The range operator is supported for any
field value which supports the "<=" and "=>" operators and thus works
for integers, IP addresses, etc.

The naive mapping "tcp.srcport >= 1662 and tcp.srcport <= 1664" is not
used because it does not have the intended effect with fields that have
multiple occurrences (e.g. tcp.port). Each condition could be satisfied
by an other value. Therefore a new DVFM instruction (ANY_IN_RANGE) is
added to test the range condition against each individual field value.

Bug: 14180
Change-Id: I53c2d0f9bc9d4f0ffaabde9a83442122965c95f7
Reviewed-on: https://code.wireshark.org/review/26945
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
2018-04-18 03:47:02 +00:00
Dario Lombardo 55c68ee69c epan: use SPDX indentifiers.
Skipping dissectors dir for now.

Change-Id: I717b66bfbc7cc81b83f8c2cbc011fcad643796aa
Reviewed-on: https://code.wireshark.org/review/25694
Petri-Dish: Dario Lombardo <lomato@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
2018-02-08 19:29:45 +00:00
Peter Wu 2f35a811a3 dfilter: fix memleak when using value_string values
When using a filter such as "ncp.alloc_reply_lvl2 == FALSE", a memory
leak would occur as follows:

 1. dfilter_fvalue_from_unparsed is called and
 2. ends up calling _uint64_from_unparsed
 3. which fails with error message "\"FALSE\" is not a valid number.".
 4. Next, mk_fvalue_from_val_string is called which maps "FALSE" to 0
 5. and the filter is successfully compiled.
 6. dfwork_free deliberately does not free the error message (since
    there should be none at this point) and we have a memleak (from 3).

Fix this memleak by clearing the error message when a successful
value_string mapping is found.

Change-Id: I78d59a4336342b09dc5448ea994b2e1d199d7f3f
Link: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=1302
Reviewed-on: https://code.wireshark.org/review/21497
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
2017-05-04 18:56:10 +00:00
Alexis La Goutte 26950539d6 semcheck: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
Change-Id: I000f9df1e41cab7fa0132153ff95caf31f3f6a6a
Reviewed-on: https://code.wireshark.org/review/17892
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2016-10-25 03:20:08 +00:00
Guy Harris c786aa56ad Fix a copy-and-pasteo.
Fix the handling of the case of a "matches" operator with a field and a
character constant as operands.

Change-Id: I6451cee907d8486db96937173d0098de91ce7956
Reviewed-on: https://code.wireshark.org/review/18424
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2016-10-24 08:17:09 +00:00
Guy Harris d7fe514fc0 Improve support for single-character fields and filter expressions.
Add an FT_CHAR type, which is like FT_UINT8 except that the value is
displayed as a C-style character constant.

Allow use of C-style character constants in filter expressions; they can
be used in comparisons with all integral types, and in "contains"
operators.

Use that type for some fields that appear (based on the way they're
displayed, or on the use of C-style character constants in their
value_string tables) to be 1-byte characters rather than 8-bit numbers.

Change-Id: I39a9f0dda0bd7f4fa02a9ca8373216206f4d7135
Reviewed-on: https://code.wireshark.org/review/17787
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2016-09-19 02:51:13 +00:00
Michael Mann 1da1f945e2 Fix checkAPI.pl warnings about printf
Many of the complaints from checkAPI.pl for use of printf are when its embedded
in an #ifdef and checkAPI isn't smart enough to figure that out.
The other (non-ifdef) use is dumping internal structures (which is a type of
debug functionality)
Add a "ws_debug_printf" macro for printf to pacify the warnings.

Change-Id: I63610e1adbbaf2feffb4ec9d4f817247d833f7fd
Reviewed-on: https://code.wireshark.org/review/16623
Reviewed-by: Michael Mann <mmann78@netscape.net>
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
2016-07-25 04:26:50 +00:00
Guy Harris 3570d264c1 FT_BOOLEAN is now stored as a 64-bit value; treat it as such.
Always look and set the uinteger64 member of the union for FT_BOOLEAN
values.

Bug: 12236
Change-Id: I7b0166e564b9d6cbb80051a81714a4b4c5f740a2
Reviewed-on: https://code.wireshark.org/review/14481
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2016-03-15 21:21:00 +00:00
João Valverde 7f520c352d Fix two warnings for semcheck.c
semcheck.c:986:24: warning: cast from function call of type 'sttype_id_t' to non-matching type 'int' [-Wbad-function-cast]
semcheck.c:986:5: warning: format '%p' expects argument of type 'void *', but argument 3 has type 'struct stnode_t *' [-Wformat=]

Change-Id: I83031251c83f6597eb7c31f35e02c5a95bd2dabb
Reviewed-on: https://code.wireshark.org/review/13930
Petri-Dish: João Valverde <j@v6e.pt>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: João Valverde <j@v6e.pt>
2016-02-14 22:10:22 +00:00
Michal Labedzki 22d561c436 Add support for IEEE-11073 FLOATs
Add support for IEEE-11073 32-bit FLOAT and 16 bit SFLOAT field types.
Use them in Bluetooth ATT dissector.

Change-Id: Ife0f3843da84a9af23483b0a0d9b69cd2f511d08
Reviewed-on: https://code.wireshark.org/review/12680
Petri-Dish: Michal Labedzki <michal.labedzki@tieto.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michal Labedzki <michal.labedzki@tieto.com>
Tested-by: Michal Labedzki <michal.labedzki@tieto.com>
Petri-Dish: Alexis La Goutte <alexis.lagoutte@gmail.com>
Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
2015-12-29 14:20:02 +00:00
Jeffrey Smith 80322d88da dfilter: Add membership operator
Added a new relational test: 'x in {a b c}'.  The only LHS entity
supported at this time is a field.  The generated DFVM operations are
equivalent to an OR'ed series of =='s, but with the redundant existence
tests removed.

Change-Id: Iddc89b81cf7ad6319aef1a2a94f93314cb721a8a
Reviewed-on: https://code.wireshark.org/review/10246
Reviewed-by: Hadriel Kaplan <hadrielk@yahoo.com>
Petri-Dish: Hadriel Kaplan <hadrielk@yahoo.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2015-09-11 06:31:33 +00:00
Pascal Quantin 523ce152e8 Do not try to display a value_string for FT_FRAMENUM type
The hfinfo->strings pointer is used to store a ft_framenum_type_t

Bug: 11325
Change-Id: Ia6ee1bdd4f1e6ff93907e6107fcecab56c0320de
Reviewed-on: https://code.wireshark.org/review/9458
Reviewed-by: Pascal Quantin <pascal.quantin@gmail.com>
Petri-Dish: Pascal Quantin <pascal.quantin@gmail.com>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2015-07-02 11:36:11 +00:00
JC Wren e2bcee6ac4 Make FT_{U}INT64 behave more like FT_{U}INT32, add support for FT_{U}INT{40,48,56}
Change-Id: I57354c309ecf3a0c8f0c7cff485638027f30bb19
Reviewed-on: https://code.wireshark.org/review/5813
Reviewed-by: Michael Mann <mmann78@netscape.net>
Petri-Dish: Michael Mann <mmann78@netscape.net>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
2015-02-21 16:57:52 +00:00
Bill Meier 3e3fc9fc5e epan/dfilter/*.c: As needed: Add editor modelines & Fix indentation
Change-Id: I410839329a98bd806c60961dfb9693d5eeeeb702
Reviewed-on: https://code.wireshark.org/review/7104
Reviewed-by: Bill Meier <wmeier@newsguy.com>
2015-02-13 19:04:44 +00:00
Guy Harris cfcbb28671 Clean up ftype-conversion and dfilter error message string handling.
Have dfilter_compile() take an additional gchar ** argument, pointing to
a gchar * item that, on error, gets set to point to a g_malloc()ed error
string.  That removes one bit of global state from the display filter
parser, and doesn't impose a fixed limit on the error message strings.

Have fvalue_from_string() and fvalue_from_unparsed() take a gchar **
argument, pointer to a gchar * item, rather than an error-reporting
function, and set the gchar * item to point to a g_malloc()ed error
string on an error.

Allow either gchar ** argument to be null; if the argument is null, no
error message is allocated or provided.

Change-Id: Ibd36b8aaa9bf4234aa6efa1e7fb95f7037493b4c
Reviewed-on: https://code.wireshark.org/review/6608
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2015-01-18 10:22:59 +00:00
Michael Mann 71c02b20eb Create FT_FCWWN field type.
Also, convert the "string" hf_ entries that used tvb_fcwwn_to_str as a string to use proto_tree_add_item with FT_FCWWN type.

Change-Id: I4ca77870499fd8239584a70874998b5d194a7167
Reviewed-on: https://code.wireshark.org/review/6036
Petri-Dish: Michael Mann <mmann78@netscape.net>
Reviewed-by: Michael Mann <mmann78@netscape.net>
2014-12-28 20:05:29 +00:00
Michal Labedzki ee4a7f2c5d DisplayFilter: Check also another fields with the same name
This adds possibility to have two fields with the same abbrev name
but different type, for example FT_ETHER and FT_STRING.
That allows to compare each one to find a valid field.

Change-Id: I8b2a1708ac9648b7a4289777c72a0f3b18f3d8f8
Reviewed-on: https://code.wireshark.org/review/5702
Petri-Dish: Michal Labedzki <michal.labedzki@tieto.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michal Labedzki <michal.labedzki@tieto.com>
2014-12-18 11:02:37 +00:00
Peter Wu f2b4daf400 Add printf-format annotations, fix garbage
The WRETH dissector showed up some garbage in the column display. Upon
further inspection, it turns out that the format string had a trailing
percent sign which caused (unsigned)-1 to be returned by
g_printf_string_upper_bound (in emem_strdup_vprintf). Then ep_alloc is
called with (unsigned)-1 + 1 = 0 memory, no wonder that garbage shows
up. ASAN could not even catch this error because EP is in charge of
this.

So, start adding G_GNUC_PRINTF annotations in each header that uses
the "fmt" or "format" paramters (grepped + awk). This revealed some
other errors. The NCP2222 dissector was missing a format string (not
a security vuln though).

Many dissectors used val_to_str with a constant (but empty) string,
these have been replaced by val_to_str_const. ASN.1 dissectors
were regenerated for this.

Minor: the mate plugin used "%X" instead of "%p" for a pointer type.

The ncp2222 dissector and wimax plugin gained modelines.

Change-Id: I7f3f6a3136116f9b251719830a39a7b21646f622
Reviewed-on: https://code.wireshark.org/review/2881
Reviewed-by: Evan Huus <eapache@gmail.com>
2014-07-06 23:00:40 +00:00
Guy Harris cb16dff992 Get rid of more tvb_get_nstringz* calls.
Add an FT_STRINGZPAD type, for null-padded strings (typically
fixed-length fields, where the string can be up to the length of the
field, and is null-padded if it's shorter than that), and use it.  Use
IS_FT_STRING() in more cases, so that less code needs to know what types
are string types.

Add a tvb_get_stringzpad() routine, which gets null-padded strings.
Currently, it does the same thing that tvb_get_string_enc() does, but
that might change if we don't store string values as null-terminated
strings.

Change-Id: I46f56e130de8f419a19b56ded914e24cc7518a66
Reviewed-on: https://code.wireshark.org/review/1082
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-04-12 22:27:22 +00:00
Alexis La Goutte 296591399f Remove all $Id$ from top of file
(Using sed : sed -i '/^ \* \$Id\$/,+1 d')

Fix manually some typo (in export_object_dicom.c and crc16-plain.c)

Change-Id: I4c1ae68d1c4afeace8cb195b53c715cf9e1227a8
Reviewed-on: https://code.wireshark.org/review/497
Reviewed-by: Anders Broman <a.broman58@gmail.com>
2014-03-04 14:27:33 +00:00
Michael Mann bcd2ecfc94 Add FT_SYSTEM_ID for type checking
svn path=/trunk/; revision=54394
2013-12-23 17:57:57 +00:00
Jakub Zawadzki 12d1509aa1 Move struct _ftype_t + callback typedefs + free macro to ftypes-int.h
svn path=/trunk/; revision=53223
2013-11-10 13:14:09 +00:00
Michael Mann ee208c8dcc Add support for RELATIVE-OID ASN.1 type. Bug 9192 (https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9192)
From Ed Beroset.

svn path=/trunk/; revision=52393
2013-10-06 02:31:10 +00:00