From e5ce3345db9e370e50bc69d19b928e42b4ddd61f Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Tue, 15 Jun 2021 13:32:46 -0700 Subject: 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.) --- epan/tvbuff.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'epan/tvbuff.c') 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 -- cgit v1.2.3