From patchwork Mon Sep 7 02:49:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22142 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 308F74484C1 for ; Mon, 7 Sep 2020 05:55:48 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0974768A370; Mon, 7 Sep 2020 05:55:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com [209.85.208.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B5D4368084B for ; Mon, 7 Sep 2020 05:55:41 +0300 (EEST) Received: by mail-lj1-f194.google.com with SMTP id u21so4047334ljl.6 for ; Sun, 06 Sep 2020 19:55:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+4+Xx/0E2P3lJ43nOxOvSYvtr2E2kt8696NPgNY/l4c=; b=cowSktDQOJkoE0q32brluaOSGYajBkIudysGr9KMZx9S+UQDWBQJwksgF2d/4HDV4e SgU26gIktXO2K6FhW77KZEUj9LUQzWfL14U/i6XxhTQGZqOQ9b9oT8PBbYuw78V/69cp fzNDd9mPBdhVCSQ3FahF9dMf4V+IkOHkGNTg27m0GqZRbFo64STXVyOnl9MjCZa3BTVG vUfhQldE51poRxS9fVH82OC9WgbnYlJ5mEpy3KI5SmMspPKjsH6obDK9XIYSPHXwQWxh Yps62caQae6lvQ4ALb3wujwNP79ncEAkXpzUDfmJUYp00Bx0LAvIvcv1xsc9+tHg+XYo bXpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+4+Xx/0E2P3lJ43nOxOvSYvtr2E2kt8696NPgNY/l4c=; b=Pt+t0MiA2VK7VrhOwxLR+OMv8CfLDq/jogha1ArC24Uq+70NTvdlKHTU5OsJqDZLMW vqQefpRgOpSjYyfqZTxp14UjOfEH3AXsYpH9/YT9A7g7RFMwZxMBNmjUPwmTLe9umqs/ u2P2OM/hVCt3MdHvbVqFn17OWbv7/eeXQhSJuad/2Z1kKw6QhDaZnclNbdXK/36L9jLQ uCIZ1MH5oZllqaC4Ul2Bquji4XgTfSb1urBdLyANTA7C5m/0Wo04IV0IwpVR09cMWZO/ QafI/rqiJBN9v1ybENDSp4m11zuJxPRhmlJI+n/DPCLd4H4P7zicf876/zXVlJgx9tLC 45OA== X-Gm-Message-State: AOAM533HrWlbeHpJJKpdtg+QiUwQ3Bvg0xQCfKzJWp7ylHDzFxKVZKMB EU18Vm4AxaKgEzkTyehNzcsld4Ee8TI= X-Google-Smtp-Source: ABdhPJx8aIOtOUa52qutVDzhufBoAtT5FxMUJUhmfi2tL/DXymA98kCyFI1QGnNpBjdtREAPEIevxw== X-Received: by 2002:a17:906:8c8:: with SMTP id o8mr18780271eje.91.1599447002083; Sun, 06 Sep 2020 19:50:02 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.50.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:50:01 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:43 +0200 Message-Id: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/10] avformat/segment: Don't overwrite AVCodecParameters after init X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The segment muxer copies the user-provided AVCodecParameters to the newly created child streams in its init function before initializing the child muxer; and since commit 8e6478b723affe4d44f94d34b98e0c47f6a0b411, it does this again before calling avformat_write_header() if that is called from seg_write_header(). The reason for this is complicated: At that time writing the header was delayed, i.e. it was not triggered by avformat_write_header() (unless the AVFMT_FLAG_AUTO_BSF was unset), but instead by writing the very first packet. The rationale behind this was to allow to run bitstream filters on the packets in the interleavement queue in order to generate missing extradata from them before the muxer's write_header function is actually called. The segment muxer went even further: It initialized the child muxer and ran the child muxer's check_bitstream functions on the packets in its own muxing queue and stole any bitstream filters that got inserted. The reason for this is that the segment muxer has an option to write the header to a separate file and for this it is needed to write the child muxer's header without delay, but with correct extradata. Unsetting AVFMT_FLAG_AUTO_BSF for the child muxer accomplished the first goal and stealing the bitstream filters the second; and in order for the child muxer to actually use the updated extradata, the old AVCodecParameters (set before avformat_init_output()) were overwritten with the new ones. Updating the extradata proceeded as follows: The bitstream filter itself simply updated the AVBSFContext's par_out when processing a packet, in violation of the new BSF API (where par_out may only be set in the init function); the muxing code then simply forwarded the updated extradata, overwriting the par_in of the next BSF in the BSF chain with the fresh par_out of the last one and the AVStream's par with the par_out of the last BSF. This was an API violation, too, of course, but it made remuxing ADTS AAC into mp4/matroska work. But this no longer serves a useful purpose since the aac_adtstoasc BSF was updated to propagate new extradata via packet side data in commit f63c3516577d605e51cf16358cbdfa0bc97565d8; the next commit then removed the code in mux.c passing new extradata along the filter chain. This alone justifies removing the code for setting the AVCodecParameters a second time. But there is even another reason to do so: It is harmful. The ogg muxer parses the extradata of Theora and Vorbis in its init function and keeps pointers to parts of it. Said pointers become dangling when the extradata is overwritten by the segment muxer, leading to use-after-frees as has happened in ticket #8881 which this commit fixes. Ticket #8517 is about another issue caused by this: Immediately after having overwritten the old AVCodecParameters the segment muxer checks whether the codec_tag is ok (the codec_tag is set generically when initializing the child muxer based upon muxer-specific lists). The check used is: If the child output format has such a list and if the codec tag of the non-child stream does not match the codec id given the list of codec tags and if there is a match for the codec id in the codec tag list, then set the codec tag to zero (and not to the existing match), otherwise set the codec tag of the child stream to the codec tag of the corresponding stream of the main AVFormatContext (which is btw redundant given that the child AVCodecParameters have just been overwritten with the AVCodecParameters of the corresponding stream of the main AVFormatContext). Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index f67456fa57..0c9b93725d 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -817,26 +817,9 @@ static int seg_write_header(AVFormatContext *s) { SegmentContext *seg = s->priv_data; AVFormatContext *oc = seg->avf; - int ret, i; + int ret; if (!seg->header_written) { - for (i = 0; i < s->nb_streams; i++) { - AVStream *st = oc->streams[i]; - AVCodecParameters *ipar, *opar; - - ipar = s->streams[i]->codecpar; - opar = oc->streams[i]->codecpar; - avcodec_parameters_copy(opar, ipar); - if (!oc->oformat->codec_tag || - av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id || - av_codec_get_tag(oc->oformat->codec_tag, ipar->codec_id) <= 0) { - opar->codec_tag = ipar->codec_tag; - } else { - opar->codec_tag = 0; - } - st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio; - st->time_base = s->streams[i]->time_base; - } ret = avformat_write_header(oc, NULL); if (ret < 0) return ret; From patchwork Mon Sep 7 02:49:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22144 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1CFF344A09D for ; Mon, 7 Sep 2020 05:58:45 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D8FF468A609; Mon, 7 Sep 2020 05:58:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com [209.85.208.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A676A68017B for ; Mon, 7 Sep 2020 05:58:38 +0300 (EEST) Received: by mail-lj1-f194.google.com with SMTP id w3so14280747ljo.5 for ; Sun, 06 Sep 2020 19:58:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=df6wun98nVlY1+BW2JhgHLnoNCoi6l1yZ4ILAFMaXVk=; b=S21zXlHUONaibyUzhIvyj4Ign9CWtmzrQRJyJt+6FZD/ZjtlclUYSlVk5ktFLkEKvT yiKdoRTxhq60O7UuvEmVFVMzsOD3F/jLeoemsauL3zLhDGcPdCB0NzAhL59J8QC3Mb9w S9Blhn1veAdOIteOA6I4/0CsUiDyT+bC4efeliddGgUdb65olN9McVKTNAaTwjyWYpAJ 7mn2R1hqYowTpTfKyDFGvzCdnySzmXeBxV9Ynz+uk9Tt4aIZkssu6D2w+Ilapt5g61qf UGDyRLkRWJmUDToKc/0BgWC1NTIqx+DI1sXsL9i73EvjqMMJYyqM5jtSFSjOZBRtJqC2 pkrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=df6wun98nVlY1+BW2JhgHLnoNCoi6l1yZ4ILAFMaXVk=; b=PeLunCjIHbMAkez5ovNp/RhGaCybrKIyeT0+jwlN6te90KPg42lcSvbt0WjmJK9Kbd fGdaYbTW3agnLq/rf/oIYDLAnq38VQ3hwugMVGzxNgSJ40xgsTVJTmnBaPZE6EKJ0uvY 4R3MwenyBQqdwbkRaxL1ORFwguqCB2B0EaKKcshGfFexN0N19P5v3i75T1gdTXfZhe1Q iH8jsG5nobq+CrnOQ4TzXGhKZAQ+7FA1zgYMTTbZgpk/qeBB+XlppbmLK4g3avNTWWhM zKAr2kOVSCvkOpwsf9JJ3RMXonfxI0pasOAcMBDzTAKVIyhbZHrKMmumuKdMPMdmTSVy A6+A== X-Gm-Message-State: AOAM533ET47J83Us94vA9GWJg5SCH6tzSP7u2bpIhl9XnnK5/85UBZaJ DVDkk0dTwKH5/yST/eKcLxOLBM2QYxY= X-Google-Smtp-Source: ABdhPJxGfEgjN7bCHz2dE7q4FhJ7yJnG46WGXhl01+m6v/ct0WajwqqI0pBUvE97dW2sldKWZHdDEQ== X-Received: by 2002:a50:fe07:: with SMTP id f7mr20345978edt.173.1599447066894; Sun, 06 Sep 2020 19:51:06 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:06 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:44 +0200 Message-Id: <20200907024952.11697-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/10] avformat/segment: Fix leak of duration/framenumber lists upon error X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The code to free them is not in the segment muxer's deinit function, but in its write_trailer function which means that these lists leak if write_trailer isn't called after their allocation. This happens e.g. if the given lists are invalid (e.g. consisting only of ',' (which delimit entries)), so that parsing them fails and so does the muxer's init function; write_trailer is then never called. This has been fixed by moving the code to free them to the deinit function. Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 0c9b93725d..0c96c8c50c 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -663,6 +663,8 @@ static void seg_free(AVFormatContext *s) ff_format_io_close(seg->avf, &seg->list_pb); avformat_free_context(seg->avf); seg->avf = NULL; + av_freep(&seg->times); + av_freep(&seg->frames); } static int seg_init(AVFormatContext *s) @@ -991,8 +993,6 @@ fail: ff_format_io_close(s, &seg->list_pb); av_opt_free(seg); - av_freep(&seg->times); - av_freep(&seg->frames); av_freep(&seg->cur_entry.filename); cur = seg->segment_list_entries; From patchwork Mon Sep 7 02:49:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22143 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 85C5744870F for ; Mon, 7 Sep 2020 05:56:53 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 671576882E3; Mon, 7 Sep 2020 05:56:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3F949689247 for ; Mon, 7 Sep 2020 05:56:47 +0300 (EEST) Received: by mail-ej1-f66.google.com with SMTP id i26so16168690ejb.12 for ; Sun, 06 Sep 2020 19:56:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xUHLr2nrCmQGWXbtb7PcfmNpZXw7CBRUoN1EM1kvVzI=; b=i1NndSIacQzNDgulCbUsXdZMSDCsZjVJaZqNL4zORT0yCTossfDcFMijskTlhWQNXF i5BLxmVajia1+kpM6mXSiF7veRtjs1G7WjZRwHIg2q1oEgRxAilBFkwAcyEDri/Qo7D1 hRv+BZlwEXh3yMEJ602NFZouZFWxKWoJ0WRmPaGmQRLfVqmnBu5xLNBuJmfJYb5vsOb8 fi32tsehPgaEchLDqSySJgpvczUKLLKENIsghYogJPPigbk8S/++vfKA7+OJuKurI/hU gZFXp81gy2LQqb1FxGfkiEf6ktF02wxdoFoIHk/BR+3Nz2qA0fXGIiwx7LxcigzX/tE4 mtFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xUHLr2nrCmQGWXbtb7PcfmNpZXw7CBRUoN1EM1kvVzI=; b=qZFpAnlhpJ27ix0Ljf4etI6cwnv1XDGoBN7Gk6xpn2A1EcVZlIivPN2R1ORyR/BSmg qwHdKn+oqmrRU+p0d5nB3Ep3hWyXmpWJsUp+I8lMYSgJgSjLXOOQh2xYIaZlq3U0La+1 +5KNY+limH+/jIVFeJwHz3CjeRyCIXVjkAOnS9bhb0ImJv9qoJRnXehCmUB3S7RvVVQY +TjOGYj7J4FMV4hI7cgaXYlZeGHP/oPakkRaECdYorwlGcQ7fL/rg2kfJfObAVVtmP67 GMcBAWMGzmUhUs3ksJ8edYIxpEOkLoBHB8qC3MxwE3eAb/IfM1WvsbMYMZcM2XVus9MQ 56uw== X-Gm-Message-State: AOAM532J2h1VKBkEtNee+pE3jXt2ClRS117OWUduwOBN6Nq8umpJEmVn wQxS+7qHLOrxoJaQZQEoNGN9oeJ4yes= X-Google-Smtp-Source: ABdhPJx5XBQhYeKzUoKMwhNBHiR8aKmGdzvR/okMtb8s/+K0wrOWVbtD23rBAUqtzOB3zTfZ8ObXiw== X-Received: by 2002:a50:9b5e:: with SMTP id a30mr19386954edj.49.1599447067733; Sun, 06 Sep 2020 19:51:07 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:07 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:45 +0200 Message-Id: <20200907024952.11697-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/10] avformat/segment: Fix segfault on allocation error, avoid allocation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" If the user has set none of the options specifying the segments' durations, a default value of 2s is used by duplicating a "2" string and using av_parse_time() on it. Yet duplicating the string was unchecked and if the allocation failed, one would get a segfault in av_parse_time(). This commit solves this by avoiding duplicating the string altogether if no string has been provided; instead the duration is set explicitly to what av_parse_time() would return for the string "2". Signed-off-by: Andreas Rheinhardt --- One could btw make the segment_time an option of type AV_OPT_TYPE_DURATION, but then one could no longer distinguish the cases in which the value has been set to its default value by the user and the one in which the user hasn't set the option at all. Would this be a problem? Btw: Why are values <= 0 allowed for this? libavformat/segment.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 0c96c8c50c..9fafec0e35 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -704,9 +704,9 @@ static int seg_init(AVFormatContext *s) return ret; } else { /* set default value if not specified */ - if (!seg->time_str) - seg->time_str = av_strdup("2"); - if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) { + if (!seg->time_str) { + seg->time = 2000000; + } else if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) { av_log(s, AV_LOG_ERROR, "Invalid time duration specification '%s' for segment_time option\n", seg->time_str); From patchwork Mon Sep 7 02:49:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22136 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2F78A44BB3D for ; Mon, 7 Sep 2020 05:51:16 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 09FFA688392; Mon, 7 Sep 2020 05:51:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AFF9E688392 for ; Mon, 7 Sep 2020 05:51:09 +0300 (EEST) Received: by mail-ej1-f66.google.com with SMTP id q13so16151383ejo.9 for ; Sun, 06 Sep 2020 19:51:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=w/RdSFKRwSH26QJP1HfTyZE3RzHceZxrB+bIQWeaAjY=; b=Gqs7RueIQ/B+mmkJMm4NR/4HS0CCRFkxY0GASbUv3gVO5dvxJHFbjHRRH2yKKGmtOO sxdftPIyKHDoI8ac9sHa/VZRciM09gO/PF2mS9OVMU21D1Wj7lumFbnWhb0KEBYD73bl QWhcMhpj65NH3rynF9qA9LSN2ZRCqfjtGUKHY1Ks9Od3Cn+mkF/xNVk92KwdpQ9OIZmw AGRWtTNuJpsVK1tjZx9NWcrAKQeg4QMgOwn52xYSe6lOkEDSbmS8TBQ29VmswIdptX4h hzy9Mz9JxCgl8eViNTGGCaa7TtxKGQZeanM3F6ltbDAv/+3EMiy9SSnxiA0dw+piqQbe 8DqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=w/RdSFKRwSH26QJP1HfTyZE3RzHceZxrB+bIQWeaAjY=; b=Dn/olwbHdSgzlU19qwqUSg8gRGr5QQ4qkfFdYWcHQBYTXqen1yFrTuHTqlu+3tcdYq +0Kc04G5QNDF/VP4cK8fkK4G+QsI56mavC3vrfNXGiN8RYVFWto/fC9boWnuSxVbxhl3 wX/t1oH09MPtYo3VNb+YJIN5moHbaDEG0wH8HfjnDdJfgV7XG53LjVExJL2VKniJmmod cUPvldn80WWjy8V2cbkjjUth+dqda2I64pBXoz7BMrYGJkHH1YlNU1n4aRA8yqAXp6I0 poCxRNg1VSa+PAD9jj8mWuFIBarot1N7PcRvszeioONiNOLn+12TPV4bHZalDZgxZ+0w zz9A== X-Gm-Message-State: AOAM530Fxp63fvnMvsm1T2iytP0ERKyLet4S40LhfgZeF7gWaHmvICYw fX15dDdEg72kIUo4cEAJknvsr9JMVpk= X-Google-Smtp-Source: ABdhPJwVl5xfffaMPK1ORk/f8T2X5hRr8yb2O4Qsh5SxOaOn4VtLdrlCbzNEsetITiGM2wucW/EmTw== X-Received: by 2002:a17:906:5284:: with SMTP id c4mr18726423ejm.521.1599447068874; Sun, 06 Sep 2020 19:51:08 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:08 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:46 +0200 Message-Id: <20200907024952.11697-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/10] avformat/segment: Fix segfault when error happens and segment list is output X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The segment muxer has an option to output a file containing a list of the segments written. The AVIOContext used for writing this file is opened via the main AVFormatContext's io_open callback; seg_free() meanwhile unconditionally closes this AVIOContext by calling ff_format_io_close() with the child muxer (the one for the actual output format) as AVFormatContext. The problem hereby is that the child AVFormatContext need not exist, even when the AVIOContext does. This leads to a segfault in ff_format_io_close() when the child muxer's io_close callback is called. Situations in which the AVFormatContext can be NULL range from an invalid reference stream parameter to an unavailable/bogus/unsupported output format to inability to allocate the AVFormatContext. The solution is to simply close the AVIOContext with the AVFormatContext that was used to open it: The main AVFormatContext. Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 9fafec0e35..e30e47b62e 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -660,7 +660,7 @@ static int select_reference_stream(AVFormatContext *s) static void seg_free(AVFormatContext *s) { SegmentContext *seg = s->priv_data; - ff_format_io_close(seg->avf, &seg->list_pb); + ff_format_io_close(s, &seg->list_pb); avformat_free_context(seg->avf); seg->avf = NULL; av_freep(&seg->times); From patchwork Mon Sep 7 02:49:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22137 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 5E00A44BB3D for ; Mon, 7 Sep 2020 05:51:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4724F68A2D7; Mon, 7 Sep 2020 05:51:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 79AA46880C9 for ; Mon, 7 Sep 2020 05:51:10 +0300 (EEST) Received: by mail-ej1-f66.google.com with SMTP id o8so2995295ejb.10 for ; Sun, 06 Sep 2020 19:51:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gKbfofrP8P5V84X5W9Sm0hZEB0bQ4qAlHK4AhkTmp8c=; b=EHJuOx7L1hzBIkEfenfKTSeXNz02t8MTpfpcf+gkD9Q3Wr8JW7x6NSrPLFYEvkZsCD ZdyQad6SpSiv9Su+4G+4Wr+8lIIHyLKDF/Db4RZ0/2lHNVVA5HRa3zc+rtVFFV4wy1dR 4P/bdjwg0rSMfiPna6nzPNWyF+uIZbjibdutFc7eZ417wZjPI4yetw/NRhwWNDXmtjhw tWlOGCIVai7Buld3mFn9d4QU25Yixq9YFRMRYmfZYhPoKs0yhO2tNyVA90jFn8RPqCgh Zaaf5WdYzbBSePvPS04qWBsZkg0Y3fPl/PbaROfvyY/CEUalRJIFV3flHHsL23gLev7W Xx4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gKbfofrP8P5V84X5W9Sm0hZEB0bQ4qAlHK4AhkTmp8c=; b=BEAy+UE7PU/eUl3UqvKnXDQ8EomO/L/UfoUsEwUGsXNe23n0CVM8tBLZ4batLNuJSb 2b5oBu7E6+JoXF2jmNxZ4ncfoMiGBpI/IFUtS5lDOrkqo6cPUMYo1C8FiCiVRG8TH/UX bbnYA+MEPTmAIBwsj0lgKpxdxy2qmNmWnqht+BxxPccAgPEsrQt5kuX+/ZKUXSKxhAFJ 6aR0f4+z0eK/Y5o4tFlDT/ZANGTWta2v0owkmvoi2vXxg8c0pVMLE2rMhVidKXB3zIiA aYeVaiU26NH92GjvSV4golIHv6M/oWATQt0G3bID1UHJroeT4wSDzqgYm+M0mLv+ojLS DQmg== X-Gm-Message-State: AOAM53067yRiy/KP3eQQTUcS1Z6yisKTszZewxAfPaXl4TSRTQwt1M7o bZeJeevNeHNlmuKR0UTBg8KFK4rfwT4= X-Google-Smtp-Source: ABdhPJzuIKHn0E9fNsJQEBo+cOlOSB2wg3c2dTdLpQmMPHIo+t7H9UF9Zz256Vef7BKWpeHikSdoBw== X-Received: by 2002:a17:906:375a:: with SMTP id e26mr18654079ejc.552.1599447069688; Sun, 06 Sep 2020 19:51:09 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:09 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:47 +0200 Message-Id: <20200907024952.11697-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/10] avformat/segment: Fix leak of string on error X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" A string containing the segment's filename that the segment muxer allocates got only freed in its write_trailer function. This implies that it leaks if write_trailer is never called, e.g. if initializing the child muxer fails. This commit fixes this by freeing the string in the deinit function instead. Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index e30e47b62e..b8eb0289c4 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -665,6 +665,7 @@ static void seg_free(AVFormatContext *s) seg->avf = NULL; av_freep(&seg->times); av_freep(&seg->frames); + av_freep(&seg->cur_entry.filename); } static int seg_init(AVFormatContext *s) @@ -993,7 +994,6 @@ fail: ff_format_io_close(s, &seg->list_pb); av_opt_free(seg); - av_freep(&seg->cur_entry.filename); cur = seg->segment_list_entries; while (cur) { From patchwork Mon Sep 7 02:49:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22138 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9ABE044BB3D for ; Mon, 7 Sep 2020 05:51:18 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7B39A68A561; Mon, 7 Sep 2020 05:51:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 790C768A212 for ; Mon, 7 Sep 2020 05:51:11 +0300 (EEST) Received: by mail-ej1-f66.google.com with SMTP id nw23so16188349ejb.4 for ; Sun, 06 Sep 2020 19:51:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K9t+LsdzkQ2VNUSwoiGEWYtZhporhG3WcwPpp6XTMmo=; b=dekW+KkHr5TnPakavS0g4g4uE8rcQ4FoIsRjn5oBf7FTB6mYU8l+IgQpbchTl0UBSn lrMNvQWbfFMYgHPbsPS05jmC4+O1If2T6Hc7yfHHeIrpeYCsf5Kjmy591QBWVTxmagkr PEb+X2qPr/8NXDlewtPoMh6Zh04hEGfOk7wMMiiozW6snjnnoZZlxqPp3cudW20c9gyi lRxJpi7i12/OHxJJLEmnEpijnfxH0Hkc9RKoWrT8PzGqW+VQ9wFQp6xKOiHc+v6ndWVD rPkfqVjZlJJWU/17UkrVBnOSNT02RiW0UuIKolqIzS02EDsDhR5ykGbDBr8wxxU9SSCc M7Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K9t+LsdzkQ2VNUSwoiGEWYtZhporhG3WcwPpp6XTMmo=; b=Voic5ePozbdIOkEvJR61lAh7aSUDLERuUitqGrHdkV+/f9vaNA4eoZJkSVIbhwSfol 1b2SFdqBgDQYTs6sCdSxOiL8IQoSH2fHxt9kUiaHHTTB5oKZoJEPFKCogOMB+OmMWHCM NCKZj5qs+kxCpd5ylJbG8xptxNznBU4aERBnHoKWj87mi+gfeZD6GgZ8tEyGk3BIBmgS jFQqJrdWXSYh0nh3znFP4XjcRyWyu/K7jTnHn1MfDJo04AT9Dbx5KBSf3/HvCpbdyPLP WTzl21czfjidkQ2u0ccgA8R3D9ErOXDo4wtWYUGzfkuxOgwb07fGjOY81MGJwWgrBGb4 A2Yg== X-Gm-Message-State: AOAM530xiXxnszT1ceSx9aAUmNoJffVsxX+H1kkQWHAkUxHye+qxsL74 BWzammU60OOzmCgb2YkA6Pc1V7LaWtM= X-Google-Smtp-Source: ABdhPJyHP2/29sY4kS8ev2q2UHqiQfYlP5UDRc7BWpsSnaMctPYcOqNigD/R3FfJgZ4sEFIhr46uWg== X-Received: by 2002:a17:906:2552:: with SMTP id j18mr18551724ejb.476.1599447070662; Sun, 06 Sep 2020 19:51:10 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:10 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:48 +0200 Message-Id: <20200907024952.11697-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/10] avformat/segment: Fix leak and invalid free of AVIOContext X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" seg_init() and seg_write_header() currently contain a few error paths in which an already opened AVIOContext for the child muxer leaks (namely if there are unrecognized options for the child muxer or if writing the header of the child muxer fails); the reason for this is that this AVIOContext is not closed in the deinit function. If all goes well, it is closed when writing the trailer. From this it also follows that the AVIOContext also leaks when the trailer is never written, even when writing the header succeeds. But simply freeing said AVIOContext in the deinit function is complicated by the fact that the AVIOContext may or may not have been opened via the io_open callback: If options are set to discard header and trailer, said AVIOContext can also be a null context which must not be closed via the io_close callback. This may lead to crashes, as io_close may presume the AVIOContext's opaque to be set. It currently works with the default io_close callback which simply calls avio_close(), because avio_close() doesn't care about opaque being NULL since commit 6e8e8431e15a58aa44cfdd8c11f9ea096837c0fa. Therefore this commit records which of the two kinds of AVIOContext is currently in use to use the right way to close it. Finally there was one instance (namely if initializing the child muxer fails with no unrecognized options) where the AVIOContext was always closed via the io_close callback. The above remark applies to this; it has been fixed, too. Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index b8eb0289c4..55d7f62ca0 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -80,6 +80,7 @@ typedef struct SegmentContext { int list_flags; ///< flags affecting list generation int list_size; ///< number of entries for the segment list file + int is_nullctx; ///< whether avf->pb is a nullctx int use_clocktime; ///< flag to cut segments at regular clock time int64_t clocktime_offset; //< clock offset for cutting the segments at regular clock time int64_t clocktime_wrap_duration; //< wrapping duration considered for starting a new segment @@ -661,8 +662,14 @@ static void seg_free(AVFormatContext *s) { SegmentContext *seg = s->priv_data; ff_format_io_close(s, &seg->list_pb); - avformat_free_context(seg->avf); - seg->avf = NULL; + if (seg->avf) { + if (seg->is_nullctx) + close_null_ctxp(&seg->avf->pb); + else + ff_format_io_close(s, &seg->avf->pb); + avformat_free_context(seg->avf); + seg->avf = NULL; + } av_freep(&seg->times); av_freep(&seg->frames); av_freep(&seg->cur_entry.filename); @@ -777,6 +784,7 @@ static int seg_init(AVFormatContext *s) } else { if ((ret = open_null_ctx(&oc->pb)) < 0) return ret; + seg->is_nullctx = 1; } av_dict_copy(&options, seg->format_options, 0); @@ -791,7 +799,6 @@ static int seg_init(AVFormatContext *s) av_dict_free(&options); if (ret < 0) { - ff_format_io_close(oc, &oc->pb); return ret; } seg->segment_frame_count = 0; @@ -834,6 +841,7 @@ static int seg_write_header(AVFormatContext *s) ff_format_io_close(oc, &oc->pb); } else { close_null_ctxp(&oc->pb); + seg->is_nullctx = 0; } if ((ret = oc->io_open(oc, &oc->pb, oc->url, AVIO_FLAG_WRITE, NULL)) < 0) return ret; @@ -984,6 +992,7 @@ static int seg_write_trailer(struct AVFormatContext *s) goto fail; if ((ret = open_null_ctx(&oc->pb)) < 0) goto fail; + seg->is_nullctx = 1; ret = av_write_trailer(oc); close_null_ctxp(&oc->pb); } else { From patchwork Mon Sep 7 02:49:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22145 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 7208844B826 for ; Mon, 7 Sep 2020 06:14:01 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 491F96881FE; Mon, 7 Sep 2020 06:14:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f68.google.com (mail-ed1-f68.google.com [209.85.208.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AE9DD68819B for ; Mon, 7 Sep 2020 06:13:54 +0300 (EEST) Received: by mail-ed1-f68.google.com with SMTP id i1so11176531edv.2 for ; Sun, 06 Sep 2020 20:13:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uvBC3dTgrK8b9mrCCOFbIvRLje8PCEQuXnjHCPkzFo8=; b=AxT9hr0A0Py9IKtg5u24iDl3AJqZVpSxeJtwEheK/LeDQtJogt5hbtUS3zJ8isumkR SEa+zkdDj1n+rkvc6Ub3HW+J2XwTnXAEP/gWD7S5nIgB/Er+WG7GVCtn7ORkAuysXEYo PstA6PoWcUYJl3Uz3myoAlXyUEudCCJl72PSjhAum3fJaaaRwXxSyaMPQFjgZ9oLEtDX BcBpdbVBXU8EQ5jODhnS3eQ96u2qqjFHIwmvojQ2mZuf+S92Thh06/IE0EVSOiPPzJlA /WOur90D00Ue7rzReIadmA++mysLXIxR4BWmotzE6jloRjsM5J3ppHllO0xY2DSwpQs2 jPXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uvBC3dTgrK8b9mrCCOFbIvRLje8PCEQuXnjHCPkzFo8=; b=SFTUqDuAVGyPE67rnoLS2VlsytCEmaJDFW8w/mkOeumHq/llJ8qU2/RXVBGF1Rr0eh A/85HBtmR4Py67Bi3wKygncW3iM3HK1c5YbBNk/XzCahiDLREK9nCnbfA4mESWgzGQTc BlgXHjz7PqSza2Y30KJeMRRkVvok5Xm0yat/Wt7/FaGUZ6oyRxMdRKmUUmd2uZ1vdvKY DyR/F0aZMp0tTcalJvb0AfQ0MtwvuydfSC2Au65bcBr4sgmnCJU3oEJuSSPAwALRPME3 3HMDF7vvbqIH+J+aLgzaDqCJv39XsVm6Iz0+viWIT6z1A/nVvWSz67Lo1ZAOkj2BJZTS fc+Q== X-Gm-Message-State: AOAM531qdIPUNdEl+V3HTDHvgesjN4q01Wal6Q3cHlshboP7H8Isz5y9 /Wpqg2JRJ1avl52mQJRAgHNL6QqeAWM= X-Google-Smtp-Source: ABdhPJyuUiF7djMHipmdQh6MdqzzatDwfeog6qkuuRkXDAcuUrJNZuFbMxnonFlO3Xxwn3nVZpOClg== X-Received: by 2002:a05:6402:1851:: with SMTP id v17mr2899990edy.46.1599447071515; Sun, 06 Sep 2020 19:51:11 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:11 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:49 +0200 Message-Id: <20200907024952.11697-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/10] avformat/segment: Free SegmentListEntries in deinit, not write_trailer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This fixes leaks when the trailer is never written. Signed-off-by: Andreas Rheinhardt --- The allocation of the filename is btw currently unchecked. Actually, using a flexible array member for it would be advantageous (it could then be allocated and freed together with its SegmentListEntry). Flexible array members are C99. Are they allowed or do we support systems that don't support them? libavformat/segment.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 55d7f62ca0..d895e6a678 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -661,6 +661,8 @@ static int select_reference_stream(AVFormatContext *s) static void seg_free(AVFormatContext *s) { SegmentContext *seg = s->priv_data; + SegmentListEntry *cur; + ff_format_io_close(s, &seg->list_pb); if (seg->avf) { if (seg->is_nullctx) @@ -673,6 +675,14 @@ static void seg_free(AVFormatContext *s) av_freep(&seg->times); av_freep(&seg->frames); av_freep(&seg->cur_entry.filename); + + cur = seg->segment_list_entries; + while (cur) { + SegmentListEntry *next = cur->next; + av_freep(&cur->filename); + av_free(cur); + cur = next; + } } static int seg_init(AVFormatContext *s) @@ -981,7 +991,6 @@ static int seg_write_trailer(struct AVFormatContext *s) { SegmentContext *seg = s->priv_data; AVFormatContext *oc = seg->avf; - SegmentListEntry *cur, *next; int ret = 0; if (!oc) @@ -1004,14 +1013,6 @@ fail: av_opt_free(seg); - cur = seg->segment_list_entries; - while (cur) { - next = cur->next; - av_freep(&cur->filename); - av_free(cur); - cur = next; - } - avformat_free_context(oc); seg->avf = NULL; return ret; From patchwork Mon Sep 7 02:49:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22139 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9EEB744BB3D for ; Mon, 7 Sep 2020 05:51:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 892E768A687; Mon, 7 Sep 2020 05:51:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2B6EB689F10 for ; Mon, 7 Sep 2020 05:51:13 +0300 (EEST) Received: by mail-ej1-f66.google.com with SMTP id i26so16158657ejb.12 for ; Sun, 06 Sep 2020 19:51:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ue3jms1eJJoPbgbUG3Ad0hiCicJhXE0ocV5BJlgf3xM=; b=PNgW6UGg8lZS6hNJtc63sx/y5txq0cIMzOhwri+UzaguawCWRdmGhQKRA7I0GoTm0l Qct8nWfFSik/HtE1uI8r9L4YEdECbjbZv5mcYlY5NcHPset+gmTv9RMuhUUFhZxMkDat yrXKMZbgumxBzEdRgonDeqVkL3PLBxlP2GMvUFq/ZEdG65ixleTN9BstfMv013sZfSws omUIFUu10sq6pJmv+yLpgtg0SDWeWeVez0Nwd1S2DE6yoD3rOz/giPWCLL7sqi/xHhkf 6IGzf3SZSziBuQVmOqJ/zYLprSdrUb0tZYdLdWXpIMcCSwjf2gfTz3T48e9r3fA02opP /cnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ue3jms1eJJoPbgbUG3Ad0hiCicJhXE0ocV5BJlgf3xM=; b=ekg/i0dw4EqOvgOrBCS97Ow1U03h3praxK1l4IjhS+ObqzG3toE1lCSe7sogezjHQz w2fLI9Q+p3qrf+5GuvZOXNhqFw2nhOJgaXmauOHy4GtvX2wbybyLegvMB/m0EKJZJXoW fGusnsWR9lJEEckNgXXjzfGnqafAeCYUY8Elu+awITAmaDMhag8Gh6x0r6x+L/G4XIuU atEcfnR9p2esl9WgnNiKuX+eScaxYbhB/V9H8umRx2mtw63FGcbWZH4oyf1kMxQmSpSu 80gQR0GwgNd5DVvJyH8PTXb/joFvRuDY4h+RvRpgQA0iN+RQ59PxBhKX9yFAPwK2eb1Q uSyg== X-Gm-Message-State: AOAM530G6pV+ksOzDu5F3e9r+yp9d3hsIs/R6QYBLCV2tRpCAacGh74d RnsO8xBevpj32KMRqnRRRnWKz5I1wHY= X-Google-Smtp-Source: ABdhPJwQb6OtNGobBYhEm1boGTsNa4BbpnXv3fNV9Ky9Xr9xY2C3qv448e3e/o7fLHtWFnQT2ws2TQ== X-Received: by 2002:a17:906:b317:: with SMTP id n23mr18585140ejz.6.1599447072371; Sun, 06 Sep 2020 19:51:12 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:11 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:50 +0200 Message-Id: <20200907024952.11697-8-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/10] avformat/segment: Remove redundant code for freeing in write_trailer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This code mostly duplicates code in the deinit function; the only exception is av_opt_free(): The options are freed generically lateron. Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index d895e6a678..0253b750b8 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -991,30 +991,21 @@ static int seg_write_trailer(struct AVFormatContext *s) { SegmentContext *seg = s->priv_data; AVFormatContext *oc = seg->avf; - int ret = 0; + int ret; if (!oc) - goto fail; + return 0; if (!seg->write_header_trailer) { if ((ret = segment_end(s, 0, 1)) < 0) - goto fail; + return ret; if ((ret = open_null_ctx(&oc->pb)) < 0) - goto fail; + return ret; seg->is_nullctx = 1; ret = av_write_trailer(oc); - close_null_ctxp(&oc->pb); } else { ret = segment_end(s, 1, 1); } -fail: - if (seg->list) - ff_format_io_close(s, &seg->list_pb); - - av_opt_free(seg); - - avformat_free_context(oc); - seg->avf = NULL; return ret; } From patchwork Mon Sep 7 02:49:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22140 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6775044BB3D for ; Mon, 7 Sep 2020 05:51:21 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 528E068A9AA; Mon, 7 Sep 2020 05:51:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f67.google.com (mail-ej1-f67.google.com [209.85.218.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2F7E768A545 for ; Mon, 7 Sep 2020 05:51:14 +0300 (EEST) Received: by mail-ej1-f67.google.com with SMTP id i26so16158689ejb.12 for ; Sun, 06 Sep 2020 19:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=247A9ZGsHgl1ba+68Te6D54URwQGzu0xvzRBysxJK/Y=; b=j6HS8gVq0U4MF/ktTTfmpcCNjp+jZjqOri7y3Q35u2s8Ww4nMsZgrFtrPRdygzDPgV jROMpmo6BH7qEMSyKB24xm/8PY5hCP+AN/vlYTGpid6C5en0D1xiqTrzk8eNezY2CZPJ Dl1etUx4qx9v4LInZoZlwuZSpeBXqX49Hpk39/L/Tet+44B3bVMTcprYwMkewCapr15D a6ckov2modTF2muAOpxLLQpMyZpaEBztOXAIdn9hNvfAIA4D0CRljFZSh3I6yoXFBhAi 7fwGCUSLnEDvf0pchFo7UrJ9oQFkx8mhwQ9R74Vbwe+ZqNKPs3zs1zIcPEWTnjtQs0ku 3cPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=247A9ZGsHgl1ba+68Te6D54URwQGzu0xvzRBysxJK/Y=; b=BX+YCMNGuF4urXvoXSTjxH4EQuCPZS294uXN3xRy3lPNbmsiXTpDDDc8zw7EJ7KOpd LnoqyDMvgjjMHSpPh/k50ovgV/6aTFc8+04/0xwmW3+v3iiTcgJUrrhMeyQYKTStHtY0 dR1uxY+BwPAp5u8lWJyv8Uml2SKNsRwgRKAizIg6yzRpPI3diOW4Bx1DdGYZ3lxHnPxa FtjxeaCbblFPEDGssv5LD+CvWYUgRm9U0g5t+mo/4vdnxiRjJmYX5/HAu/EIDeWuWkzV fdQhUpQ+5BZ9aXcnh4GzWsx1QdnhDAkQA3nO/xCvn5WP4jblK8eF/royJqldFinoW3vC 8m/A== X-Gm-Message-State: AOAM5312QfXpc4OyqIl89V5d3JdUolrzvxQCxmiYiItPFugdj5Uoybwj NOJSutqHUNt4cjpw2b2jj44fRh46OHY= X-Google-Smtp-Source: ABdhPJxrwGSQLx167jfSHbNFvUjmuu3V3/zwf+H8NWknsLowdfwH765tX79cwveHe0epT7SVvKLpIg== X-Received: by 2002:a17:906:4c81:: with SMTP id q1mr18383910eju.72.1599447073394; Sun, 06 Sep 2020 19:51:13 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:12 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:51 +0200 Message-Id: <20200907024952.11697-9-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/10] avformat/segment: Fix error messages X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Andreas Rheinhardt --- libavformat/segment.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 0253b750b8..3dcbda6957 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -511,7 +511,7 @@ static int parse_times(void *log_ctx, int64_t **times, int *nb_times, /* check on monotonicity */ if (i && (*times)[i-1] > (*times)[i]) { av_log(log_ctx, AV_LOG_ERROR, - "Specified time %f is greater than the following time %f\n", + "Specified time %f is smaller than the last time %f\n", (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000); FAIL(AVERROR(EINVAL)); } @@ -561,7 +561,7 @@ static int parse_frames(void *log_ctx, int **frames, int *nb_frames, f = strtol(fstr, &tailptr, 10); if (*tailptr || f <= 0 || f >= INT_MAX) { av_log(log_ctx, AV_LOG_ERROR, - "Invalid argument '%s', must be a positive integer <= INT64_MAX\n", + "Invalid argument '%s', must be a positive integer < INT_MAX\n", fstr); FAIL(AVERROR(EINVAL)); } @@ -570,7 +570,7 @@ static int parse_frames(void *log_ctx, int **frames, int *nb_frames, /* check on monotonicity */ if (i && (*frames)[i-1] > (*frames)[i]) { av_log(log_ctx, AV_LOG_ERROR, - "Specified frame %d is greater than the following frame %d\n", + "Specified frame %d is smaller than the last frame %d\n", (*frames)[i], (*frames)[i-1]); FAIL(AVERROR(EINVAL)); } From patchwork Mon Sep 7 02:49:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22141 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 89F9C44BB3D for ; Mon, 7 Sep 2020 05:51:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 72F7F68A91A; Mon, 7 Sep 2020 05:51:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f65.google.com (mail-ej1-f65.google.com [209.85.218.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 173BC68A545 for ; Mon, 7 Sep 2020 05:51:15 +0300 (EEST) Received: by mail-ej1-f65.google.com with SMTP id e23so16213133eja.3 for ; Sun, 06 Sep 2020 19:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rp9p7ja2OugZ9zgOJhfowG0Pn2KQlu+WFPyXNoQD/pc=; b=AY35iDDV6Y9jW4Z3IufmNzfldEk8iuGFObSfL+y+D5FSWWw4XusgBI3XdpPhHPMwtT u8MMK2rW8QBVKAa8B7nVRFWVWiSFKSNlDUXJ45Rk5MKem4YtywjmcC8fdcPCgeJPo9Dq uizXQqvyDk4BEZlMvzQ6PRBXJOdSdAT5yawVwXYSphXgSF8nLrbkFaaCl/f7Wjytmx52 tx0yfQQR+FIA5QktR2yRYVRI7FdSr2TUGOEUGcdX/FD4L5LdDYwt7xVNVNnqY9FM+l3n yoh6GWhscsVPHVZiRwxtcMGmYY+VzRvEiAcxpY4raYgPTIo664sQ8TSy6MElXQ56h5K6 6Qrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rp9p7ja2OugZ9zgOJhfowG0Pn2KQlu+WFPyXNoQD/pc=; b=stpIL/RsZa+wN04XMxCeZbQpb/IOG/pt8PRd9qZaGAEE75T/4PULJ30aRtU8slIqer Edgy1DcRQjfRpgtJYL4AMw+Jy1BtJdx/0l4DLzWE68exXrt+Xh3BSyfL3I8chQTWyiit MkIFjXUI9pXJ9aK3LSx9yOSUxr0Ot61vxt9M0h9eP4PLGnwFFuDtu8PGH21VkADhYbUl KaYKRfJ/4LEMizh0gqaUnk/uE2pBunb9Vp6QpEOHH0vbeAJpYbi2ZwZNeWL6SYQSopW4 EtL5gwopP2Gpw/3jRm2tt0q48FRqGj2UhRcmHVYnr9Ckv/1m1d478uNZyowzsDeAb/Zt wTMw== X-Gm-Message-State: AOAM530HPty1/5Xl0gKrtVaNXwNk8xUVmYP6NIfqhfCPaekHT7uqZkvZ PGTG2VeWMruG2em1CRm8JHDvTEKZcEQ= X-Google-Smtp-Source: ABdhPJx9UST/0R1IbhUteZs2ueJAaUd6gsPbsfohGawBn2Mse5kVRIeASAnF+jZ6eRsx8zj3Ew/+BQ== X-Received: by 2002:a17:906:c34d:: with SMTP id ci13mr18602633ejb.356.1599447074188; Sun, 06 Sep 2020 19:51:14 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm13802048eje.16.2020.09.06.19.51.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2020 19:51:13 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Sep 2020 04:49:52 +0200 Message-Id: <20200907024952.11697-10-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> References: <20200907024952.11697-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/10] avformat/segment: Avoid duplicating string when parsing frames list X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Andreas Rheinhardt --- The first check in the loop could be removed btw; its only advantage is a better error message. libavformat/segment.c | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 3dcbda6957..38fca253a2 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -525,46 +525,38 @@ end: static int parse_frames(void *log_ctx, int **frames, int *nb_frames, const char *frames_str) { - char *p; - int i, ret = 0; - char *frames_str1 = av_strdup(frames_str); - char *saveptr = NULL; - - if (!frames_str1) - return AVERROR(ENOMEM); - -#define FAIL(err) ret = err; goto end + const char *p; + int i; *nb_frames = 1; - for (p = frames_str1; *p; p++) + for (p = frames_str; *p; p++) if (*p == ',') (*nb_frames)++; *frames = av_malloc_array(*nb_frames, sizeof(**frames)); if (!*frames) { av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced frames array\n"); - FAIL(AVERROR(ENOMEM)); + return AVERROR(ENOMEM); } - p = frames_str1; + p = frames_str; for (i = 0; i < *nb_frames; i++) { long int f; char *tailptr; - char *fstr = av_strtok(p, ",", &saveptr); - p = NULL; - if (!fstr) { + if (*p == '\0' || *p == ',') { av_log(log_ctx, AV_LOG_ERROR, "Empty frame specification in frame list %s\n", frames_str); - FAIL(AVERROR(EINVAL)); + return AVERROR(EINVAL); } - f = strtol(fstr, &tailptr, 10); - if (*tailptr || f <= 0 || f >= INT_MAX) { + f = strtol(p, &tailptr, 10); + if (*tailptr != '\0' && *tailptr != ',' || f <= 0 || f >= INT_MAX) { av_log(log_ctx, AV_LOG_ERROR, "Invalid argument '%s', must be a positive integer < INT_MAX\n", - fstr); - FAIL(AVERROR(EINVAL)); + p); + return AVERROR(EINVAL); } + p = tailptr + 1; /* Might point one past '\0' */ (*frames)[i] = f; /* check on monotonicity */ @@ -572,13 +564,11 @@ static int parse_frames(void *log_ctx, int **frames, int *nb_frames, av_log(log_ctx, AV_LOG_ERROR, "Specified frame %d is smaller than the last frame %d\n", (*frames)[i], (*frames)[i-1]); - FAIL(AVERROR(EINVAL)); + return AVERROR(EINVAL); } } -end: - av_free(frames_str1); - return ret; + return 0; } static int open_null_ctx(AVIOContext **ctx)