From patchwork Thu Feb 1 12:47:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhipeng Lu X-Patchwork-Id: 195304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2719:b0:106:209c:c626 with SMTP id hl25csp123435dyb; Thu, 1 Feb 2024 04:48:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4PCaBkXqITN73BLpsPBLOVhDxg/Po0EpXC49Bj9GpNsBM0JcnGGt51M/68iMu6hqHtXxu X-Received: by 2002:a05:620a:19a6:b0:783:458e:b1f7 with SMTP id bm38-20020a05620a19a600b00783458eb1f7mr2960361qkb.60.1706791737108; Thu, 01 Feb 2024 04:48:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706791737; cv=pass; d=google.com; s=arc-20160816; b=I/XPPwHldLxpIfECIOZsFTMbMRwlkC7uuwet6D25a8J1MI+Ipp05frTr3yo1eWTEQ3 0xueTYDYQlSdaik1DBMesZVfNVppsOAKJSK/j+z+Nk6ecGoBemrsw/0EAyCLX2GCuivL YbAz+CMEWdrqtoCSttYsoWM1QMTjYJlNnckF2jNqv8rxNNgRwpsYSu6/oAPtncFzia6v mQnngw3FiC5kvF3srtsZR+ohn153hbTzIBEr2xauiPRXIlF3QTGs03XQ2XWlesG/Kh1f LiAXd79g0bHuiJ34fOQz1vReSIKTJcGuZiPTken5TVFq/sful1jtrQRM7PcMk5hUTshN N4dA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=jFcDBhtzF1mnmhk8ValUHCCIsZVPBLKwan7I++UXwP8=; fh=WjVj+WrGSGcE2+3Kuv9kMhPuWWlWYImMPQq9rXyTeMg=; b=w6UGf3PPUXePAVQ+mR7SFvP0nTqjseAp4WItQd4/PxGCb+Q5DEl/u3O9wKkToHlRw5 7fzVG6yYZUMyRofNbTqSJpQbcWvMdURTx+M9g2CprA6q10HtpnOnJdJW6+HD8x9h1us6 n+X+E+RhSwI69CDpujy1sNPDs6IIXl3Rg5H208cYTTeu6e0d+Ig/3urMqkBbjSeiix2v H2yJxw30xJmq4C5zN+Gr5ZkKXHI0byYbHX67Ang8hGStMoo60QPw2LKtjd35II/+rD1H aibhoLnD0r4Zc6xHIu/ktutulW/TWTcIRv8VRE7eXC4zrua/g2x1JZGxPRJqDooLplY0 GoQw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-48158-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48158-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCU2yn3Rt+T61iWTxCppIogCRotP6ZYdZqpfIqC2Oni4J3dMbzzwdPB1hi2lB1pFF+KdF1BVfw30xHHBXdbUlYMwDz61ag== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d15-20020a05620a240f00b007853af18378si3506993qkn.742.2024.02.01.04.48.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 04:48:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-48158-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-48158-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48158-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E150F1C250EC for ; Thu, 1 Feb 2024 12:48:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CE2EF53373; Thu, 1 Feb 2024 12:48:35 +0000 (UTC) Received: from zg8tmtu5ljg5lje1ms4xmtka.icoremail.net (zg8tmtu5ljg5lje1ms4xmtka.icoremail.net [159.89.151.119]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D635C2135B; Thu, 1 Feb 2024 12:48:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.89.151.119 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706791714; cv=none; b=IAzI9P79oGCM5Bw2zoc/RKK/lzdost2fy2D/ZrsILktYbgPNowkK8CrH01HITHEQ+jjAA+vWaxSXeuj8aMBJSb2JtDOuTnOHnBVk1pvcnP+i2fmu7PhYldSG4miz8dlhjEUCMvX0lSjxQdopPJfbmXVWUsOSPFiQPmtrTl4ZVBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706791714; c=relaxed/simple; bh=tlpv1/+2BtvqTlePI9oh+wPTXNu3Bca21y8Cpn5fxy0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=r6Gu/GB7M5Se88jy7NnTf3OykkT7Kt/mZ+OIRUmeSgC9vKjf/PwIj/knWVG/WgacfNsPGwydTm2gQfaA+1tdvjaGGM5kVugdPy35d+9/rVDFBhiG2UdwnD4zSDHzxiaEBfpnorVc3Lb/ZliAFgyz+250YLjqXIQVwpMkPURkoY0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn; spf=pass smtp.mailfrom=zju.edu.cn; arc=none smtp.client-ip=159.89.151.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zju.edu.cn Received: from luzhipeng.223.5.5.5 (unknown [183.128.135.43]) by mail-app2 (Coremail) with SMTP id by_KCgCnfKwTk7tlV88xAQ--.53000S2; Thu, 01 Feb 2024 20:48:19 +0800 (CST) From: Zhipeng Lu To: alexious@zju.edu.cn Cc: Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] media: v4l2-tpg: fix some memleaks in tpg_alloc Date: Thu, 1 Feb 2024 20:47:53 +0800 Message-Id: <20240201124756.3054028-1-alexious@zju.edu.cn> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: by_KCgCnfKwTk7tlV88xAQ--.53000S2 X-Coremail-Antispam: 1UD129KBjvJXoWxCw13uF4kuF1xGw1fZr4fXwb_yoW5AFy7pa 4rWw1jkryUXr13tF9rArWfur43Ww4rWFy7WrW5A3yvqa4Fyr40kry5A342gan0yrWq93W3 Jryvy3Z8KFnrtFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvm14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_JrI_JrylYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc7CjxVAaw2AFwI0_ JF0_Jw1lc2xSY4AK67AK6ryUMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r 4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF 67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2I x0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2 z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnU UI43ZEXa7VUjpnQUUUUUU== X-CM-SenderInfo: qrsrjiarszq6lmxovvfxof0/ X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789700852332960640 X-GMAIL-MSGID: 1789700852332960640 In tpg_alloc, resources should be deallocated in each and every error-handling paths, since they are allocated in for statements. Otherwise there would be memleaks because tpg_free is called only when tpg_alloc return 0. Fixes: 63881df94d3e ("[media] vivid: add the Test Pattern Generator") Signed-off-by: Zhipeng Lu --- drivers/media/common/v4l2-tpg/v4l2-tpg-core.c | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c b/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c index a366566f22c3..642c48e8c1f5 100644 --- a/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c +++ b/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c @@ -113,6 +113,7 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w) { unsigned pat; unsigned plane; + int ret = 0; tpg->max_line_width = max_w; for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++) { @@ -121,14 +122,18 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w) tpg->lines[pat][plane] = vzalloc(array3_size(max_w, 2, pixelsz)); - if (!tpg->lines[pat][plane]) - return -ENOMEM; + if (!tpg->lines[pat][plane]) { + ret = -ENOMEM; + goto free_lines; + } if (plane == 0) continue; tpg->downsampled_lines[pat][plane] = vzalloc(array3_size(max_w, 2, pixelsz)); - if (!tpg->downsampled_lines[pat][plane]) - return -ENOMEM; + if (!tpg->downsampled_lines[pat][plane]) { + ret = -ENOMEM; + goto free_lines; + } } } for (plane = 0; plane < TPG_MAX_PLANES; plane++) { @@ -136,18 +141,45 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w) tpg->contrast_line[plane] = vzalloc(array_size(pixelsz, max_w)); - if (!tpg->contrast_line[plane]) - return -ENOMEM; + if (!tpg->contrast_line[plane]) { + ret = -ENOMEM; + goto free_contrast_line; + } tpg->black_line[plane] = vzalloc(array_size(pixelsz, max_w)); - if (!tpg->black_line[plane]) - return -ENOMEM; + if (!tpg->black_line[plane]) { + ret = -ENOMEM; + goto free_contrast_line; + } tpg->random_line[plane] = vzalloc(array3_size(max_w, 2, pixelsz)); - if (!tpg->random_line[plane]) - return -ENOMEM; + if (!tpg->random_line[plane]) { + ret = -ENOMEM; + goto free_contrast_line; + } } return 0; + +free_contrast_line: + for (plane = 0; plane < TPG_MAX_PLANES; plane++) { + vfree(tpg->contrast_line[plane]); + vfree(tpg->black_line[plane]); + vfree(tpg->random_line[plane]); + tpg->contrast_line[plane] = NULL; + tpg->black_line[plane] = NULL; + tpg->random_line[plane] = NULL; + } +free_lines: + for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++) + for (plane = 0; plane < TPG_MAX_PLANES; plane++) { + vfree(tpg->lines[pat][plane]); + tpg->lines[pat][plane] = NULL; + if (plane == 0) + continue; + vfree(tpg->downsampled_lines[pat][plane]); + tpg->downsampled_lines[pat][plane] = NULL; + } + return ret; } EXPORT_SYMBOL_GPL(tpg_alloc);