Ticket #1163: 0001-ffprobe-check-for-errors-and-abort-immediately.patch

File 0001-ffprobe-check-for-errors-and-abort-immediately.patch, 10.0 KB (added by saste, 6 years ago)
  • ffprobe.c

    From 10395a90bc0301a62c2eaeb7e92c45fdd93af66a Mon Sep 17 00:00:00 2001
    From: Stefano Sabatini <stefasab@gmail.com>
    Date: Wed, 2 Oct 2013 16:55:15 +0200
    Subject: [PATCH] ffprobe: check for errors, and abort immediately
    
    This allows to make use of validation features.
    ---
     ffprobe.c | 125 ++++++++++++++++++++++++++++++++++++++++++--------------------
     1 file changed, 85 insertions(+), 40 deletions(-)
    
    diff --git a/ffprobe.c b/ffprobe.c
    index 80a286b..5cc11a2 100644
    a b static inline void writer_print_integer(WriterContext *wctx, 
    428428    }
    429429}
    430430
    431 static inline void writer_print_string(WriterContext *wctx,
    432                                        const char *key, const char *val, int opt)
     431static inline int writer_print_string(WriterContext *wctx,
     432                                      const char *key, const char *val, int opt)
    433433{
    434434    const struct section *section = wctx->section[wctx->level];
     435    int ret = 0;
    435436
    436437    if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
    437         return;
     438        return 0;
    438439
    439440    if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
    440441        wctx->writer->print_string(wctx, key, val);
    441442        wctx->nb_item[wctx->level]++;
    442443    }
     444
     445    return ret;
    443446}
    444447
    445448static inline void writer_print_rational(WriterContext *wctx,
    static void writer_register_all(void) 
    14551458#define print_section_header(s) writer_print_section_header(w, s)
    14561459#define print_section_footer(s) writer_print_section_footer(w, s)
    14571460
    1458 static inline void show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
     1461static inline int show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
    14591462{
    14601463    AVDictionaryEntry *tag = NULL;
     1464    int ret = 0;
    14611465
    14621466    if (!tags)
    1463         return;
     1467        return 0;
    14641468    writer_print_section_header(wctx, section_id);
    1465     while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX)))
    1466         writer_print_string(wctx, tag->key, tag->value, 0);
     1469
     1470    while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
     1471        ret = writer_print_string(wctx, tag->key, tag->value, 0);
     1472        if (ret < 0)
     1473            break;
     1474    }
    14671475    writer_print_section_footer(wctx);
     1476
     1477    return ret;
    14681478}
    14691479
    14701480static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
    end: 
    17231733    return ret;
    17241734}
    17251735
    1726 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
     1736static int read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
    17271737{
    17281738    int i, ret = 0;
    17291739    int64_t cur_ts = fmt_ctx->start_time;
    static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx) 
    17381748                break;
    17391749        }
    17401750    }
     1751
     1752    return ret;
    17411753}
    17421754
    1743 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
     1755static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
    17441756{
    17451757    AVStream *stream = fmt_ctx->streams[stream_idx];
    17461758    AVCodecContext *dec_ctx;
    static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i 
    17491761    const char *s;
    17501762    AVRational sar, dar;
    17511763    AVBPrint pbuf;
     1764    int ret = 0;
    17521765
    17531766    av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
    17541767
    static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i 
    19031916    writer_print_section_footer(w);
    19041917    }
    19051918
    1906     show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
     1919    ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
    19071920
    19081921    writer_print_section_footer(w);
    19091922    av_bprint_finalize(&pbuf, NULL);
    19101923    fflush(stdout);
     1924
     1925    return ret;
    19111926}
    19121927
    1913 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
     1928static int show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
    19141929{
    1915     int i;
     1930    int i, ret = 0;
     1931
    19161932    writer_print_section_header(w, SECTION_ID_STREAMS);
    19171933    for (i = 0; i < fmt_ctx->nb_streams; i++)
    1918         if (selected_streams[i])
    1919             show_stream(w, fmt_ctx, i, 0);
     1934        if (selected_streams[i]) {
     1935            ret = show_stream(w, fmt_ctx, i, 0);
     1936            if (ret < 0)
     1937                break;
     1938        }
    19201939    writer_print_section_footer(w);
     1940
     1941    return ret;
    19211942}
    19221943
    1923 static void show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
     1944static int show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
    19241945{
    1925     int i;
     1946    int i, ret = 0;
    19261947
    19271948    writer_print_section_header(w, SECTION_ID_PROGRAM);
    19281949    print_int("program_id", program->id);
    static void show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram * 
    19341955    print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
    19351956    print_ts("end_pts", program->end_time);
    19361957    print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
    1937     show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
     1958    ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
     1959    if (ret < 0)
     1960        goto end;
    19381961
    19391962    writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
    19401963    for (i = 0; i < program->nb_stream_indexes; i++) {
    1941         if (selected_streams[program->stream_index[i]])
    1942             show_stream(w, fmt_ctx, program->stream_index[i], 1);
     1964        if (selected_streams[program->stream_index[i]]) {
     1965            ret = show_stream(w, fmt_ctx, program->stream_index[i], 1);
     1966            if (ret < 0)
     1967                break;
     1968        }
    19431969    }
    19441970    writer_print_section_footer(w);
    19451971
     1972end:
    19461973    writer_print_section_footer(w);
     1974    return ret;
    19471975}
    19481976
    1949 static void show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
     1977static int show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
    19501978{
    1951     int i;
     1979    int i, ret = 0;
    19521980
    19531981    writer_print_section_header(w, SECTION_ID_PROGRAMS);
    19541982    for (i = 0; i < fmt_ctx->nb_programs; i++) {
    19551983        AVProgram *program = fmt_ctx->programs[i];
    19561984        if (!program)
    19571985            continue;
    1958         show_program(w, fmt_ctx, program);
     1986        ret = show_program(w, fmt_ctx, program);
     1987        if (ret < 0)
     1988            break;
    19591989    }
    19601990    writer_print_section_footer(w);
     1991    return ret;
    19611992}
    19621993
    1963 static void show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
     1994static int show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
    19641995{
    1965     int i;
     1996    int i, ret = 0;
    19661997
    19671998    writer_print_section_header(w, SECTION_ID_CHAPTERS);
    19681999    for (i = 0; i < fmt_ctx->nb_chapters; i++) {
    static void show_chapters(WriterContext *w, AVFormatContext *fmt_ctx) 
    19752006        print_time("start_time", chapter->start, &chapter->time_base);
    19762007        print_int("end", chapter->end);
    19772008        print_time("end_time", chapter->end, &chapter->time_base);
    1978         show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
     2009        ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
    19792010        writer_print_section_footer(w);
    19802011    }
    19812012    writer_print_section_footer(w);
     2013
     2014    return ret;
    19822015}
    19832016
    1984 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
     2017static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
    19852018{
    19862019    char val_str[128];
    19872020    int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
     2021    int ret = 0;
    19882022
    19892023    writer_print_section_header(w, SECTION_ID_FORMAT);
    19902024    print_str("filename",         fmt_ctx->filename);
    static void show_format(WriterContext *w, AVFormatContext *fmt_ctx) 
    20022036    if (fmt_ctx->bit_rate > 0) print_val    ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
    20032037    else                       print_str_opt("bit_rate", "N/A");
    20042038    print_int("probe_score", av_format_get_probe_score(fmt_ctx));
    2005     show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
     2039    ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
    20062040
    20072041    writer_print_section_footer(w);
    20082042    fflush(stdout);
     2043    return ret;
    20092044}
    20102045
    20112046static void show_error(WriterContext *w, int err)
    static int probe_file(WriterContext *wctx, const char *filename) 
    21042139    int ret, i;
    21052140    int section_id;
    21062141
     2142#define CHECK_END if (ret < 0) goto end
     2143
    21072144    do_read_frames = do_show_frames || do_count_frames;
    21082145    do_read_packets = do_show_packets || do_count_packets;
    21092146
    21102147    ret = open_input_file(&fmt_ctx, filename);
    2111     if (ret < 0)
    2112         return ret;
     2148    CHECK_END;
    21132149
    21142150    nb_streams_frames  = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
    21152151    nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
    static int probe_file(WriterContext *wctx, const char *filename) 
    21202156            ret = avformat_match_stream_specifier(fmt_ctx,
    21212157                                                  fmt_ctx->streams[i],
    21222158                                                  stream_specifier);
    2123             if (ret < 0)
    2124                 goto end;
     2159            CHECK_END;
    21252160            else
    21262161                selected_streams[i] = ret;
    21272162            ret = 0;
    static int probe_file(WriterContext *wctx, const char *filename) 
    21402175            section_id = SECTION_ID_FRAMES;
    21412176        if (do_show_frames || do_show_packets)
    21422177            writer_print_section_header(wctx, section_id);
    2143         read_packets(wctx, fmt_ctx);
     2178        ret = read_packets(wctx, fmt_ctx);
    21442179        if (do_show_frames || do_show_packets)
    21452180            writer_print_section_footer(wctx);
     2181        CHECK_END;
     2182    }
     2183    if (do_show_programs) {
     2184        ret = show_programs(wctx, fmt_ctx);
     2185        CHECK_END;
     2186    }
     2187
     2188    if (do_show_streams) {
     2189        ret = show_streams(wctx, fmt_ctx);
     2190        CHECK_END;
     2191    }
     2192    if (do_show_chapters) {
     2193        ret = show_chapters(wctx, fmt_ctx);
     2194        CHECK_END;
     2195    }
     2196    if (do_show_format) {
     2197        ret = show_format(wctx, fmt_ctx);
     2198        CHECK_END;
    21462199    }
    2147     if (do_show_programs)
    2148         show_programs(wctx, fmt_ctx);
    2149     if (do_show_streams)
    2150         show_streams(wctx, fmt_ctx);
    2151     if (do_show_chapters)
    2152         show_chapters(wctx, fmt_ctx);
    2153     if (do_show_format)
    2154         show_format(wctx, fmt_ctx);
    21552200
    21562201end:
    21572202    close_input_file(&fmt_ctx);