diff mbox

[FFmpeg-devel] pnmdec: make sure v is capped by maxval

Message ID 665e0d12-f81d-3e6a-6f4a-2feccf1b20ec@googlemail.com
State Superseded
Headers show

Commit Message

Andreas Cadhalpun Nov. 9, 2016, 12:11 a.m. UTC
Otherwise put_bits can be called with a value that doesn't fit in the
sample_len, causing an assertion failure.
---
 libavcodec/pnmdec.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Michael Niedermayer Nov. 9, 2016, 10:10 a.m. UTC | #1
On Wed, Nov 09, 2016 at 01:11:29AM +0100, Andreas Cadhalpun wrote:
> Otherwise put_bits can be called with a value that doesn't fit in the
> sample_len, causing an assertion failure.
> ---
>  libavcodec/pnmdec.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c
> index ca97cc3..0381ea6 100644
> --- a/libavcodec/pnmdec.c
> +++ b/libavcodec/pnmdec.c
> @@ -145,6 +145,10 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
>                          /* read a sequence of digits */
>                          do {
>                              v = 10*v + c;
> +                             if (v > s->maxval) {
> +                                av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
> +                                return AVERROR_INVALIDDATA;
> +                            }

indention is a bit noisy

i think it can overflow if maxval is large,
it would be faster to check outside the loop

[...]
diff mbox

Patch

diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c
index ca97cc3..0381ea6 100644
--- a/libavcodec/pnmdec.c
+++ b/libavcodec/pnmdec.c
@@ -145,6 +145,10 @@  static int pnm_decode_frame(AVCodecContext *avctx, void *data,
                         /* read a sequence of digits */
                         do {
                             v = 10*v + c;
+                             if (v > s->maxval) {
+                                av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
+                                return AVERROR_INVALIDDATA;
+                            }
                             c = (*s->bytestream++) - '0';
                         } while (c <= 9);
                     }