[FFmpeg-devel,02/15] bytestream: Make get_bytes_left compatible with overread

Submitted by Andreas Rheinhardt on Oct. 17, 2019, 8:29 a.m.

Details

Message ID 20191017082945.13534-2-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Oct. 17, 2019, 8:29 a.m.
bytestream2_get_bytes_left returns an unsigned int; as a result,
it returns big positive numbers if an overread already happened,
making it unsuitable for scenarios where one wants to allow this
(because the buffer is actually padded so that no segfaults can
happen). So add a function returning ptrdiff_t for this purpose;
given that it is intended to be used with the unsafe functions,
it has an "u" suffix, although it is not unsafe by itself.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/bytestream.h | 5 +++++
 1 file changed, 5 insertions(+)

Comments

James Almer Oct. 17, 2019, 1:41 p.m.
On 10/17/2019 5:29 AM, Andreas Rheinhardt wrote:
> bytestream2_get_bytes_left returns an unsigned int; as a result,
> it returns big positive numbers if an overread already happened,
> making it unsuitable for scenarios where one wants to allow this
> (because the buffer is actually padded so that no segfaults can
> happen). So add a function returning ptrdiff_t for this purpose;
> given that it is intended to be used with the unsafe functions,
> it has an "u" suffix, although it is not unsafe by itself.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/bytestream.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/libavcodec/bytestream.h b/libavcodec/bytestream.h
> index 7be7fc22fc..18774524cf 100644
> --- a/libavcodec/bytestream.h
> +++ b/libavcodec/bytestream.h
> @@ -156,6 +156,11 @@ static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *
>      return g->buffer_end - g->buffer;
>  }
>  
> +static av_always_inline ptrdiff_t bytestream2_get_bytes_leftu(GetByteContext *g)
> +{
> +    return g->buffer_end - g->buffer;
> +}

Just make bytestream2_get_bytes_left() return an int instead. It's what
get_bits_left() does, and it's used to detect overreads.
There's no need for a new function.

> +
>  static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
>  {
>      return p->buffer_end - p->buffer;
>

Patch hide | download patch | download mbox

diff --git a/libavcodec/bytestream.h b/libavcodec/bytestream.h
index 7be7fc22fc..18774524cf 100644
--- a/libavcodec/bytestream.h
+++ b/libavcodec/bytestream.h
@@ -156,6 +156,11 @@  static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *
     return g->buffer_end - g->buffer;
 }
 
+static av_always_inline ptrdiff_t bytestream2_get_bytes_leftu(GetByteContext *g)
+{
+    return g->buffer_end - g->buffer;
+}
+
 static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
 {
     return p->buffer_end - p->buffer;