[FFmpeg-devel] [PATCH v3 3/4] libavcodec/decode: avoid UB when getting plane sizes
Brian Kim
bkkim at google.com
Mon Jul 13 20:09:39 EEST 2020
This uses av_image_fill_plane_sizes instead of av_image_fill_pointers
when we are getting plane sizes to avoid UB from adding offsets to NULL.
Signed-off-by: Brian Kim <bkkim at google.com>
---
libavcodec/decode.c | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index de9c079f9d..8ea9293ebf 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1471,12 +1471,12 @@ static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
switch (avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO: {
- uint8_t *data[4];
int linesize[4];
- int size[4] = { 0 };
int w = frame->width;
int h = frame->height;
- int tmpsize, unaligned;
+ int unaligned;
+ ptrdiff_t tmpsize, linesize1[4];
+ size_t sizes[4];
avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
@@ -1494,21 +1494,20 @@ static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
unaligned |= linesize[i] % pool->stride_align[i];
} while (unaligned);
- tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
- NULL, linesize);
- if (tmpsize < 0) {
- ret = tmpsize;
+ for (i = 0; i < 4; i++)
+ linesize1[i] = linesize[i];
+ ret = av_image_fill_plane_sizes(sizes, avctx->pix_fmt, h, linesize1);
+ if (ret < 0)
goto fail;
- }
-
- for (i = 0; i < 3 && data[i + 1]; i++)
- size[i] = data[i + 1] - data[i];
- size[i] = tmpsize - (data[i] - data[0]);
for (i = 0; i < 4; i++) {
pool->linesize[i] = linesize[i];
- if (size[i]) {
- pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
+ if (sizes[i]) {
+ if (sizes[i] > INT_MAX - (16 + STRIDE_ALIGN - 1)) {
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ pool->pools[i] = av_buffer_pool_init(sizes[i] + 16 + STRIDE_ALIGN - 1,
CONFIG_MEMORY_POISONING ?
NULL :
av_buffer_allocz);
--
2.27.0.389.gc38d7665816-goog
More information about the ffmpeg-devel
mailing list