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)