From f369b9356c4606cd4d713d60f7db5de119d901fa Mon Sep 17 00:00:00 2001 From: Alexandra Khirnova Date: Tue, 10 Sep 2013 11:57:35 +0200 Subject: [PATCH] avformat: Use av_reallocp_array() where suitable Signed-off-by: Diego Biurrun --- libavformat/asfenc.c | 10 +++++++--- libavformat/gxfenc.c | 20 ++++++++++++-------- libavformat/matroskadec.c | 11 ++++++----- libavformat/matroskaenc.c | 18 ++++++++++++------ libavformat/mov.c | 40 +++++++++++++++++++++------------------ libavformat/mpegts.c | 6 +++--- libavformat/mxfdec.c | 24 +++++++++++++---------- libavformat/oggdec.c | 16 ++++++---------- libavformat/utils.c | 18 ++++++++---------- 9 files changed, 90 insertions(+), 73 deletions(-) diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c index a523b3a051..6be285c531 100644 --- a/libavformat/asfenc.c +++ b/libavformat/asfenc.c @@ -788,10 +788,14 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt) if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) { for (i = asf->nb_index_count; i < start_sec; i++) { if (i >= asf->nb_index_memory_alloc) { + int err; asf->nb_index_memory_alloc += ASF_INDEX_BLOCK; - asf->index_ptr = (ASFIndex *)av_realloc(asf->index_ptr, - sizeof(ASFIndex) * - asf->nb_index_memory_alloc); + if ((err = av_reallocp_array(&asf->index_ptr, + asf->nb_index_memory_alloc, + sizeof(*asf->index_ptr))) < 0) { + asf->nb_index_memory_alloc = 0; + return err; + } } // store asf->index_ptr[i].packet_number = (uint32_t)packet_st; diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c index 128122f9d9..74df237e61 100644 --- a/libavformat/gxfenc.c +++ b/libavformat/gxfenc.c @@ -342,11 +342,13 @@ static int gxf_write_map_packet(AVFormatContext *s, int rewrite) if (!rewrite) { if (!(gxf->map_offsets_nb % 30)) { - gxf->map_offsets = av_realloc(gxf->map_offsets, - (gxf->map_offsets_nb+30)*sizeof(*gxf->map_offsets)); - if (!gxf->map_offsets) { + int err; + if ((err = av_reallocp_array(&gxf->map_offsets, + gxf->map_offsets_nb + 30, + sizeof(*gxf->map_offsets))) < 0) { + gxf->map_offsets_nb = 0; av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n"); - return -1; + return err; } } gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here @@ -873,11 +875,13 @@ static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt) if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { if (!(gxf->flt_entries_nb % 500)) { - gxf->flt_entries = av_realloc(gxf->flt_entries, - (gxf->flt_entries_nb+500)*sizeof(*gxf->flt_entries)); - if (!gxf->flt_entries) { + int err; + if ((err = av_reallocp_array(&gxf->flt_entries, + gxf->flt_entries_nb + 500, + sizeof(*gxf->flt_entries))) < 0) { + gxf->flt_entries_nb = 0; av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n"); - return -1; + return err; } } gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset; diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index d5b60714ac..e8ef81f3d1 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -878,15 +878,16 @@ static int ebml_parse_elem(MatroskaDemuxContext *matroska, uint32_t id = syntax->id; uint64_t length; int res; - void *newelem; data = (char *)data + syntax->data_offset; if (syntax->list_elem_size) { EbmlList *list = data; - newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); - if (!newelem) - return AVERROR(ENOMEM); - list->elem = newelem; + if ((res = av_reallocp_array(&list->elem, + list->nb_elem + 1, + syntax->list_elem_size)) < 0) { + list->nb_elem = 0; + return res; + } data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; memset(data, 0, syntax->list_elem_size); list->nb_elem++; diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 5edaa36c66..86baa65503 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -295,14 +295,17 @@ static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos) { mkv_seekhead_entry *entries = seekhead->entries; + int err; // don't store more elements than we reserved space for if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries) return -1; - entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry)); - if (entries == NULL) - return AVERROR(ENOMEM); + if ((err = av_reallocp_array(&entries, seekhead->num_entries + 1, + sizeof(*entries))) < 0) { + seekhead->num_entries = 0; + return err; + } entries[seekhead->num_entries ].elementid = elementid; entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset; @@ -377,13 +380,16 @@ static mkv_cues * mkv_start_cues(int64_t segment_offset) static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos) { mkv_cuepoint *entries = cues->entries; + int err; if (ts < 0) return 0; - entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint)); - if (entries == NULL) - return AVERROR(ENOMEM); + if ((err = av_reallocp_array(&entries, cues->num_entries + 1, + sizeof(*entries))) < 0) { + cues->num_entries = 0; + return err; + } entries[cues->num_entries ].pts = ts; entries[cues->num_entries ].tracknum = stream + 1; diff --git a/libavformat/mov.c b/libavformat/mov.c index 6614f89394..9c72426539 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -1841,7 +1841,6 @@ static void mov_build_index(MOVContext *mov, AVStream *st) unsigned int stps_index = 0; unsigned int i, j; uint64_t stream_size = 0; - AVIndexEntry *mem; /* adjust first dts according to edit list */ if (sc->time_offset && mov->time_scale > 0) { @@ -1875,10 +1874,12 @@ static void mov_build_index(MOVContext *mov, AVStream *st) return; if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) return; - mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries)); - if (!mem) + if (av_reallocp_array(&st->index_entries, + st->nb_index_entries + sc->sample_count, + sizeof(*st->index_entries)) < 0) { + st->nb_index_entries = 0; return; - st->index_entries = mem; + } st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries); for (i = 0; i < sc->chunk_count; i++) { @@ -1973,10 +1974,12 @@ static void mov_build_index(MOVContext *mov, AVStream *st) av_dlog(mov->fc, "chunk count %d\n", total); if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) return; - mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries)); - if (!mem) + if (av_reallocp_array(&st->index_entries, + st->nb_index_entries + total, + sizeof(*st->index_entries)) < 0) { + st->nb_index_entries = 0; return; - st->index_entries = mem; + } st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries); // populate index @@ -2307,13 +2310,15 @@ static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom) static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom) { MOVTrackExt *trex; + int err; if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) return AVERROR_INVALIDDATA; - trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data)); - if (!trex) - return AVERROR(ENOMEM); - c->trex_data = trex; + if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1, + sizeof(*c->trex_data))) < 0) { + c->trex_count = 0; + return err; + } trex = &c->trex_data[c->trex_count++]; avio_r8(pb); /* version */ avio_rb24(pb); /* flags */ @@ -2335,7 +2340,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) int64_t dts; int data_offset = 0; unsigned entries, first_sample_flags = frag->flags; - int flags, distance, i, found_keyframe = 0; + int flags, distance, i, found_keyframe = 0, err; for (i = 0; i < c->fc->nb_streams; i++) { if (c->fc->streams[i]->id == frag->track_id) { @@ -2373,12 +2378,11 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) } if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) return AVERROR_INVALIDDATA; - ctts_data = av_realloc(sc->ctts_data, - (entries+sc->ctts_count)*sizeof(*sc->ctts_data)); - if (!ctts_data) - return AVERROR(ENOMEM); - sc->ctts_data = ctts_data; - + if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count, + sizeof(*sc->ctts_data))) < 0) { + sc->ctts_count = 0; + return err; + } if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb); if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb); dts = sc->track_end - sc->time_offset; diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index ba41f7fbd9..d623452130 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -201,10 +201,10 @@ static void clear_programs(MpegTSContext *ts) static void add_pat_entry(MpegTSContext *ts, unsigned int programid) { struct Program *p; - void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program)); - if(!tmp) + if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) { + ts->nb_prg = 0; return; - ts->prg = tmp; + } p = &ts->prg[ts->nb_prg]; p->id = programid; p->nb_pids = 0; diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index d2039f6c35..7c0f6573f5 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -410,18 +410,20 @@ static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, U static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset) { MXFContext *mxf = arg; - MXFPartition *partition, *tmp_part; + MXFPartition *partition; UID op; uint64_t footer_partition; uint32_t nb_essence_containers; + int err; if (mxf->partitions_count+1 >= UINT_MAX / sizeof(*mxf->partitions)) return AVERROR(ENOMEM); - tmp_part = av_realloc(mxf->partitions, (mxf->partitions_count + 1) * sizeof(*mxf->partitions)); - if (!tmp_part) - return AVERROR(ENOMEM); - mxf->partitions = tmp_part; + if ((err = av_reallocp_array(&mxf->partitions, mxf->partitions_count + 1, + sizeof(*mxf->partitions))) < 0) { + mxf->partitions_count = 0; + return err; + } if (mxf->parsing_backward) { /* insert the new partition pack in the middle @@ -546,13 +548,15 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set) { - MXFMetadataSet **tmp; + int err; + if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets)) return AVERROR(ENOMEM); - tmp = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets)); - if (!tmp) - return AVERROR(ENOMEM); - mxf->metadata_sets = tmp; + if ((err = av_reallocp_array(&mxf->metadata_sets, mxf->metadata_sets_count + 1, + sizeof(*mxf->metadata_sets))) < 0) { + mxf->metadata_sets_count = 0; + return err; + } mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set; mxf->metadata_sets_count++; return 0; diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c index d22d5bc5b8..023492d969 100644 --- a/libavformat/oggdec.c +++ b/libavformat/oggdec.c @@ -84,7 +84,7 @@ static int ogg_restore(AVFormatContext *s, int discard) struct ogg *ogg = s->priv_data; AVIOContext *bc = s->pb; struct ogg_state *ost = ogg->state; - int i; + int i, err; if (!ost) return 0; @@ -92,7 +92,6 @@ static int ogg_restore(AVFormatContext *s, int discard) ogg->state = ost->next; if (!discard) { - struct ogg_stream *old_streams = ogg->streams; for (i = 0; i < ogg->nstreams; i++) av_free(ogg->streams[i].buf); @@ -100,16 +99,13 @@ static int ogg_restore(AVFormatContext *s, int discard) avio_seek(bc, ost->pos, SEEK_SET); ogg->curidx = ost->curidx; ogg->nstreams = ost->nstreams; - ogg->streams = av_realloc(ogg->streams, - ogg->nstreams * sizeof(*ogg->streams)); - - if (ogg->streams) { + if ((err = av_reallocp_array(&ogg->streams, ogg->nstreams, + sizeof(*ogg->streams))) < 0) { + ogg->nstreams = 0; + return err; + } else memcpy(ogg->streams, ost->streams, ost->nstreams * sizeof(*ogg->streams)); - } else { - av_free(old_streams); - ogg->nstreams = 0; - } } av_free(ost); diff --git a/libavformat/utils.c b/libavformat/utils.c index 3065c71ba4..83c8fe1ad1 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -2574,14 +2574,11 @@ AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c) { AVStream *st; int i; - AVStream **streams; - if (s->nb_streams >= INT_MAX/sizeof(*streams)) + if (av_reallocp_array(&s->streams, s->nb_streams + 1, sizeof(*s->streams)) < 0) { + s->nb_streams = 0; return NULL; - streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams)); - if (!streams) - return NULL; - s->streams = streams; + } st = av_mallocz(sizeof(AVStream)); if (!st) @@ -2674,7 +2671,6 @@ void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int i { int i, j; AVProgram *program=NULL; - void *tmp; if (idx >= ac->nb_streams) { av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx); @@ -2689,10 +2685,12 @@ void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int i if(program->stream_index[j] == idx) return; - tmp = av_realloc(program->stream_index, sizeof(unsigned int)*(program->nb_stream_indexes+1)); - if(!tmp) + if (av_reallocp_array(&program->stream_index, + program->nb_stream_indexes + 1, + sizeof(*program->stream_index)) < 0) { + program->nb_stream_indexes = 0; return; - program->stream_index = tmp; + } program->stream_index[program->nb_stream_indexes++] = idx; return; }