all: Use av_memdup() where appropriate

Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-11-27 10:50:47 +01:00
parent b574fb472e
commit a4798a5d51
8 changed files with 11 additions and 25 deletions

View File

@ -1104,12 +1104,10 @@ int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
avctx->sw_pix_fmt = fmt[n - 1]; 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) if (!choices)
return AV_PIX_FMT_NONE; return AV_PIX_FMT_NONE;
memcpy(choices, fmt, (n + 1) * sizeof(*choices));
for (;;) { for (;;) {
// Remove the previous hwaccel, if there was one. // Remove the previous hwaccel, if there was one.
hwaccel_uninit(avctx); hwaccel_uninit(avctx);

View File

@ -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)); 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]) { if (!rect->data[0]) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
goto fail; goto fail;
} }
memcpy(rect->data[0], region->pbuf, region->buf_size);
if ((clut == &default_clut && ctx->compute_clut < 0) || ctx->compute_clut == 1) { if ((clut == &default_clut && ctx->compute_clut < 0) || ctx->compute_clut == 1) {
if (!region->has_computed_clut) { if (!region->has_computed_clut) {
compute_default_clut(ctx, region->computed_clut, rect, rect->w, rect->h); 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); clut = get_clut(ctx, clut_id);
if (!clut) { if (!clut) {
clut = av_malloc(sizeof(*clut)); clut = av_memdup(&default_clut, sizeof(*clut));
if (!clut) if (!clut)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
memcpy(clut, &default_clut, sizeof(*clut));
clut->id = clut_id; clut->id = clut_id;
clut->version = -1; clut->version = -1;

View File

@ -1116,10 +1116,9 @@ static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
HFParam hf[4]; HFParam hf[4];
/* duplicate input */ /* 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) if (!in)
return AVERROR(ENOMEM); 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); highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);

View File

@ -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)) if (os->nb_extra_packets >= FF_ARRAY_ELEMS(os->extra_packets))
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
os->extra_packet_sizes[os->nb_extra_packets] = size; 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]) if (!os->extra_packets[os->nb_extra_packets])
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
memcpy(os->extra_packets[os->nb_extra_packets], buf, size);
os->nb_extra_packets++; os->nb_extra_packets++;
} else if (type == 0x12) { } else if (type == 0x12) {
if (os->metadata) if (os->metadata)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
os->metadata_size = size - 11 - 4; 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) if (!os->metadata)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
memcpy(os->metadata, buf + 11, os->metadata_size);
} }
buf += size; buf += size;
buf_size -= size; buf_size -= size;

View File

@ -938,10 +938,9 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
// audio track - add a second stream for this // audio track - add a second stream for this
AVStream *sub_st; AVStream *sub_st;
// priv_data cannot be shared between streams // 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) if (!sub_pes)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
memcpy(sub_pes, pes, sizeof(*sub_pes));
sub_st = avformat_new_stream(pes->stream, NULL); sub_st = avformat_new_stream(pes->stream, NULL);
if (!sub_st) { if (!sub_st) {

View File

@ -320,10 +320,9 @@ static int vorbis_header(AVFormatContext *s, int idx)
return priv->vp ? 0 : AVERROR_INVALIDDATA; return priv->vp ? 0 : AVERROR_INVALIDDATA;
priv->len[pkt_type >> 1] = os->psize; 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]) if (!priv->packet[pkt_type >> 1])
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
if (os->buf[os->pstart] == 1) { if (os->buf[os->pstart] == 1) {
const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */ const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
unsigned blocksize, bs0, bs1; unsigned blocksize, bs0, bs1;

View File

@ -387,10 +387,9 @@ static void copy_default_source_addrs(struct RTSPSource **addrs, int count,
int i; int i;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
rtsp_src = addrs[i]; rtsp_src = addrs[i];
rtsp_src2 = av_malloc(sizeof(*rtsp_src2)); rtsp_src2 = av_memdup(rtsp_src, sizeof(*rtsp_src));
if (!rtsp_src2) if (!rtsp_src2)
continue; continue;
memcpy(rtsp_src2, rtsp_src, sizeof(*rtsp_src));
dynarray_add(dest, dest_count, rtsp_src2); dynarray_add(dest, dest_count, rtsp_src2);
} }
} }

View File

@ -244,10 +244,8 @@ int av_bprint_finalize(AVBPrint *buf, char **ret_str)
str = buf->str; str = buf->str;
buf->str = NULL; buf->str = NULL;
} else { } else {
str = av_malloc(real_size); str = av_memdup(buf->str, real_size);
if (str) if (!str)
memcpy(str, buf->str, real_size);
else
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
} }
*ret_str = str; *ret_str = str;