From nobody Fri Dec 19 20:33:52 2025 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 5378522E818; Sun, 24 Mar 2024 23:51:48 +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=1711324309; cv=none; b=aWC55azaySUXPRacdZohMBTcLID5cW1Djjvsktf+ZCN2TW3S6gHNIN0N9TAkL65XlGwTjQbxELdqbuPfZMeqPTEPzoAaSxYGO+2MvoTyWxUMfGx9bz8TkSZjnv9QAzFIa3GjZZ+w+K3lSPKMUcXi/G+i11V2wq0gPMp7tE3dXAg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711324309; c=relaxed/simple; bh=S9uLLi/8atLuxegltsLWhu4dmc6BgpwNdMCZp5eDUl8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XuiUGrNkvZydKHIo3sSjlZY2MQlc6JJ6BzPezmJ8MyaaI2ozVysJsmWDSe9n9dC1MQRTREqIQBmmAptNZPOeVUpnoUIWS2n3on+7AMam7SSzXvgGp/z2VLnQJhnWAdg8XU1/9uzi59xO9jNus5aX0Z6Q+0J7jOl1ymdkp5uM89M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EGyhlUJJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EGyhlUJJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EA38C433F1; Sun, 24 Mar 2024 23:51:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711324307; bh=S9uLLi/8atLuxegltsLWhu4dmc6BgpwNdMCZp5eDUl8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EGyhlUJJ8bt2QaCgZe/t6SojhU/vw3WtQ1jNSZnqst5fUS5a7/UXBX/ETsm47viNg f0tN/wsK78mgb+OJ6/xXlBmhexUS6FjMUqeBGLWgLmHH8BehhlTmJGlilpIop3bfI6 xjOFo0FRK8223Vr60yj4CgOFjW7290fXAtZYCZxlD+KingRqqc/is2vCt0uXp6Wj6U 8QoNtejnzLQ1TpI/FPj3mCI16iI5gD3LoepOsQ7tq1stkrPPFzmqEA8SmD7LJEsPwy +ljzxtOUy1lnu2l7ruKOZehIiJSyzFAiLGXcZvHG7YKTa0nI3FAmC1pGRD5Oq8X9XX /jGP5uo1CAWTg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhipeng Lu , Hans Verkuil , Sasha Levin Subject: [PATCH 4.19 080/148] media: v4l2-tpg: fix some memleaks in tpg_alloc Date: Sun, 24 Mar 2024 19:49:04 -0400 Message-ID: <20240324235012.1356413-81-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324235012.1356413-1-sashal@kernel.org> References: <20240324235012.1356413-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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 2036b94269afe..182a300e2d445 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 =3D 0; =20 tpg->max_line_width =3D max_w; for (pat =3D 0; pat < TPG_MAX_PAT_LINES; pat++) { @@ -121,14 +122,18 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w) =20 tpg->lines[pat][plane] =3D vzalloc(array3_size(max_w, 2, pixelsz)); - if (!tpg->lines[pat][plane]) - return -ENOMEM; + if (!tpg->lines[pat][plane]) { + ret =3D -ENOMEM; + goto free_lines; + } if (plane =3D=3D 0) continue; tpg->downsampled_lines[pat][plane] =3D vzalloc(array3_size(max_w, 2, pixelsz)); - if (!tpg->downsampled_lines[pat][plane]) - return -ENOMEM; + if (!tpg->downsampled_lines[pat][plane]) { + ret =3D -ENOMEM; + goto free_lines; + } } } for (plane =3D 0; plane < TPG_MAX_PLANES; plane++) { @@ -136,18 +141,45 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w) =20 tpg->contrast_line[plane] =3D vzalloc(array_size(pixelsz, max_w)); - if (!tpg->contrast_line[plane]) - return -ENOMEM; + if (!tpg->contrast_line[plane]) { + ret =3D -ENOMEM; + goto free_contrast_line; + } tpg->black_line[plane] =3D vzalloc(array_size(pixelsz, max_w)); - if (!tpg->black_line[plane]) - return -ENOMEM; + if (!tpg->black_line[plane]) { + ret =3D -ENOMEM; + goto free_contrast_line; + } tpg->random_line[plane] =3D vzalloc(array3_size(max_w, 2, pixelsz)); - if (!tpg->random_line[plane]) - return -ENOMEM; + if (!tpg->random_line[plane]) { + ret =3D -ENOMEM; + goto free_contrast_line; + } } return 0; + +free_contrast_line: + for (plane =3D 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] =3D NULL; + tpg->black_line[plane] =3D NULL; + tpg->random_line[plane] =3D NULL; + } +free_lines: + for (pat =3D 0; pat < TPG_MAX_PAT_LINES; pat++) + for (plane =3D 0; plane < TPG_MAX_PLANES; plane++) { + vfree(tpg->lines[pat][plane]); + tpg->lines[pat][plane] =3D NULL; + if (plane =3D=3D 0) + continue; + vfree(tpg->downsampled_lines[pat][plane]); + tpg->downsampled_lines[pat][plane] =3D NULL; + } + return ret; } EXPORT_SYMBOL_GPL(tpg_alloc); =20 --=20 2.43.0