diff options
author | Guy Harris <gharris@sonic.net> | 2021-06-15 13:32:46 -0700 |
---|---|---|
committer | Guy Harris <gharris@sonic.net> | 2021-06-15 13:32:46 -0700 |
commit | e5ce3345db9e370e50bc69d19b928e42b4ddd61f (patch) | |
tree | 1ad1c87d1adf61c144b3ff46733af47528720205 /epan | |
parent | 2c6d897b5882c942b8615651aae7aab47aa462fd (diff) |
tvbuff: add tvb_ensure_reported_length_remaining().
It is to tvb_reported_length_remaining() as
tvb_ensure_captured_length_remaining() is to
tvb_captured_length_remaining() - it throws an exception if the offset
is out of range.
(Note that an offset that's just past the end of the {reported,
captured} data is *not* out of range, it just means that there is no
data remaining. Anything *past* that is out of range and thus invalid.)
Diffstat (limited to 'epan')
-rw-r--r-- | epan/tvbuff.c | 21 | ||||
-rw-r--r-- | epan/tvbuff.h | 4 |
2 files changed, 22 insertions, 3 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c index b154791a4d..d737fad84f 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -539,9 +539,6 @@ tvb_ensure_captured_length_remaining(const tvbuff_t *tvb, const gint offset) return rem_length; } - - - /* Validates that 'length' bytes are available starting from * offset (pos/neg). Does not throw an exception. */ gboolean @@ -713,6 +710,24 @@ tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset) return 0; } +guint +tvb_ensure_reported_length_remaining(const tvbuff_t *tvb, const gint offset) +{ + guint abs_offset = 0; + int exception; + + DISSECTOR_ASSERT(tvb && tvb->initialized); + + exception = compute_offset(tvb, offset, &abs_offset); + if (exception) + THROW(exception); + + if (tvb->reported_length >= abs_offset) + return tvb->reported_length - abs_offset; + else + THROW(ReportedBoundsError); +} + /* Set the reported length of a tvbuff to a given value; used for protocols * whose headers contain an explicit length and where the calling * dissector's payload may include padding as well as the packet for diff --git a/epan/tvbuff.h b/epan/tvbuff.h index cd8c850544..128bcc7d2a 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -252,6 +252,10 @@ WS_DLL_PUBLIC guint tvb_reported_length(const tvbuff_t *tvb); WS_DLL_PUBLIC gint tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset); +/** Same as above, but throws an exception if the offset is out of bounds. */ +WS_DLL_PUBLIC guint tvb_ensure_reported_length_remaining(const tvbuff_t *tvb, + const gint offset); + /** Set the reported length of a tvbuff to a given value; used for protocols whose headers contain an explicit length and where the calling dissector's payload may include padding as well as the packet for |