Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2387593lqp; Sun, 24 Mar 2024 17:11:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWR/T5M9tYGq1Bri62rSDjRc6Y7njHPV7dHnDhLgpCJ1BEypmJGGJdGoadcMyzii0E6m/Dhq/w37S5RcttOepggCFj7RPXxJSuhQeAsFg== X-Google-Smtp-Source: AGHT+IEP21pb26A8MdXSforrixUKWfhnDgijpzp5a3EW/AXZIUeg3wYOg4W1Ws10DmPfX6TgFfBc X-Received: by 2002:a05:6870:a90d:b0:221:96f6:bb67 with SMTP id eq13-20020a056870a90d00b0022196f6bb67mr6364864oab.31.1711325482115; Sun, 24 Mar 2024 17:11:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711325482; cv=pass; d=google.com; s=arc-20160816; b=YjOvAJASCtmWs/6lASg3K2hJDTd1dL4RBZV2H093gY+9wmH1CjyKagj+k6iq9DsdkC PQUhjKl6Krt59v1n7w04CIW3o4LxhVFSOARm6bG3qI0UqTWe7e63V1bH3I9wkz7VJgAl A138qYJYkPsYHb7O7fpPfC7l8MrWBw3G2CC90gq3M68baKk4CSM6Y28kM9ZrLzEmXXLI BzFLr7bVtFS4j8UddoKErmT6Y/a5aR7xpkVGYmcyl46s9qk32lNm3OKS3Kdc1jcsToNa v6Db5p+AVvedBSEqHqDdKEAa2ZCH7dcVwq1i3Uqd7RzAbJL+vrsBoOeYhCuB/hYeJPA7 GItA== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=1uAVVTrZuMzBPzYuLawrAeSxxndubx6YJIJ5GQaPGss=; fh=vmG0dG0DZT6jWf9zcuMJh9AJpWkzqGsQQMg4+lHc/TM=; b=heRNsMXawXSo4v6iJuFjn00hNQpfaNGGc5/Cm/XzhPgOudIXUK5Ru2QvZvi4Cti7dU CLThFc+uLb/1iEIwz75CL5UKKfzrNvE7WkNTBHQHBdZ2hriW2ObYBrh/E6lOnipSN6Fn h3H72qBNdhvx81c5ztjJZhX397ZMFus1+Tk3+P2GKEvm8VOAw2/aIMYw4C+mGeSMc/ri cXRhvHhksoZGMVvmivS/pnhzs69L2nvNTXSpnsqZ97ojSfDnDLMUo/ULqw+WHvYukvDJ CsVPgkiIsumC4l2EhaxVMFadrzrRJ6uEs0h+1CrQLBeddDMci9kQ7GGPpsNLu0hirJIw j8gg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=po2yI47z; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113286-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113286-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id a12-20020a631a0c000000b005dc10248136si6481869pga.314.2024.03.24.17.11.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 17:11:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-113286-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=po2yI47z; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113286-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113286-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C6DC1B23444 for ; Mon, 25 Mar 2024 00:01:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 32DAF12836B; Sun, 24 Mar 2024 22:41:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="po2yI47z" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5186418D886; Sun, 24 Mar 2024 22:41:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711320087; cv=none; b=D+SHHB9FRZxAzcSXlKPqNwOi0wQAszJg5nND5bINOykY+NQ7RCq0o07suN/0rCLbUkwez1+FDHCFO51R+Z+bu710fl+mJt5B32Axodh7CDI/ITq4PKnzKbIy3E4UQHBH0SMtFwh2oJpRDYyO7GxxY3MQxjxoU92XiHii3VCclBM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711320087; c=relaxed/simple; bh=EWgKnN4Jo0bf+oXAc5grq6JHSvSF9OfBeXnKDb9HOws=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DNnOLwUqzeRtkUkpyFExU41JeAbqpD2GAFzDBHBRcJnKRW+4nIQqGdw+jZB9X8W0Pm2gltpj4JuhxGR+emsi6FyMW7rCsiyV+AhDBz9vI1Zd3AgbjZaJWmcEkcOWkeeUKZebc2bqmNPYDDhA37NU4oTzbdI70/wUs9e+cY1yCvU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=po2yI47z; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35C8EC433F1; Sun, 24 Mar 2024 22:41:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711320086; bh=EWgKnN4Jo0bf+oXAc5grq6JHSvSF9OfBeXnKDb9HOws=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=po2yI47zHtQihVtZxa5IrNbfCZXbAxTP2HwiKwi3z/GibvlJPyVl1OS3hyiTGwO0K h25A8gneFUjMd8jObQKr6IirO5VoC3bsx80PYpjE9GLWs0VaERPBLB5JRCh96fsnRB CGVz0ItQHtA9I2v383wgO0/F0j1ywVoft1+PdD8HzSnN/q5r0/eQzsLtUQHFs7G9uS coGXAlAQDG7nkxVcgwTruqa9okXEbdc8ZlQHJnfvIjBj2+JcQuisZz5cORd2H2UBmE xYPa6zwevsZNquIs1sdtfcUo41HUW/3E1EmlnG9vbK3jT0EaA0qa+Uo9nsiv7/S4FX 5kEVZFz8tGgjw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhipeng Lu , Hans Verkuil , Sasha Levin Subject: [PATCH 6.8 395/715] media: v4l2-tpg: fix some memleaks in tpg_alloc Date: Sun, 24 Mar 2024 18:29:34 -0400 Message-ID: <20240324223455.1342824-396-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324223455.1342824-1-sashal@kernel.org> References: <20240324223455.1342824-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Zhipeng Lu [ Upstream commit 8cf9c5051076e0eb958f4361d50d8b0c3ee6691c ] 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 Signed-off-by: Hans Verkuil Signed-off-by: Sasha Levin --- 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 a366566f22c3b..642c48e8c1f58 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); -- 2.43.0