[FFmpeg-devel] [PATCH] all: Use av_memdup() where appropriate

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Dec 3 11:27:28 EET 2021


Andreas Rheinhardt:
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> ---
>  libavcodec/decode.c          | 4 +---
>  libavcodec/dvbsubdec.c       | 8 ++------
>  libavcodec/g723_1enc.c       | 3 +--
>  libavformat/hdsenc.c         | 6 ++----
>  libavformat/mpegts.c         | 3 +--
>  libavformat/oggparsevorbis.c | 3 +--
>  libavformat/rtsp.c           | 3 +--
>  libavutil/bprint.c           | 6 ++----
>  8 files changed, 11 insertions(+), 25 deletions(-)
> 
> diff --git a/libavcodec/decode.c b/libavcodec/decode.c
> index c44724d150..52bf5dcd33 100644
> --- a/libavcodec/decode.c
> +++ b/libavcodec/decode.c
> @@ -1104,12 +1104,10 @@ int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
>          avctx->sw_pix_fmt = fmt[n - 1];
>      }
>  
> -    choices = av_malloc_array(n + 1, sizeof(*choices));
> +    choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
>      if (!choices)
>          return AV_PIX_FMT_NONE;
>  
> -    memcpy(choices, fmt, (n + 1) * sizeof(*choices));
> -
>      for (;;) {
>          // Remove the previous hwaccel, if there was one.
>          hwaccel_uninit(avctx);
> diff --git a/libavcodec/dvbsubdec.c b/libavcodec/dvbsubdec.c
> index d192f3251d..81ccaf4c57 100644
> --- a/libavcodec/dvbsubdec.c
> +++ b/libavcodec/dvbsubdec.c
> @@ -823,14 +823,12 @@ static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_ou
>              }
>              memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(*clut_table));
>  
> -            rect->data[0] = av_malloc(region->buf_size);
> +            rect->data[0] = av_memdup(region->pbuf, region->buf_size);
>              if (!rect->data[0]) {
>                  ret = AVERROR(ENOMEM);
>                  goto fail;
>              }
>  
> -            memcpy(rect->data[0], region->pbuf, region->buf_size);
> -
>              if ((clut == &default_clut && ctx->compute_clut < 0) || ctx->compute_clut == 1) {
>                  if (!region->has_computed_clut) {
>                      compute_default_clut(ctx, region->computed_clut, rect, rect->w, rect->h);
> @@ -1074,12 +1072,10 @@ static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
>      clut = get_clut(ctx, clut_id);
>  
>      if (!clut) {
> -        clut = av_malloc(sizeof(*clut));
> +        clut = av_memdup(&default_clut, sizeof(*clut));
>          if (!clut)
>              return AVERROR(ENOMEM);
>  
> -        memcpy(clut, &default_clut, sizeof(*clut));
> -
>          clut->id = clut_id;
>          clut->version = -1;
>  
> diff --git a/libavcodec/g723_1enc.c b/libavcodec/g723_1enc.c
> index 2b3cccee09..2a8149b4cd 100644
> --- a/libavcodec/g723_1enc.c
> +++ b/libavcodec/g723_1enc.c
> @@ -1116,10 +1116,9 @@ static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
>      HFParam hf[4];
>  
>      /* duplicate input */
> -    start = in = av_malloc(frame->nb_samples * sizeof(int16_t));
> +    start = in = av_memdup(frame->data[0], frame->nb_samples * sizeof(int16_t));
>      if (!in)
>          return AVERROR(ENOMEM);
> -    memcpy(in, frame->data[0], frame->nb_samples * sizeof(int16_t));
>  
>      highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
>  
> diff --git a/libavformat/hdsenc.c b/libavformat/hdsenc.c
> index e5353bac65..64d9f1413d 100644
> --- a/libavformat/hdsenc.c
> +++ b/libavformat/hdsenc.c
> @@ -93,19 +93,17 @@ static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
>              if (os->nb_extra_packets >= FF_ARRAY_ELEMS(os->extra_packets))
>                  return AVERROR_INVALIDDATA;
>              os->extra_packet_sizes[os->nb_extra_packets] = size;
> -            os->extra_packets[os->nb_extra_packets] = av_malloc(size);
> +            os->extra_packets[os->nb_extra_packets] = av_memdup(buf, size);
>              if (!os->extra_packets[os->nb_extra_packets])
>                  return AVERROR(ENOMEM);
> -            memcpy(os->extra_packets[os->nb_extra_packets], buf, size);
>              os->nb_extra_packets++;
>          } else if (type == 0x12) {
>              if (os->metadata)
>                  return AVERROR_INVALIDDATA;
>              os->metadata_size = size - 11 - 4;
> -            os->metadata      = av_malloc(os->metadata_size);
> +            os->metadata      = av_memdup(buf + 11, os->metadata_size);
>              if (!os->metadata)
>                  return AVERROR(ENOMEM);
> -            memcpy(os->metadata, buf + 11, os->metadata_size);
>          }
>          buf      += size;
>          buf_size -= size;
> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
> index 36ab7ab3af..2479cb6f7d 100644
> --- a/libavformat/mpegts.c
> +++ b/libavformat/mpegts.c
> @@ -938,10 +938,9 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
>              // audio track - add a second stream for this
>              AVStream *sub_st;
>              // priv_data cannot be shared between streams
> -            PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
> +            PESContext *sub_pes = av_memdup(pes, sizeof(*sub_pes));
>              if (!sub_pes)
>                  return AVERROR(ENOMEM);
> -            memcpy(sub_pes, pes, sizeof(*sub_pes));
>  
>              sub_st = avformat_new_stream(pes->stream, NULL);
>              if (!sub_st) {
> diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
> index 77e8d301b2..e1ef510892 100644
> --- a/libavformat/oggparsevorbis.c
> +++ b/libavformat/oggparsevorbis.c
> @@ -320,10 +320,9 @@ static int vorbis_header(AVFormatContext *s, int idx)
>          return priv->vp ? 0 : AVERROR_INVALIDDATA;
>  
>      priv->len[pkt_type >> 1]    = os->psize;
> -    priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
> +    priv->packet[pkt_type >> 1] = av_memdup(os->buf + os->pstart, os->psize);
>      if (!priv->packet[pkt_type >> 1])
>          return AVERROR(ENOMEM);
> -    memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
>      if (os->buf[os->pstart] == 1) {
>          const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
>          unsigned blocksize, bs0, bs1;
> diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
> index a1aa969cad..abd448246d 100644
> --- a/libavformat/rtsp.c
> +++ b/libavformat/rtsp.c
> @@ -383,10 +383,9 @@ static void copy_default_source_addrs(struct RTSPSource **addrs, int count,
>      int i;
>      for (i = 0; i < count; i++) {
>          rtsp_src = addrs[i];
> -        rtsp_src2 = av_malloc(sizeof(*rtsp_src2));
> +        rtsp_src2 = av_memdup(rtsp_src, sizeof(*rtsp_src));
>          if (!rtsp_src2)
>              continue;
> -        memcpy(rtsp_src2, rtsp_src, sizeof(*rtsp_src));
>          dynarray_add(dest, dest_count, rtsp_src2);
>      }
>  }
> diff --git a/libavutil/bprint.c b/libavutil/bprint.c
> index a6849d7178..ea51d9b09c 100644
> --- a/libavutil/bprint.c
> +++ b/libavutil/bprint.c
> @@ -244,10 +244,8 @@ int av_bprint_finalize(AVBPrint *buf, char **ret_str)
>                  str = buf->str;
>              buf->str = NULL;
>          } else {
> -            str = av_malloc(real_size);
> -            if (str)
> -                memcpy(str, buf->str, real_size);
> -            else
> +            str = av_memdup(buf->str, real_size);
> +            if (!str)
>                  ret = AVERROR(ENOMEM);
>          }
>          *ret_str = str;
> 

Will apply tonight unless there are objections.

- Andreas


More information about the ffmpeg-devel mailing list