diff mbox series

[FFmpeg-devel] avcodec/smcenc: Avoid dangling pointers in context

Message ID AM7PR03MB666042F95EF333FBF0772FF88F9C9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 40cf317d09920e18045515cb867a512d071a6cf6
Headers show
Series [FFmpeg-devel] avcodec/smcenc: Avoid dangling pointers in context | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Nov. 19, 2021, 8:45 p.m. UTC
Also reduces codesize: 24E6->2296B with GCC 10 and -O3.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/smcenc.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Comments

Paul B Mahol Nov. 20, 2021, 9:10 a.m. UTC | #1
Commit message is misleading, better to use: switch to stack for
PutByteContext
Andreas Rheinhardt Nov. 20, 2021, 9:12 a.m. UTC | #2
Paul B Mahol:
> Commit message is misleading, better to use: switch to stack for
> PutByteContext
> 
Will do. Although the commit message is correct: The pointers contained
in the PutByteContext become dangling after each encode call.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/smcenc.c b/libavcodec/smcenc.c
index 5b0035b244..52795efb5f 100644
--- a/libavcodec/smcenc.c
+++ b/libavcodec/smcenc.c
@@ -39,7 +39,6 @@ 
 
 typedef struct SMCContext {
     AVFrame *prev_frame;    // buffer for previous source frame
-    PutByteContext pb;
 
     uint8_t mono_value;
     int nb_distinct;
@@ -110,9 +109,9 @@  static int count_distinct_items(const uint8_t *block_values,
      s->color_octets[i][6] == distinct_values[x] || \
      s->color_octets[i][7] == distinct_values[x])
 
-static void smc_encode_stream(SMCContext *s, const AVFrame *frame)
+static void smc_encode_stream(SMCContext *s, const AVFrame *frame,
+                              PutByteContext *pb)
 {
-    PutByteContext *pb = &s->pb;
     const uint8_t *src_pixels = (const uint8_t *)frame->data[0];
     const int stride = frame->linesize[0];
     const uint8_t *prev_pixels = (const uint8_t *)s->prev_frame->data[0];
@@ -492,6 +491,7 @@  static int smc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 {
     SMCContext *s = avctx->priv_data;
     const AVFrame *pict = frame;
+    PutByteContext pb;
     uint8_t *pal;
     int ret;
 
@@ -506,18 +506,18 @@  static int smc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         s->key_frame = 0;
     }
 
-    bytestream2_init_writer(&s->pb, pkt->data, pkt->size);
+    bytestream2_init_writer(&pb, pkt->data, pkt->size);
 
-    bytestream2_put_be32(&s->pb, 0x00);
+    bytestream2_put_be32(&pb, 0x00);
 
     pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
     if (!pal)
         return AVERROR(ENOMEM);
     memcpy(pal, frame->data[1], AVPALETTE_SIZE);
 
-    smc_encode_stream(s, pict);
+    smc_encode_stream(s, pict, &pb);
 
-    av_shrink_packet(pkt, bytestream2_tell_p(&s->pb));
+    av_shrink_packet(pkt, bytestream2_tell_p(&pb));
 
     pkt->data[0] = 0x0;