History

avformat/img2enc: allocate temp and target filename dynamically

To avoid truncation of filename with additional .tmp extension.

Marton Balint committed 1 year ago

Commit 7410e291 parent 9b0cc801 file · +22 −13Browse files

Unified · Split

libavformat/img2enc.c+22−13
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include "libavutil/avstring.h"
2929 #include "libavutil/dict.h"
3030 #include "libavutil/log.h"
31+#include "libavutil/mem.h"
3132 #include "libavutil/opt.h"
3233 #include "libavutil/pixdesc.h"
3334 #include "libavutil/time_internal.h"
@@ -42,8 +43,6 @@ typedef struct VideoMuxData { · 8 unmodified lines
4243 int start_img_number;
4344 int img_number;
4445 int split_planes; /**< use independent file for each Y, U, V plane */
45− char tmp[4][1024];
46− char target[4][1024];
4746 int update;
4847 int use_strftime;
4948 int frame_pts;
@@ -141,11 +140,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 91 unmodified lines
141140 {
142141 VideoMuxData *img = s->priv_data;
143142 AVIOContext *pb[4] = {0};
143+ char* target[4] = {0};
144+ char* tmp[4] = {0};
144145 char filename[1024];
145146 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
146147 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
147148 int ret, i;
148− int nb_renames = 0;
149149 AVDictionary *options = NULL;
150150
151151 if (img->update) {
@@ -180,10 +180,16 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 28 unmodified lines
180180 }
181181 for (i = 0; i < 4; i++) {
182182 av_dict_copy(&options, img->protocol_opts, 0);
183− snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);
184− av_strlcpy(img->target[i], filename, sizeof(img->target[i]));
185− if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) {
186− av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : filename);
183+ if (img->use_rename) {
184+ tmp[i] = av_asprintf("%s.tmp", filename);
185+ target[i] = av_strdup(filename);
186+ if (!tmp[i] || !target[i]) {
187+ ret = AVERROR(ENOMEM);
188+ goto fail;
189+ }
190+ }
191+ if (s->io_open(s, &pb[i], tmp[i] ? tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) {
192+ av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", tmp[i] ? tmp[i] : filename);
187193 ret = AVERROR(EIO);
188194 goto fail;
189195 }
@@ -197,8 +203,6 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 7 unmodified lines
197203 break;
198204 filename[strlen(filename) - 1] = "UVAx"[i];
199205 }
200− if (img->use_rename)
201− nb_renames = i + 1;
202206
203207 if (img->split_planes) {
204208 int ysize = par->width * par->height;
@@ -223,10 +227,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 18 unmodified lines
223227 if (ret < 0)
224228 goto fail;
225229
226− for (i = 0; i < nb_renames; i++) {
227− int ret = ff_rename(img->tmp[i], img->target[i], s);
230+ for (i = 0; i < 4 && tmp[i]; i++) {
231+ int ret = ff_rename(tmp[i], target[i], s);
228232 if (ret < 0)
229− return ret;
233+ goto fail;
234+ av_freep(&tmp[i]);
235+ av_freep(&target[i]);
230236 }
231237
232238 img->img_number++;
@@ -234,9 +240,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 1 unmodified line
234240
235241 fail:
236242 av_dict_free(&options);
237− for (i = 0; i < FF_ARRAY_ELEMS(pb); i++)
243+ for (i = 0; i < FF_ARRAY_ELEMS(pb); i++) {
244+ av_freep(&tmp[i]);
245+ av_freep(&target[i]);
238246 if (pb[i])
239247 ff_format_io_close(s, &pb[i]);
248+ }
240249 return ret;
241250 }
242251

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t