From patchwork Mon May 25 14:08:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19858 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 D882E44AA13 for ; Mon, 25 May 2020 17:34:19 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B12A468980D; Mon, 25 May 2020 17:34:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f65.google.com (mail-ed1-f65.google.com [209.85.208.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 97D64680891 for ; Mon, 25 May 2020 17:34:13 +0300 (EEST) Received: by mail-ed1-f65.google.com with SMTP id s19so15151184edt.12 for ; Mon, 25 May 2020 07:34: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=PuRFTT2N5nPQHM8FhlrTAwBmGbwZ81x2axi80Lh217c=; b=qLV8jFel4x/08JOOgr8FtNUnjOrJFX2J0jHuX3lbUUBycdbLeR623ZALjegDD9FqU3 S13azGKx3+Q3htGI/mtyKooI7t5lFJuJnfxc4rvhjOL2nXhZx9iN8SJwV6FJx52z/qQK PvQhkKfbYG6mYmbXbahmhGbPJooyKYQnPJoWj5tLupy7OLGi4p+dAog7pc9pYberHItR FjsAfEl1JcT9xK/NriFCdKom2yCZR0rsgsJfUCxGgG+rDw7yn/VJA5fjKM7FuSYw8RIU 7o3+FTJg9QUeMUQGfbGGYzTIzFucvyA8fawZcvMdrYqhtPPq+uCaTpHMRds57ceA6Qk3 g9uw== 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=PuRFTT2N5nPQHM8FhlrTAwBmGbwZ81x2axi80Lh217c=; b=dpIdd36TboufXHWqJqyFMop7zDSd6Ay9VmppFUwcVho/KiZzgn+GrDKDIJ9YX5AP9S PCVXS5fp/wzr83S4QaNfsqR+j/AoFuVs4VBq6ex2d5+xZSpKb6wPFlzym33QyI3yAHJX 94UKUcbV8NKdkAqzdlnwkLKNMuh1YoNig+r31nuhnjkWysq/6vTwTplC7xGVnPfxblae 55oO+YoIeEcTlIi8qtp+KRzNEK1jGPFy18xb0Y0HSXl4mmn39cNS3m8b4zW5LXumTWAK RTWm/NXa+HzAdo0cdIIGYMWoWtYQGH/KK7m+aD1TMGX4/ZWlGsUMYUHm/7huNCS8mGDC iXmA== X-Gm-Message-State: AOAM5324uzw2Goo8jjwKncshnYrp5Ed12wcwrJy1MXX2e7d9cwmQv12s 9mZFASjzjRB21nbcFUMi+i15FVX8 X-Google-Smtp-Source: ABdhPJyOMjl2NbLrCMC9Frh76iOIWSwXZG8uH+8tVXjVgHV9KPKbjqAoy1n+Wlp3pDB912AvlIDagQ== X-Received: by 2002:adf:dc0f:: with SMTP id t15mr15143927wri.165.1590415716137; Mon, 25 May 2020 07:08:36 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id x186sm10355218wmg.8.2020.05.25.07.08.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 07:08:35 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 16:08:01 +0200 Message-Id: <20200525140801.18889-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200525140801.18889-1-andreas.rheinhardt@gmail.com> References: <20200525140801.18889-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5] avformat/aviobuf: Also return truncated buffer in avio_get_dyn_buf() 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" Two kinds of errors can happen when working with dynamic buffers: (Re)allocation errors or truncation errors (one has to truncate the buffer to a size of INT_MAX because avio_close_dyn_buf() and avio_get_dyn_buf() both return an int). Right now, avio_get_dyn_buf() returns an empty buffer in either case. But given that avio_get_dyn_buf() does not destroy the dynamic buffer, one can return the buffer in case of truncation and let the user check the error flags and decide for himself instead of hardcoding a single way to proceed in case of truncation. (This actually restores the behaviour from before commit 163bb9ac0af495a5cb95441bdb5c02170440d28c.) Signed-off-by: Andreas Rheinhardt --- Unfortunately the "let the user decide" approach is not possible for avio_close_dyn_buf(). Said function has many more deficits: On allocation failure it returns a size of -AV_INPUT_BUFFER_PADDING_SIZE, although the documentation does not even allow negative return values at all. On truncation, the padding is not really written, yet it is subtracted from the size (or to put it another way: the padding is not zeroed in this case). It does not return the actual size of the buffer, but rather the last position (which can be different in case a backward seek had been performed). In this case, it is not the end of the buffer that is padded, but rather the position when calling avio_close_dyn_buf(). There is no documented way to check for errors; checking whether the returned buffer is NULL works in case of allocation failures, but not for truncation. If we returned NULL on truncation, too, callers could check for errors. But a real solution would involve deprecating avio_close_dyn_buf() and maybe the rest of the current dynamic buffer API. libavformat/aviobuf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index a13c677875..66cc8c7adc 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1370,13 +1370,13 @@ int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer) { DynBuffer *d; - if (!s || s->error) { + if (!s) { *pbuffer = NULL; return 0; } d = s->opaque; - if (!d->size) { + if (!s->error && !d->size) { *pbuffer = d->io_buffer; return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer; }