libavfilter/buffersrc.c:381: error: Use After Free
  accessing memory that was invalidated by call to `free()` on line 380 indirectly during the call to `ff_add_format()`.

libavfilter/buffersrc.c:371:5: invalidation part of the trace starts here
369.     BufferSourceContext *c = ctx->priv;
370.     AVFilterChannelLayouts *channel_layouts = NULL;
371.     AVFilterFormats *formats = NULL;
         ^
372.     AVFilterFormats *samplerates = NULL;
373. 

libavfilter/buffersrc.c:371:5: variable `formats` declared here
  369.     BufferSourceContext *c = ctx->priv;
  370.     AVFilterChannelLayouts *channel_layouts = NULL;
  371.     AVFilterFormats *formats = NULL;
           ^
  372.     AVFilterFormats *samplerates = NULL;
  373. 

libavfilter/buffersrc.c:380:9: when calling `ff_add_format` here
  378.         break;
  379.     case AVMEDIA_TYPE_AUDIO:
  380.         ff_add_format(&formats,           c->sample_fmt);
               ^
  381.         ff_set_common_formats(ctx, formats);
  382. 

libavfilter/formats.c:201:1: parameter `avff` of ff_add_format
    199. } while (0)
    200. 
    201. int ff_add_format(AVFilterFormats **avff, int fmt)
         ^
    202. {
    203.     ADD_FORMAT(avff, fmt, int, formats, nb_formats);

libavfilter/formats.c:203:5: when calling `av_freep` here
    201. int ff_add_format(AVFilterFormats **avff, int fmt)
    202. {
    203.     ADD_FORMAT(avff, fmt, int, formats, nb_formats);
             ^
    204. }
    205. 

libavutil/mem.c:198:1: parameter `arg` of av_freep
      196. }
      197. 
      198. void av_freep(void *arg)
           ^
      199. {
      200.     void **ptr = (void **)arg;

libavutil/mem.c:200:5: assigned
      198. void av_freep(void *arg)
      199. {
      200.     void **ptr = (void **)arg;
               ^
      201.     av_free(*ptr);
      202.     *ptr = NULL;

libavutil/mem.c:201:5: when calling `av_free` here
      199. {
      200.     void **ptr = (void **)arg;
      201.     av_free(*ptr);
               ^
      202.     *ptr = NULL;
      203. }

libavutil/mem.c:186:1: parameter `ptr` of av_free
        184. }
        185. 
        186. void av_free(void *ptr)
             ^
        187. {
        188. #if CONFIG_MEMALIGN_HACK

libavutil/mem.c:194:5: was invalidated by call to `free()`
        192.     _aligned_free(ptr);
        193. #else
        194.     free(ptr);
                 ^
        195. #endif
        196. }

libavfilter/buffersrc.c:371:5: use-after-lifetime part of the trace starts here
369.     BufferSourceContext *c = ctx->priv;
370.     AVFilterChannelLayouts *channel_layouts = NULL;
371.     AVFilterFormats *formats = NULL;
         ^
372.     AVFilterFormats *samplerates = NULL;
373. 

libavfilter/buffersrc.c:371:5: variable `formats` declared here
  369.     BufferSourceContext *c = ctx->priv;
  370.     AVFilterChannelLayouts *channel_layouts = NULL;
  371.     AVFilterFormats *formats = NULL;
           ^
  372.     AVFilterFormats *samplerates = NULL;
  373. 

libavfilter/buffersrc.c:381:36: invalid access occurs here
  379.     case AVMEDIA_TYPE_AUDIO:
  380.         ff_add_format(&formats,           c->sample_fmt);
  381.         ff_set_common_formats(ctx, formats);
                                          ^
  382. 
  383.         ff_add_format(&samplerates,       c->sample_rate);

static int query_formats(AVFilterContext *ctx)
{
    BufferSourceContext *c = ctx->priv;
    AVFilterChannelLayouts *channel_layouts = NULL;
    AVFilterFormats *formats = NULL;
    AVFilterFormats *samplerates = NULL;
    switch (ctx->outputs[0]->type) {
    case AVMEDIA_TYPE_VIDEO:
        ff_add_format(&formats, c->pix_fmt);
        ff_set_common_formats(ctx, formats);
        break;
    case AVMEDIA_TYPE_AUDIO:
        ff_add_format(&formats,           c->sample_fmt);
        ff_set_common_formats(ctx, formats);
        ff_add_format(&samplerates,       c->sample_rate);
        ff_set_common_samplerates(ctx, samplerates);
        ff_add_channel_layout(&channel_layouts, c->channel_layout);
        ff_set_common_channel_layouts(ctx, channel_layouts);
        break;
    default:
        return AVERROR(EINVAL);
    }
    return 0;
}
['static int query_formats(AVFilterContext *ctx)\n{\n    BufferSourceContext *c = ctx->priv;\n    AVFilterChannelLayouts *channel_layouts = NULL;\n    AVFilterFormats *formats = NULL;\n    AVFilterFormats *samplerates = NULL;\n    switch (ctx->outputs[0]->type) {\n    case AVMEDIA_TYPE_VIDEO:\n        ff_add_format(&formats, c->pix_fmt);\n        ff_set_common_formats(ctx, formats);\n        break;\n    case AVMEDIA_TYPE_AUDIO:\n        ff_add_format(&formats,           c->sample_fmt);\n        ff_set_common_formats(ctx, formats);\n        ff_add_format(&samplerates,       c->sample_rate);\n        ff_set_common_samplerates(ctx, samplerates);\n        ff_add_channel_layout(&channel_layouts, c->channel_layout);\n        ff_set_common_channel_layouts(ctx, channel_layouts);\n        break;\n    default:\n        return AVERROR(EINVAL);\n    }\n    return 0;\n}', 'int ff_add_format(AVFilterFormats **avff, int fmt)\n{\n    ADD_FORMAT(avff, fmt, int, formats, nb_formats);\n}', 'void av_freep(void *arg)\n{\n    void **ptr = (void **)arg;\n    av_free(*ptr);\n    *ptr = NULL;\n}', 'void av_free(void *ptr)\n{\n#if CONFIG_MEMALIGN_HACK\n    if (ptr)\n        free((char *)ptr - ((char *)ptr)[-1]);\n#elif HAVE_ALIGNED_MALLOC\n    _aligned_free(ptr);\n#else\n    free(ptr);\n#endif\n}']