avformat: Add max_streams option

This allows user apps to stop OOM due to excessive number of streams

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
(cherry picked from commit 1296f84495)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer 2016-11-18 17:00:30 +01:00
parent 774461ea62
commit c6fbff1358
4 changed files with 13 additions and 1 deletions

View File

@ -205,6 +205,10 @@ For example to separate the fields with newlines and indention:
ffprobe -dump_separator " ffprobe -dump_separator "
" -i ~/videos/matrixbench_mpeg2.mpg " -i ~/videos/matrixbench_mpeg2.mpg
@end example @end example
@item max_streams @var{integer} (@emph{input})
Specifies the maximum number of streams. This can be used to reject files that
would require too many resources due to a large number of streams.
@end table @end table
@c man end FORMAT OPTIONS @c man end FORMAT OPTIONS

View File

@ -1822,6 +1822,13 @@ typedef struct AVFormatContext {
* Demuxing: Set by user. * Demuxing: Set by user.
*/ */
int (*open_cb)(struct AVFormatContext *s, AVIOContext **p, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options); int (*open_cb)(struct AVFormatContext *s, AVIOContext **p, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options);
/**
* The maximum number of streams.
* - encoding: unused
* - decoding: set by user through AVOptions (NO direct access)
*/
int max_streams;
} AVFormatContext; } AVFormatContext;
int av_format_get_probe_score(const AVFormatContext *s); int av_format_get_probe_score(const AVFormatContext *s);

View File

@ -109,6 +109,7 @@ static const AVOption avformat_options[] = {
{"dump_separator", "set information dump field separator", OFFSET(dump_separator), AV_OPT_TYPE_STRING, {.str = ", "}, CHAR_MIN, CHAR_MAX, D|E}, {"dump_separator", "set information dump field separator", OFFSET(dump_separator), AV_OPT_TYPE_STRING, {.str = ", "}, CHAR_MIN, CHAR_MAX, D|E},
{"codec_whitelist", "List of decoders that are allowed to be used", OFFSET(codec_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D }, {"codec_whitelist", "List of decoders that are allowed to be used", OFFSET(codec_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D },
{"format_whitelist", "List of demuxers that are allowed to be used", OFFSET(format_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D }, {"format_whitelist", "List of demuxers that are allowed to be used", OFFSET(format_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D },
{"max_streams", "maximum number of streams", OFFSET(max_streams), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, D },
{NULL}, {NULL},
}; };

View File

@ -3764,7 +3764,7 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
int i; int i;
AVStream **streams; AVStream **streams;
if (s->nb_streams >= INT_MAX/sizeof(*streams)) if (s->nb_streams >= FFMIN(s->max_streams, INT_MAX/sizeof(*streams)))
return NULL; return NULL;
streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams)); streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
if (!streams) if (!streams)