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
28#include "libavutil/avstring.h"28#include "libavutil/avstring.h"
29#include "libavutil/dict.h"29#include "libavutil/dict.h"
30#include "libavutil/log.h"30#include "libavutil/log.h"
31#include "libavutil/mem.h"
31#include "libavutil/opt.h"32#include "libavutil/opt.h"
32#include "libavutil/pixdesc.h"33#include "libavutil/pixdesc.h"
33#include "libavutil/time_internal.h"34#include "libavutil/time_internal.h"
@@ -42,8 +43,6 @@ typedef struct VideoMuxData { · 8 unmodified lines
42 int start_img_number;43 int start_img_number;
43 int img_number;44 int img_number;
44 int split_planes; /**< use independent file for each Y, U, V plane */45 int split_planes; /**< use independent file for each Y, U, V plane */
45 char tmp[4][1024];
46 char target[4][1024];
47 int update;46 int update;
48 int use_strftime;47 int use_strftime;
49 int frame_pts;48 int frame_pts;
@@ -141,11 +140,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 91 unmodified lines
141{140{
142 VideoMuxData *img = s->priv_data;141 VideoMuxData *img = s->priv_data;
143 AVIOContext *pb[4] = {0};142 AVIOContext *pb[4] = {0};
143 char* target[4] = {0};
144 char* tmp[4] = {0};
144 char filename[1024];145 char filename[1024];
145 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;146 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
146 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);147 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
147 int ret, i;148 int ret, i;
148 int nb_renames = 0;
149 AVDictionary *options = NULL;149 AVDictionary *options = NULL;
150 150
151 if (img->update) {151 if (img->update) {
@@ -180,10 +180,16 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 28 unmodified lines
180 }180 }
181 for (i = 0; i < 4; i++) {181 for (i = 0; i < 4; i++) {
182 av_dict_copy(&options, img->protocol_opts, 0);182 av_dict_copy(&options, img->protocol_opts, 0);
183 snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);183 if (img->use_rename) {
184 av_strlcpy(img->target[i], filename, sizeof(img->target[i]));184 tmp[i] = av_asprintf("%s.tmp", filename);
185 if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) {185 target[i] = av_strdup(filename);
186 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : 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);
187 ret = AVERROR(EIO);193 ret = AVERROR(EIO);
188 goto fail;194 goto fail;
189 }195 }
@@ -197,8 +203,6 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 7 unmodified lines
197 break;203 break;
198 filename[strlen(filename) - 1] = "UVAx"[i];204 filename[strlen(filename) - 1] = "UVAx"[i];
199 }205 }
200 if (img->use_rename)
201 nb_renames = i + 1;
202 206
203 if (img->split_planes) {207 if (img->split_planes) {
204 int ysize = par->width * par->height;208 int ysize = par->width * par->height;
@@ -223,10 +227,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 18 unmodified lines
223 if (ret < 0)227 if (ret < 0)
224 goto fail;228 goto fail;
225 229
226 for (i = 0; i < nb_renames; i++) {230 for (i = 0; i < 4 && tmp[i]; i++) {
227 int ret = ff_rename(img->tmp[i], img->target[i], s);231 int ret = ff_rename(tmp[i], target[i], s);
228 if (ret < 0)232 if (ret < 0)
229 return ret;233 goto fail;
234 av_freep(&tmp[i]);
235 av_freep(&target[i]);
230 }236 }
231 237
232 img->img_number++;238 img->img_number++;
@@ -234,9 +240,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 1 unmodified line
234 240
235fail:241fail:
236 av_dict_free(&options);242 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]);
238 if (pb[i])246 if (pb[i])
239 ff_format_io_close(s, &pb[i]);247 ff_format_io_close(s, &pb[i]);
248 }
240 return ret;249 return ret;
241}250}
242 251

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