From patchwork Wed Mar 17 16:32:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26427 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 B0B2144BA1E for ; Wed, 17 Mar 2021 18:32:19 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6EBF268B02A; Wed, 17 Mar 2021 18:32:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6F5B968AF00 for ; Wed, 17 Mar 2021 18:32:13 +0200 (EET) Received: by mail-ej1-f42.google.com with SMTP id r17so3580703ejy.13 for ; Wed, 17 Mar 2021 09:32: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:reply-to:mime-version :content-transfer-encoding; bh=1D/m5nThmcxRqJ4zHWDheOS5k1zMcRWwcMmALYZ3F+8=; b=djxNbrUkHRQGDZELNgR5TnxNHHg+qgg6SRyGlJd9CIrG+STES3XG9dEWFegjieOSNg kvxEBJ8ZdbPi8XHEssyn4KIMKCdZx755KS3b73irG2Hs+c+3tacwY1F7y9a9s/+iyQa8 llNig9cOCmz8QO3vuqz6+FT/pooCXsKMIROYL23R3mTI3DK1ZiVsoMKsv7/wUSse4BNA +uKdmpNsJPVrO39pH+OcVo9YH4IwSUCf14DaJIKWrQxQYrn8SRAbfRvK9CH9YNOcG2ck 9eu3eK/j++J+Co7hfTUthHNSkmx6zld1pudp3zcdWG/LlKUQu66s7lv5n5YL9iPtR4l+ 4nCQ== 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:reply-to :mime-version:content-transfer-encoding; bh=1D/m5nThmcxRqJ4zHWDheOS5k1zMcRWwcMmALYZ3F+8=; b=r8n4aX/DE0LXe63yCum9GRl9jZIwcxdzWiftrFz5YT4KAljrQAERoIITIj4FMDcxFL V8AqzV/PR8a8h2tyNtRQN1B0Po8oOAiY+ZDiwmIHyyO2q3RMuCWGSnLm8TcGqY7hc628 4MX73lDp2cQ3FSPJwcspeXRTzOSw56sWlUxesc0OfX8CtMi2NXm/+9LBixiwbCM7Nj69 kawQOVC4q/sVSuW5wSfie0oModqlIIVEsEUP26THY2AjR6ehbhmV0AzGFwsNFHpJLl52 oMRWOb0eNJikWUkvSNqJQSlYHZegVkj8PyMkpXML6x4gyETynEPi0TgbQZyZJ+8Y2dj6 PiRw== X-Gm-Message-State: AOAM531g9HPJD1iW1SblL+K0rz4NGXc43ZEIh7oxhYrh0GgLyVrnDA11 GsDGeGJVJJWJCP8Jap9XKwIWskAgWxlWNw== X-Google-Smtp-Source: ABdhPJxlNmJo5bMeQ0yK21K90Zy++x8qSS465pNaPlvwrZnfDvridKAEPMMvWtMvEXwP5JD6WAEWEg== X-Received: by 2002:a17:906:1c41:: with SMTP id l1mr36121907ejg.299.1615998732691; Wed, 17 Mar 2021 09:32:12 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id gx2sm6518382ejc.82.2021.03.17.09.32.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 09:32:12 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 17 Mar 2021 17:32:00 +0100 Message-Id: <20210317163202.672493-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/pngdec: Use internal AVBPrint string when parsing chunks 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" One saves an allocation in case the string fits into the buffer. Signed-off-by: Andreas Rheinhardt --- 5663301560d77486c7f7c03c1aa5f542fab23c24 caused a regression that makes some png files lose their metadata, because decode_idat_chunk() unrefs the frame that the metadata has been attached to. I therefore tested these commits with the aforementioned commit reverted. libavcodec/pngdec.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index a5a71ef161..6b9fdf5a22 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -517,7 +517,7 @@ static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed, const uint8_t *data_end = data + length; const uint8_t *keyword = data; const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword); - uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL; + uint8_t *kw_utf8 = NULL, *txt_utf8 = NULL; unsigned text_len; AVBPrint bp; @@ -533,19 +533,16 @@ static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed, return AVERROR_INVALIDDATA; if ((ret = decode_zbuf(&bp, data, data_end)) < 0) return ret; + data = bp.str; text_len = bp.len; - ret = av_bprint_finalize(&bp, (char **)&text); - if (ret < 0) - return ret; } else { - text = (uint8_t *)data; - text_len = data_end - text; + text_len = data_end - data; } kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword); - txt_utf8 = iso88591_to_utf8(text, text_len); - if (text != data) - av_free(text); + txt_utf8 = iso88591_to_utf8(data, text_len); + if (compressed) + av_bprint_finalize(&bp, NULL); if (!(kw_utf8 && txt_utf8)) { av_free(kw_utf8); av_free(txt_utf8); @@ -851,7 +848,7 @@ static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, static int decode_iccp_chunk(PNGDecContext *s, int length, AVFrame *f) { int ret, cnt = 0; - uint8_t *data, profile_name[82]; + uint8_t profile_name[82]; AVBPrint bp; AVFrameSideData *sd; @@ -873,19 +870,15 @@ static int decode_iccp_chunk(PNGDecContext *s, int length, AVFrame *f) if ((ret = decode_zbuf(&bp, s->gb.buffer, s->gb.buffer + length)) < 0) return ret; - ret = av_bprint_finalize(&bp, (char **)&data); - if (ret < 0) - return ret; - sd = av_frame_new_side_data(f, AV_FRAME_DATA_ICC_PROFILE, bp.len); if (!sd) { - av_free(data); + av_bprint_finalize(&bp, NULL); return AVERROR(ENOMEM); } + memcpy(sd->data, bp.str, bp.len); + av_bprint_finalize(&bp, NULL); av_dict_set(&sd->metadata, "name", profile_name, 0); - memcpy(sd->data, data, bp.len); - av_free(data); /* ICC compressed data and CRC */ bytestream2_skip(&s->gb, length + 4); From patchwork Wed Mar 17 16:32:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26428 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 D476B44BA1E for ; Wed, 17 Mar 2021 18:32:40 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B8E5268B0A6; Wed, 17 Mar 2021 18:32:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CC24568B050 for ; Wed, 17 Mar 2021 18:32:34 +0200 (EET) Received: by mail-ej1-f43.google.com with SMTP id bm21so3633988ejb.4 for ; Wed, 17 Mar 2021 09:32: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:reply-to :mime-version:content-transfer-encoding; bh=pPXokuNIfMdVIjC9/8hFvwV3g3AN1x+Yrga9ZSgGnLU=; b=RD7DuyK1XmMNqdwvdvI64pj8aXklnxu4Z0qHyCBkFw31rR1RnVz3fnBetyuU4ps64B e5hBcI2QJlejeR5ittHRC+gr4c96k5xULYstz7rN87vjHBjCxZY0Vgdn0Nyu+DZA2oeQ wickXHJLibz4viXApVg4uvaGTHqVOmmV7ogmeqOs6rM97D4IkKqWUIsTW9go+lGggcGz 1k89p+JmEQp3Hl/HQJv/a/0cl9ERStfUVpSmQFn/utTStAnsa/3Hn0c4ipDHNObIBR/Y pnpyBtyQRy7jKPcLzB05Fw+Jjn1EGxbfId4I50FsVU3/3qZQ/uSOTi4tdzbAoPHgEcWM kV6A== 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:reply-to:mime-version:content-transfer-encoding; bh=pPXokuNIfMdVIjC9/8hFvwV3g3AN1x+Yrga9ZSgGnLU=; b=lNE2aGOG86W8MiCZibk3XrGioBBp2HUaJ/3ak1yofFWvU6SBepgAurO3RywzjVc4NG Q7zzrFrWMGv6c+cCxvazdt3LB2TmwFpkye/fDqlNgrgHZiu0YKg1reXO5oRqqYApNTuf 6dARh001+IMUS2K6Y0SBcrMd8rMJv/Fe/Q8/LC+TFkqfMGlqUMIRGkbLdzHjFLffTigt 5KT8kqRNzUXTFiIn6MPNZXp1S268F42r0LTvLfJeigQR5K3pGRTV/LCMmbZQ1sZimrKo uGa3aPYBBPekOvIIWCCA4uMyDpdohe1dUF/lzGEocDPa3qZRbFbbjjO11sZ8hZ3pbA/D MKAg== X-Gm-Message-State: AOAM533JI78P+v6tNCTza6niOJ+5z2ib9nFRA7U1oqfeMNIqTzKsS7kU fM+GT3bvwJ7+nwMNU2Rqz4NPC4GofPObKg== X-Google-Smtp-Source: ABdhPJwcVOBMyjRWyEKa+bQvh2zqfWl3IFB8MBhfSmeYZ2yo0oxWr9FwKnCD8R5FSs1glcfwfFthzw== X-Received: by 2002:a17:906:1fd6:: with SMTP id e22mr37140752ejt.481.1615998754141; Wed, 17 Mar 2021 09:32:34 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id gx2sm6518382ejc.82.2021.03.17.09.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 09:32:33 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 17 Mar 2021 17:32:01 +0100 Message-Id: <20210317163202.672493-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210317163202.672493-1-andreas.rheinhardt@gmail.com> References: <20210317163202.672493-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/pngdec: Don't use AVBPrint where inappropriate 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 --- libavcodec/pngdec.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 6b9fdf5a22..21e79a24a7 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -1338,17 +1338,13 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, break; } case MKTAG('g', 'A', 'M', 'A'): { - AVBPrint bp; - char *gamma_str; + char gamma_str[18]; int num = bytestream2_get_be32(&s->gb); - av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED); - av_bprintf(&bp, "%i/%i", num, 100000); - ret = av_bprint_finalize(&bp, &gamma_str); - if (ret < 0) - return ret; + snprintf(gamma_str, sizeof(gamma_str), "%i/100000", num); - av_dict_set(&p->metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL); + if ((ret = av_dict_set(&p->metadata, "gamma", gamma_str, 0)) < 0) + return ret; bytestream2_skip(&s->gb, 4); /* crc */ break; From patchwork Wed Mar 17 16:32:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26429 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 E093944BA1E for ; Wed, 17 Mar 2021 18:32:42 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C8F5968B0A8; Wed, 17 Mar 2021 18:32:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1C65E68B097 for ; Wed, 17 Mar 2021 18:32:36 +0200 (EET) Received: by mail-ej1-f54.google.com with SMTP id lr13so3613782ejb.8 for ; Wed, 17 Mar 2021 09:32: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:reply-to :mime-version:content-transfer-encoding; bh=mi2siHozXyoiiTlc4bAC+PCHjRgke8dYakNSakG15us=; b=F4rqB0v8PsLw7JQhkSFpQtNNJo4yshNVdnYZ42FHfPGlKP9pRer3eWqtYakD8FRxCy y6dPw59RqiFK7zCbcusn7PWtJ5BSeuzxiJCAcSgLJ/HhCp1j3BmRGHZ7MkvsIY4YWORI /j+Tps7lvmkmL1kq6iJo7OeWJD9sgW+iib822AmvuVKbcvi9gZIMJ6gfxtsWhh0+IzLf Q3PMRjH3SACbp/Bz8zVslsiebQRFy9EBdDrWnHo0C/r4mpNH8XmNMJYgav1UWsXnTHpX Uht5qnjQViWTSOuOs48HqXBALa0lP5vV+0NdXxyah5VdQtm6NUwRsdNyevpF4R6OscU3 HTqg== 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:reply-to:mime-version:content-transfer-encoding; bh=mi2siHozXyoiiTlc4bAC+PCHjRgke8dYakNSakG15us=; b=tcABugyWUJHu1kzRNvtU7of7eESCWea37OR+z1QW/fFAbWF4Qt4CR8PlfCEen5BgMP WwMprEKWsog14e5OA6EXvvruuUtiWLzpfbpL4vBMNk3C5quiOywvCNxNrxiGwIK42E5u gWZNmvwHY2KQo1ifnReNHBfy8QPyn/Wx9tg3GGPHodJkZbjtbWk6WZo4dYvF4MzHMRS4 q5fVYjaMI9NGLgA2vXMPfZuVmC/lfBFcwZebuPTjSwVTiOiNM06P++LT8SE/g7uAKM4f B+CvMdXMZqvWKTtV4u8Iffr2PGNivmK3Nkprsqns6pITq+xw8hmX4WrVyxhIUKUPBNfR fymQ== X-Gm-Message-State: AOAM533U5HBgHsd/KN3t8Sj0E4VBnpP8zig4y9cmDEwuIYF/RTGKALc9 JXa03Sf4qrxIQtsLw8zWOyVCRp9uannH4Q== X-Google-Smtp-Source: ABdhPJzpfWQrqTE6Mcv1C2/O5WqodPdvgIPvo9QdfU1k8AxUc1tJ+WTgQKpyoZe8nkdAauyoWAUuaQ== X-Received: by 2002:a17:906:f9db:: with SMTP id lj27mr36760788ejb.399.1615998755345; Wed, 17 Mar 2021 09:32:35 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id gx2sm6518382ejc.82.2021.03.17.09.32.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 09:32:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 17 Mar 2021 17:32:02 +0100 Message-Id: <20210317163202.672493-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210317163202.672493-1-andreas.rheinhardt@gmail.com> References: <20210317163202.672493-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/pngdec: Improve decoding text chunks 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" By checking immediately whether the first allocation was successfull one can simplify the cleanup code in case of errors. Signed-off-by: Andreas Rheinhardt --- libavcodec/pngdec.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 21e79a24a7..813f16692c 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -539,12 +539,13 @@ static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed, text_len = data_end - data; } - kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword); txt_utf8 = iso88591_to_utf8(data, text_len); if (compressed) av_bprint_finalize(&bp, NULL); - if (!(kw_utf8 && txt_utf8)) { - av_free(kw_utf8); + if (!txt_utf8) + return AVERROR(ENOMEM); + kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword); + if (!kw_utf8) { av_free(txt_utf8); return AVERROR(ENOMEM); }