From patchwork Thu May 21 02:36: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: 19788 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 EB5BF44BE19 for ; Thu, 21 May 2020 05:37:03 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B4CF468A9DB; Thu, 21 May 2020 05:37:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B9B966806A5 for ; Thu, 21 May 2020 05:36:56 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id f134so4293568wmf.1 for ; Wed, 20 May 2020 19:36:56 -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=iakEvigs8uECCmHHLgGT38QG3sk8vw/uVD8C/AMiDq8=; b=tiK1qBg2JiVE/W3MyUTp6S2mzMvZg05isrUGjfyN9a4emoOsO+2png6mSNblYTT7/x ziciEeSyvrYI6+qX9hwrhELHCJPJb0jobwlrIYSNEJcEeU+SriLueifDsM+ZUdHHNkSw 9GfgQGSYoQlnv8k0XJ5lYflXQ+3INxhrroIsKvTwH/QrDI4lPaer0p+T1JFuk9Csh0ra erLmnIys+yuZBRfrVj0lLm8uWOE9/PLnoBGcMb/PBM1kfQcWCQo4z5ZF0Nt23kTbe9wu j3AzvLKpAfRyXfSRwGtglRG8R6LPkTKme7rvarduX0eVdX1tvper9RO2LBMlnMymWCJB PzFA== 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=iakEvigs8uECCmHHLgGT38QG3sk8vw/uVD8C/AMiDq8=; b=p/v8t1Fah62YDW82ezH1kcGaipIRwm7myKCIn0vftf2aEMqT53JO042ntRc1dtYDui j+O6Zr/StQ1Mlbozz0zm5gathFOWiP7GSGn5PtNMCLjzfFLxTFuudLND5txHqSh3WoDa NQ/+ZevO+LSN4jAV9W77xmbkZc7XYDEa/VdwSPPHxStQZlbyPVHsWQkwpcIvdpw123Pd gvU3GYz0djyKSAEsh6YlujyqQd2OM6JOdB+IY3JrekDSrhjI9z4eGyCb37WxpG7w+40o QlUB77QXHAtGWq7T0OGGO02553pj13mq6YJPxvObBqanJIJxqZ0rDPecXefJ9tMGuzmf 8iZA== X-Gm-Message-State: AOAM531m+/CHqDzKQd9R13Yvp0dIxZCeCoC47HuXdaF36/lYNnJbyYGl o2uceD3UIkZ2R7t7SIjChndJYRhp X-Google-Smtp-Source: ABdhPJwIBXBC59Hx66swsZcb2DViuQZ9OTax52G0QeHsNj6Z3maoaf65As/M1kqRrZW8MLCTFiLrPg== X-Received: by 2002:a1c:9e8a:: with SMTP id h132mr6966310wme.32.1590028615949; Wed, 20 May 2020 19:36:55 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id w15sm4654073wrl.73.2020.05.20.19.36.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2020 19:36:55 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 21 May 2020 04:36:45 +0200 Message-Id: <20200521023647.27847-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avformat/matroskadec: Beautify matroska_parse_laces() 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/matroskadec.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 4fa32365c3..c02c2561c1 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -3035,9 +3035,9 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, if (size <= 0) return AVERROR_INVALIDDATA; - *laces = *data + 1; - data += 1; - size -= 1; + *laces = *data + 1; + data += 1; + size -= 1; switch (type) { case 0x1: /* Xiph lacing */ @@ -3047,31 +3047,26 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, for (n = 0; n < *laces - 1; n++) { lace_size[n] = 0; - while (1) { - if (size <= total) { + do { + if (size <= total) return AVERROR_INVALIDDATA; - } temp = *data; total += temp; lace_size[n] += temp; data += 1; size -= 1; - if (temp != 0xff) - break; - } + } while (temp == 0xff); } - if (size < total) { + if (size < total) return AVERROR_INVALIDDATA; - } lace_size[n] = size - total; break; } case 0x2: /* fixed-size lacing */ - if (size % (*laces)) { + if (size % (*laces)) return AVERROR_INVALIDDATA; - } for (n = 0; n < *laces; n++) lace_size[n] = size / *laces; break; @@ -3107,15 +3102,15 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, } data += offset; size -= offset; - if (size < total) { + if (size < total) return AVERROR_INVALIDDATA; - } + lace_size[*laces - 1] = size - total; break; } } - *buf = data; + *buf = data; return 0; } From patchwork Thu May 21 02:36: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: 19789 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 ECE18448A5C for ; Thu, 21 May 2020 05:37:35 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D6B2768AA7F; Thu, 21 May 2020 05:37:35 +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 4429168AB96 for ; Thu, 21 May 2020 05:37:29 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id z72so4876321wmc.2 for ; Wed, 20 May 2020 19:37:29 -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=kbe/ZkT+aoGXLKT//OZupuoJmo+gYH+Pl2ayNmksC/E=; b=IZAfbShm94KCAVYrhcYdVcje+tdcDH3ra+ylylPh7c2SykyUKwapqKSif2RJ+Y2YOi nI0Pm/h2GVoWgMk0BNfci1UZVm7KdkpFWguidsxuEvFzwgaTUNFnldyO/+mKlklnJZSi GMZbC1w+hMZ9AJz+74UfWYpmC78NrOlOIrm65wTdqFIrfqEex+hGTJRDErxvQd7RbqWC IxuM3anDiT97YJO5RYMtw4T6nL3p4sAnciDyUL7EBhARdam6m2PV5HgmA3Q4J+PuD08D TlF9R6yTQpd50ErZIy13SHwx1uZxC5JisWA13pOINenfd3yLwpWi6EaDBZtO1KqqGcVl GavQ== 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=kbe/ZkT+aoGXLKT//OZupuoJmo+gYH+Pl2ayNmksC/E=; b=EopnALuOzcsqS8l+2NC4zocYf0VCsXFqEuBypfgyPRfKX+fQe9roT8YSu7vGoTH9e6 8EtA6x/rsyS8lQgI1pA4BICyaMzohvYyhtnVSCAIRL9XJeSCSKq949k/PQERWtVi1rOP 65oR6Qx/BhrP/8pRHZGBRd6CqGfnu5u1gqB5hz4d6PI9lgsLD2ulvunWY8LJtY3uMeKN NZX2d6EcYskQOcRW/Wv70b5JiQK4QiwGDndvdiTyoK7zJmxPAOxmpv5GD6T/Iu2hNp71 FTufmfxQ+mWsudoIx7CpmmSaVRC8Y8frRuEZpG0r7lEwC5XPRrXqumpMJynxWzwHrQqi fzNw== X-Gm-Message-State: AOAM533BalK75Dyl1vGQO+oKZMRhWLnlgfopV/AxGkNVHon9GjHtLfvr ZJU47zoMXLz5nFzbkVbdVWvX5pbQ X-Google-Smtp-Source: ABdhPJx1ArWgC+Bt8C6ZDK/VpTdmZSFjxM9OFgfQNn5wTEI3Csg5BwXKIP4You8W/xPNM8Xo6wH+fA== X-Received: by 2002:a1c:b4d4:: with SMTP id d203mr6959499wmf.188.1590028648470; Wed, 20 May 2020 19:37:28 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id w15sm4654073wrl.73.2020.05.20.19.37.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2020 19:37:27 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 21 May 2020 04:36:46 +0200 Message-Id: <20200521023647.27847-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521023647.27847-1-andreas.rheinhardt@gmail.com> References: <20200521023647.27847-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avformat/ttaenc: Defer freeing dynamic buffer 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 TTA muxer writes a seektable in a dynamic buffer as it receives packets and when writing the trailer, closes the dynamic buffer using avio_close_dyn_buf(), writes the seektable and frees the buffer. But the TTA muxer already has a deinit function which unconditionally calls ffio_free_dyn_buf() on the dynamic buffer, so switching to avio_get_dyn_buf() means that one can remove the code to free the buffer; furthermore, it also might save an allocation if the seektable is so small that it fits into the dynamic buffer's write buffer or if adding the padding that avio_close_dyn_buf() adds necessitated reallocating of the underlying buffer. Signed-off-by: Andreas Rheinhardt --- libavformat/ttaenc.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libavformat/ttaenc.c b/libavformat/ttaenc.c index 4860aab4c1..becd3e7153 100644 --- a/libavformat/ttaenc.c +++ b/libavformat/ttaenc.c @@ -145,10 +145,8 @@ static int tta_write_trailer(AVFormatContext *s) /* Write Seek table */ crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX; avio_wl32(tta->seek_table, crc); - size = avio_close_dyn_buf(tta->seek_table, &ptr); + size = avio_get_dyn_buf(tta->seek_table, &ptr); avio_write(s->pb, ptr, size); - tta->seek_table = NULL; - av_free(ptr); /* Write audio data */ tta_queue_flush(s); From patchwork Thu May 21 02:36: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: 19790 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 528CC448A5C for ; Thu, 21 May 2020 05:37:37 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 37A7268AD2F; Thu, 21 May 2020 05:37:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 68FE768AA7F for ; Thu, 21 May 2020 05:37:30 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id l11so5193980wru.0 for ; Wed, 20 May 2020 19:37:30 -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=qs10Y/DogxWpy/vTfDYr0svFvMTCF33octCuzB9RpMU=; b=kYNEx6/AqXXXSN03rImlYfxj9JOX73h1SOFJHcrwfX6kZ1CWTaEmFKvoAxQSOB/PPP FkwF7dCTfbYBDXky0t85DQKuzh7EWs6Q7VFQohB0rfyZIUuQJrN6szs4squFDMglhdAt LCVM4vZjZ/pq+D3020IYT/lDcRC/7N4yNe8RUm2L5Imgye83AMxnS1Tb2fGq6ONdo9js /6ivQdwDuOrfLFmUSx8u9wpJAU8tBV6MgPrBjQ52L5VhbQvFiS/Wug9PdNLDT7c/CKO+ n+QtpRPJ2WXCCWDPOsocOrNzJIPlej1+ZjUMeNCEwlyujBxqnAF1XEFvA9Kg8HgHbOJP k0VA== 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=qs10Y/DogxWpy/vTfDYr0svFvMTCF33octCuzB9RpMU=; b=npF2a+M+c7kh7cq+OfYPR0LV5LZumgtJRQs/UTq+dYYAISZP2BSGG1ui3bnaEm24CO sg9nyRlMjA5csHxXqUt8ULG/hxPLoShxsBGv+Zp4Rb4mM1e+/Adi8YBXt8RZB+SL3X05 mp0i8ruGxuAqCrNOBPMKriIYVxQvN5d74Hf6qxDhd2ydbkw3RHfm7LFRyquByW6YuTuJ Ia9Q9siboANFaVBtKB1lDihIlkL38uOhvTu1tyt12T7H+EFNqZxC1bk+FvA28Agy9ryR KCW7+qXa95qKyVefuG7txBq4lbfHX5xBPmXgKNSSlwBcMLgRvqMyz+EwP5A5+kqJk+JO rdGw== X-Gm-Message-State: AOAM5306jRev1aDT+nYn0xQ/v0r9EEF7s9+Be3UoZSfwtx7MPyl9LkXv JxqV9JR0hlQe9J9rMyVzlKvIodP3 X-Google-Smtp-Source: ABdhPJx1fqfN8OWX/+5rMaHatyMB1YwgxgQjGOhBUfRVrCszuTEH0dDiztr5+0cy9kltVuZKl6F0ow== X-Received: by 2002:a5d:4dc3:: with SMTP id f3mr2898522wru.417.1590028649433; Wed, 20 May 2020 19:37:29 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id w15sm4654073wrl.73.2020.05.20.19.37.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2020 19:37:28 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 21 May 2020 04:36:47 +0200 Message-Id: <20200521023647.27847-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521023647.27847-1-andreas.rheinhardt@gmail.com> References: <20200521023647.27847-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] avutil/mem: Use max_alloc_size as-is 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 size of a single allocation performed by av_malloc() or av_realloc() is supposed to be bounded by max_alloc_size, which defaults to INT_MAX and can be set by the user; yet currently this is not completely honoured: The actual value used is max_alloc_size - 32. How this came to be can only be understood historically: a) 0ecca7a49f8e254c12a3a1de048d738bfbb614c6 disallowed allocations > INT_MAX. At that time the size parameter of av_malloc() was an unsigned and the commentary added ("lets disallow possible ambiguous cases") indicates that this was done as a precaution against calling the functions with negative int values. Genuinely limiting the size of allocations to INT_MAX doesn't seem to have been the intention given that at this time the memalign hack introduced in commit da9b170c6f06184a5114dc66afb8385cd0ffff83 (which when enabled increased the size of allocations slightly so that one can return a correctly aligned pointer that actually does not point to the beginning of the allocated buffer) was already present. b) Said memalign hack allocated 17 bytes more than actually desired, yet allocating 16 bytes more is actually enough and so this was changed in a9493601638b048c44751956d2360f215918800c; this commit also replaced INT_MAX by INT_MAX - 16 (and made the limit therefore a limit on the size of the allocated buffer), but kept the comment, although there is nothing ambiguous about allocating (INT_MAX - 16)..INT_MAX. c) 13dfce3d44f99a2d7df71aba8ae003d58db726f7 then increased 16 to 32 for AVX, 6b4c0be5586acad3bbafd7d2dd02a8328a5ab632 replaced INT_MAX by MAX_MALLOC_SIZE (which was of course defined to be INT_MAX) and 5a8e994287d8ef181c0a5eac537547d7059b4524 added max_alloc_size and made it user-selectable. d) 4fb311c804098d78e5ce5f527f9a9c37536d3a08 then dropped the memalign hack, yet it kept the -32 (probably because the comment about ambiguous cases was still present?), although it is no longer needed at all after this commit. Therefore this commit removes it and uses max_alloc_size directly. Signed-off-by: Andreas Rheinhardt --- libavutil/mem.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libavutil/mem.c b/libavutil/mem.c index a29c224264..cfb6d8ab8f 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -78,8 +78,7 @@ void *av_malloc(size_t size) { void *ptr = NULL; - /* let's disallow possibly ambiguous cases */ - if (size > (max_alloc_size - 32)) + if (size > max_alloc_size) return NULL; #if HAVE_POSIX_MEMALIGN @@ -134,8 +133,7 @@ void *av_malloc(size_t size) void *av_realloc(void *ptr, size_t size) { - /* let's disallow possibly ambiguous cases */ - if (size > (max_alloc_size - 32)) + if (size > max_alloc_size) return NULL; #if HAVE_ALIGNED_MALLOC @@ -482,12 +480,12 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) if (min_size <= *size) return ptr; - if (min_size > max_alloc_size - 32) { + if (min_size > max_alloc_size) { *size = 0; return NULL; } - min_size = FFMIN(max_alloc_size - 32, FFMAX(min_size + min_size / 16 + 32, min_size)); + min_size = FFMIN(max_alloc_size, FFMAX(min_size + min_size / 16 + 32, min_size)); ptr = av_realloc(ptr, min_size); /* we could set this to the unmodified min_size but this is safer