From patchwork Mon May 25 14:07:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19854 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 79B2A44A859 for ; Mon, 25 May 2020 17:08:18 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5A65E68A0D1; Mon, 25 May 2020 17:08:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 143D8687F64 for ; Mon, 25 May 2020 17:08:12 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id r9so132331wmh.2 for ; Mon, 25 May 2020 07:08: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:mime-version :content-transfer-encoding; bh=E63Zgff57PI0e5sEDr6ql787MQgetNc5IjJOB+5dhIo=; b=E2/jjZ9W52ADVhRBFJZDLNK7z7FQqVvtjhFviwb7EQb8OGTumOFh281eYXC5DueGqn 2aMO6FwnrhzoOauQd3EUTihxmsFvaDbgADgwlW6MDqptrLjoLwJcaH+o2GFmJc3X77zN VK8istCJW6yq56SWbYktNhANER/UI5GmIkxD03XkFIYfXhLz5gBIRoGbqVpCqG7mf4o9 VNw1Ot6fjTRijdoBe1KaYu3QjkTyRQaVt69hAulm9XmNA1tbT+sdT4WheedZlWw2Mfpq VvGKKU6uFD0K96J/wNHwPhTGW//BJMVpcG01oV8m6kwUQsW1QSmQJsXjtUJvMm6sm6qM K7ig== 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=E63Zgff57PI0e5sEDr6ql787MQgetNc5IjJOB+5dhIo=; b=jgQldah9J3LbEwvphNXWQm/3eZBm7I7aD+lg5PNsR4UFsTDnRYt7Cgu896l1ULq+EZ swoabjttI8/9I+3jnR4bAI5IYMXfvO3n6aO9/on4lY9yekDJS1Ar7sCRTTlco2tWXSo6 dDVQW0m+J8ejlJPfflj4+e8xUINkLHPWdFPeGbH1mTJmtu586eBymV/28BaQWRFxxthe 5DbQIw1W3+E0aNxHSXkPNnY2JC9qHOWpUynNr4VL8IBSSoTe2eBJyqUOS6+eFtichpZ6 UaH2Qc51l5zcWSOUZ/pq/wggwJHf5PDQ8o0sWVVDgiLmfip8u9BkeF8bcFHkWdxgSRQT XFAQ== X-Gm-Message-State: AOAM5337zOjmaCdiscSXHegXxmbZEsGr+i5iQ2LIagqu39Ui1lyLZWqg 9oPheShjW4uvErl+BiWScvfsU/6z X-Google-Smtp-Source: ABdhPJxShixMCVPHIo+DxSZCq8CP+xS4BluTzYb7zgs9tpWjLDNxL+Ude+WGtPSaS45GJAC5zIRaIw== X-Received: by 2002:a05:600c:2dd7:: with SMTP id e23mr24715542wmh.96.1590415691143; Mon, 25 May 2020 07:08:11 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 07:08:10 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 16:07:57 +0200 Message-Id: <20200525140801.18889-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/5] avformat/aviobuf: Don't check for overflow after it happened 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 adding two ints overflows, it doesn't matter whether the result will be stored in an unsigned or not; and checking afterwards does not make it retroactively defined. Signed-off-by: Andreas Rheinhardt --- libavformat/aviobuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index eb0387bdf7..33c2d6f037 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1275,7 +1275,7 @@ static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) unsigned new_size, new_allocated_size; /* reallocate buffer if needed */ - new_size = d->pos + buf_size; + new_size = (unsigned)d->pos + buf_size; new_allocated_size = d->allocated_size; if (new_size < d->pos || new_size > INT_MAX/2) return -1; From patchwork Mon May 25 14:07:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19855 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 99DBD44A859 for ; Mon, 25 May 2020 17:08:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7CDAD68A471; Mon, 25 May 2020 17:08:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A6E89680C0D for ; Mon, 25 May 2020 17:08:33 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id g14so9312107wme.1 for ; Mon, 25 May 2020 07:08:33 -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=tdIcVmghw9hSAL3Mt3Bdcpnd3/T2J6bxlrVF6k7YE2Q=; b=MGvOS6afReuF59wjE5MzEWKWCYpH5Jc8NNaC1MAEI5P4adHNueHWFeXMuq6FH+NRZV Y+k6jzKws6DN4p85mLYTB6HF5WQ48j8Nrx907iybYPLu1RjRTto7m6YCzj2TxnmdYTUV zALbOp+MOQ3Msqjh1ogNENggm+3JQH6nAS2z9CyNWijjKLoPHczLombJpaMrfWJm+vGO OYsnMniHhdF50w/vW4Sm2l3Nf7PJ1mrG2d8upj2Rp+4jn73Xmr1e3p5TQ/rGg4z5U70+ KiGn9kcvMj52ikKN3OoLoR/b9CjjKGxRbgB3OoijttezO02dM+q9q/MCWt9si1IaqvYs F7bQ== 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=tdIcVmghw9hSAL3Mt3Bdcpnd3/T2J6bxlrVF6k7YE2Q=; b=QQ67i+ukt2+ajN54ME+7V9n7+FRqjrMKCoDmUfv//OiIzTeA/UQYQunzLSrXs2sD2m Y7WW0MArw6PZMW5yOCFvaDpUFVmuliE7loJfehteiQA8yz/c9mVeD+snaiWECRX3zlRs qBys7SfBeHgfvF90aAKxToGmcaaHzb6OdbGkG0vKeKtg4BFQFUCuvUvLvpIKIn7RsX5f /nRKHgQ1wPJT6thUDVp8nR27vDIXghSDBxT51hHZ76BAb3S6Us75+p2MRbJjwQedoTrM 1uxwvk8dIcKRgHJNo9710Fk7LtcPq5mo4VUGlEsNo4PTgHNiKZZiYX5cxkVdZhzAFiiJ V31w== X-Gm-Message-State: AOAM531nnsAGegNur19UNaWF0mnPisnoEI8VYiYs++JzsqHOjQ96MMFw unKj+boDWneuqhfsXO8k7jy6tVwp X-Google-Smtp-Source: ABdhPJyEoOqwTg6KgKJjCOfqvwb8L2EUpxiyw4W4yXDH/O2vbOy0SGhoDuqD3DPaKPgK6ln6cFrCXA== X-Received: by 2002:a1c:7712:: with SMTP id t18mr24091827wmi.182.1590415712850; Mon, 25 May 2020 07:08:32 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 07:08:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 16:07:58 +0200 Message-Id: <20200525140801.18889-2-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 2/5] avformat/aviobuf: Simplify dyn_buf_write() a bit 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/aviobuf.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 33c2d6f037..fcde7e8510 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1272,22 +1272,19 @@ typedef struct DynBuffer { static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) { DynBuffer *d = opaque; - unsigned new_size, new_allocated_size; + unsigned new_size; /* reallocate buffer if needed */ new_size = (unsigned)d->pos + buf_size; - new_allocated_size = d->allocated_size; if (new_size < d->pos || new_size > INT_MAX/2) return -1; - while (new_size > new_allocated_size) { - if (!new_allocated_size) - new_allocated_size = new_size; - else + if (new_size > d->allocated_size) { + unsigned new_allocated_size = d->allocated_size ? d->allocated_size + : new_size; + int err; + while (new_size > new_allocated_size) new_allocated_size += new_allocated_size / 2 + 1; - } - if (new_allocated_size > d->allocated_size) { - int err; if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) { d->allocated_size = 0; d->size = 0; From patchwork Mon May 25 14:07:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19856 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 AB75C44A859 for ; Mon, 25 May 2020 17:08:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 97FEE68A821; Mon, 25 May 2020 17:08:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D8D3A68A471 for ; Mon, 25 May 2020 17:08:34 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id v19so142353wmj.0 for ; Mon, 25 May 2020 07:08:34 -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=CHDkAGSWWcu8b0zzoZBZeHiN/ATFViKQ+9MbP2Ebvvc=; b=tKf0YwF63prNwRQ7l86Dv+V5WiYpDx9yDiLnyNCFKrn4lSv93AdsWk2K4DK4i+G7Y/ cMPWZRcd64+JzakV7XudfA2eLi3u7EOFu8K+7KzDHDGNSE0s7B7qDaqZMN0bzvSvHe4z ozExjOOY1YZhyVdQ5GV4WLzyuiChR4ecfXJb1fOB9qiu3sBj1c4IDpzMHNDpVE2t7vPb TaOgquNORF08rbsjuPnsx9TgOL1HGBxtI17w8AjYNY1kITd3UbNiYAmSDHpmLXSmwylI JRBwsL3GjdW06DH4U8em5Dg59zD8MVXdCu3f6FU/2sN5J3kuRoITi0lWaj8URvSnzcPO sdNg== 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=CHDkAGSWWcu8b0zzoZBZeHiN/ATFViKQ+9MbP2Ebvvc=; b=HgVLHb85+Lqy+BOm5pmfImyksY90qVTpckjMopQhrrUWNTMCV0KBk3ddjAB/oceHTe DW9LnFuvV0B+FlaQkQUojtIh5mhoH13z5wpC6H1D7Vj+I1uIDbcHZKsLO+zCVxBRfwbS IZn4MeLbjG/QNu+QBZEqCZpFCEutz6P2t7f1hfZLk47cebZawdgvhS5hEDWbpYKfcJvy xTBv27fjj52qZ7DnkF3diGUp8HsDrXzhEvuCNXvliH5Z+N6T+oGzKvPcACb/NyZPrSxw rZ8J/kBR+y7iEGzUA+/dRC/FpE+qdxxGK5ohEHBfDLD38vqH3eTeh1xvvtAshKCOEkne yuNQ== X-Gm-Message-State: AOAM533nGUaXTWXNKWBUNMdcC6B7zPclTQui7co1JP4qCSSr3pterTaz GJWUL0+DX9k3+Mn/YbfzS+n7wSsH X-Google-Smtp-Source: ABdhPJwDAnx96T1vvy4dppU04T9ktqWjpdvLuFudLMGBti7IRHugJDx8qUs9NRobZ5SJUuhQaCAcUA== X-Received: by 2002:a1c:9e43:: with SMTP id h64mr25083615wme.0.1590415714019; Mon, 25 May 2020 07:08:34 -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.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 07:08:33 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 16:07:59 +0200 Message-Id: <20200525140801.18889-3-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 3/5] avformat/aviobuf: Stop restricting dynamic buffer sizes to INT_MAX/2 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 has originally been done in 568e18b15e2ddf494fd8926707d34ca08c8edce5 as a precaution against integer overflows, but it is actually easy to support the full range of int without overflows. Signed-off-by: Andreas Rheinhardt --- libavformat/aviobuf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index fcde7e8510..688c7c50fd 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1276,7 +1276,7 @@ static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) /* reallocate buffer if needed */ new_size = (unsigned)d->pos + buf_size; - if (new_size < d->pos || new_size > INT_MAX/2) + if (new_size < d->pos || new_size > INT_MAX) return -1; if (new_size > d->allocated_size) { unsigned new_allocated_size = d->allocated_size ? d->allocated_size @@ -1285,6 +1285,8 @@ static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) while (new_size > new_allocated_size) new_allocated_size += new_allocated_size / 2 + 1; + new_allocated_size = FFMIN(new_allocated_size, INT_MAX); + if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) { d->allocated_size = 0; d->size = 0; From patchwork Mon May 25 14:08:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19857 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 A5B7144A859 for ; Mon, 25 May 2020 17:08:44 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 947F968A9F0; Mon, 25 May 2020 17:08:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 28F14689AE3 for ; Mon, 25 May 2020 17:08:36 +0300 (EEST) Received: by mail-wm1-f46.google.com with SMTP id y5so131416wmj.4 for ; Mon, 25 May 2020 07:08:36 -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=+iCDwPHwtwrx6/+ohgu6Rh2t/Ghz+2MdAFhKyl1fD10=; b=usakIHj73VqU8ofDbFFY7U0PtnbKQeOCnwY/iZnKJ6oxb5MahVzOiVN23jn3HCQ1cm 6vlyNxr3+lO7u2ppY1zymD9H6NkdS3COvfspLIFdk+GzRNJ7khKeqZ4wBbsoq8ED07zt cRRhjKpPjwpBfEPKgO+kJZrW2F72MUejs3IG/B6kS8SMdHM+ufLTFptEFZ9ILI6d7UUN MYpD1PHA5oZnAXpn2vsuxIC5FlHzCY+GJEpqvxpctLskISLLoY1XkJmAFBIJP1NpCMUy 3a+K+h8U/z/0Rg3F7iNTrDqzMMT8N2X1Fcloi5YUP1SMlgeumctTer232McGCNeymMtk UJ0w== 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=+iCDwPHwtwrx6/+ohgu6Rh2t/Ghz+2MdAFhKyl1fD10=; b=cl9Bg+MmVP+ef3maWaZ41HBWuNDjMZkvclNqFf2sP4Pe9ciljtNmCWPT5f2uJpkuWI Bfme3ay4GSthTWaQy3ZekQO9N2cCo+aZwcIJa+Zv0vOyWCGP/RQh5rs8oATKESB5aGpv K3F0WapXVynui2JVTw3BSo8qNM8l+GuzErXn8wuzKbuZPwKgXNBDWRdEvr8GtXyeOOCk i3iZTD3uQrTcmSnRkBlQ/RRTi7fTKczI+nFIEJY4h2FO6/ZWhJgTQe53uJJGzNXC10Ek xbtxo1gXk2iEGEOnB4MI1MFf2PAB7QzVodFqeCYQvqQRGYTOuRg4asgtOiNAc06P9ELE N6tA== X-Gm-Message-State: AOAM532rg4ZDeqn3hrSwSMG2okZ/D2K7ktuzruoHaaydPOdaKz6dM0g2 VMjYd6xuOXps17Rxlj8j3NTuU9Au X-Google-Smtp-Source: ABdhPJwHuTcMYue28b6qWVpEvnHHdUQ8cWNmQFASMQJZE9SKfGlr/go7K65c2zZQbzBgmliouu9UHg== X-Received: by 2002:a7b:c951:: with SMTP id i17mr23994097wml.63.1590415715169; Mon, 25 May 2020 07:08:35 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 07:08:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 May 2020 16:08:00 +0200 Message-Id: <20200525140801.18889-4-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 4/5] avformat/aviobuf: Return better error codes 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/aviobuf.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 688c7c50fd..a13c677875 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -696,7 +696,7 @@ int avio_read_partial(AVIOContext *s, unsigned char *buf, int size) int len; if (size < 0) - return -1; + return AVERROR(EINVAL); if (s->read_packet && s->write_flag) { len = read_packet_wrapper(s, buf, size); @@ -1277,7 +1277,7 @@ static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) /* reallocate buffer if needed */ new_size = (unsigned)d->pos + buf_size; if (new_size < d->pos || new_size > INT_MAX) - return -1; + return AVERROR(ERANGE); if (new_size > d->allocated_size) { unsigned new_allocated_size = d->allocated_size ? d->allocated_size : new_size; @@ -1324,8 +1324,10 @@ static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence) offset += d->pos; else if (whence == SEEK_END) offset += d->size; - if (offset < 0 || offset > 0x7fffffffLL) - return -1; + if (offset < 0) + return AVERROR(EINVAL); + if (offset > INT_MAX) + return AVERROR(ERANGE); d->pos = offset; return 0; } @@ -1336,7 +1338,7 @@ static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size) unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024; if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size) - return -1; + return AVERROR(ERANGE); d = av_mallocz(sizeof(DynBuffer) + io_buffer_size); if (!d) return AVERROR(ENOMEM); @@ -1360,7 +1362,7 @@ int avio_open_dyn_buf(AVIOContext **s) int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size) { if (max_packet_size <= 0) - return -1; + return AVERROR(EINVAL); return url_open_dyn_buf_internal(s, max_packet_size); } 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; }