Make just a single reallocation per call instead of one reallocation
per copied chapters. This fixes possible memory leaks on realloc
failures. Also correct the allocation since it needs multiples of
sizeof(AVChapter*) and not sizeof(AVChapter).
Fixes CID700633 and CID700719.
... | ... |
@@ -1085,8 +1085,14 @@ static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) |
1085 | 1085 |
{ |
1086 | 1086 |
AVFormatContext *is = ifile->ctx; |
1087 | 1087 |
AVFormatContext *os = ofile->ctx; |
1088 |
+ AVChapter **tmp; |
|
1088 | 1089 |
int i; |
1089 | 1090 |
|
1091 |
+ tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters)); |
|
1092 |
+ if (!tmp) |
|
1093 |
+ return AVERROR(ENOMEM); |
|
1094 |
+ os->chapters = tmp; |
|
1095 |
+ |
|
1090 | 1096 |
for (i = 0; i < is->nb_chapters; i++) { |
1091 | 1097 |
AVChapter *in_ch = is->chapters[i], *out_ch; |
1092 | 1098 |
int64_t ts_off = av_rescale_q(ofile->start_time - ifile->ts_offset, |
... | ... |
@@ -1112,11 +1118,7 @@ static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) |
1112 | 1112 |
if (copy_metadata) |
1113 | 1113 |
av_dict_copy(&out_ch->metadata, in_ch->metadata, 0); |
1114 | 1114 |
|
1115 |
- os->nb_chapters++; |
|
1116 |
- os->chapters = av_realloc(os->chapters, sizeof(AVChapter) * os->nb_chapters); |
|
1117 |
- if (!os->chapters) |
|
1118 |
- return AVERROR(ENOMEM); |
|
1119 |
- os->chapters[os->nb_chapters - 1] = out_ch; |
|
1115 |
+ os->chapters[os->nb_chapters++] = out_ch; |
|
1120 | 1116 |
} |
1121 | 1117 |
return 0; |
1122 | 1118 |
} |