From nobody Wed Oct 15 22:47:23 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 38DC21774CE; Sun, 24 Mar 2024 23:37:59 +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=1711323481; cv=none; b=srzDOJk3GvQGDU9JePmksgvm9Om2sENv6Mfe3LoXJOl0+CGMPZ9sMg0Q6iu5UCohsiIhqdPVuAAhgBEFUv2RfW2pliK0muI8iTWTkW5afflSYnwAacVh9Q7T8HNN9+9UVeq67c8ZmKGSi9SyYJFHShktZM2MANZdkcH23MC/Eh4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323481; c=relaxed/simple; bh=VybEtaiHM3RUVHWaxHA3J6K57xG86oXNEcOzIuXJSE4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uetNfV3hP1PjG6gj3JNM3Zj1OjfXwVyEDMIbVTHWFtdPUfeRweF168Hj1h04OOJYwNDBBpnToHuWrB3Wo3MlVaQ6To9F7Mfwo9Hn2HdbeLNZUwgiPpX7ZYKOZILLxoeKR4OyJM0FCNdaxdw2GH5CM9QvgEjwqrXXP+3BIkLUOiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gJuiESTM; 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="gJuiESTM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2DA52C43399; Sun, 24 Mar 2024 23:37:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711323479; bh=VybEtaiHM3RUVHWaxHA3J6K57xG86oXNEcOzIuXJSE4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gJuiESTMWWqJ8IzynIsmDvFFfiReT8BTDRI8sAtDhaKMQFTHKovH1VyYhh2eUkSxx Os8v1hohmTWEGg/UfELsOozi8IlGTgmuXE6zV+m4LQ7aNlImUVXv3c9aQIYf/l8oA3 BEHhIhHDXakb7FGP0nOCb3jVGvKGLmVdIYWXGsxcfCeeRy1OarC4oZSbDsW1HNNQlk eMYBHaxi2f/gWm8ZM93TqTtn8rxeAJVIYR1RCtUYw9TjWJhAs6c7ZLa6J8xHED/iKj PdaT6ml/rm1qPO2++rWafP4zpGM5Uyq1Dk3Ca+avG2OiB1hDNGYG09iXwxQfu4rHn1 esE/D65Ttb7Zw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhipeng Lu , Hans Verkuil , Sasha Levin Subject: [PATCH 5.15 184/317] media: v4l2-tpg: fix some memleaks in tpg_alloc Date: Sun, 24 Mar 2024 19:32:44 -0400 Message-ID: <20240324233458.1352854-185-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324233458.1352854-1-sashal@kernel.org> References: <20240324233458.1352854-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 7607b516a7c43..68968bfa2edc1 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