Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2489732lqp; Sun, 24 Mar 2024 23:16:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXgSR23qSCT7/ogt/zU9sXEj5sKcuPf+lpySi3+0m/qIH5ga+vhF9MUNrN3NBS3gShVvoR4NvhmVzCDYLg64FOm/zzBDiJrqb2Lnv+Cow== X-Google-Smtp-Source: AGHT+IHcmSALtY7rXLmqqlOgN60yiOmb8UKh0S3a8UVjemsfinEObirUQn4rifjgB0XqOP24Wov5 X-Received: by 2002:a05:6a00:180e:b0:6ea:8775:a66d with SMTP id y14-20020a056a00180e00b006ea8775a66dmr6354841pfa.1.1711347401387; Sun, 24 Mar 2024 23:16:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711347401; cv=pass; d=google.com; s=arc-20160816; b=YOo5nyBKuE21oe6Uv7mMYT6BZPtYojG+3JBhTphgfojEskf1ennmnxLQGlGgd+hO/a 4kttMxIJsytZNtmFfd60aBDEiiAUgZgyncjBCqdFaFWVLh5sV5I/dMucVX85F9UaORDE zRH0MM0uAm230Yy/OlcdF0jQhmiPx4TneiCjw8Q8kr6Qufhyki0Os92UH5nV8LM7MYNh DG7jc5CcV4r1yiZccbb7PlvhjSui5hdRyUD3duab/13Oy6SeQqaM/uInAy2xT14kBmHh fGhaD5MJsLpXq3ZSJoC+Dp9OLwum8K6Qg89ui3MnYqQ+u9UUfsPYJe+DSLK7zt4IrTg4 NSmg== 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=eBNGEkWBZd/9IjWjqweCQFRQW+JGlwm7D8WdW8XjieU=; fh=vmG0dG0DZT6jWf9zcuMJh9AJpWkzqGsQQMg4+lHc/TM=; b=z+VJSWuN/ToDiKtICYL+j8v7ZAvST9X99fqrRQv1tMeXF4ojozcNWfmTi/KnND6603 TuNt3G90+WXHuMRZEam2pbRVN0JOgv/FqfiiiQy+1pUuyqZktuGD9CFVxn5XRx2qjacY PaY1Y+hHbkFC9ZjNGed5Pq/L0tp3qjGou4ZVjz2gpvItFCg2ow4IHDgeVZJH6dUplxKI VZ1Vn5ZRRObAlSSpSS++MFeVkg8LaLZT8zn2CxdW8iuK7Bv3CM31irxfIw0NVulWfXmv OqN6vjBjTBunP7NXzxthcY8toNXdqVeZS/9hNqlUM87BWBS9msuipoHwpL8RW0eH4C/p Rj/Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WF8KMHqP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-114553-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-114553-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i64-20020a62c143000000b006e6b7630052si4496409pfg.280.2024.03.24.23.16.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 23:16:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-114553-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WF8KMHqP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-114553-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-114553-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 195B02918BF for ; Mon, 25 Mar 2024 06:16:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF7ED1327E8; Sun, 24 Mar 2024 23:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WF8KMHqP" 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 682551FC11E; Sun, 24 Mar 2024 23:16:28 +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=1711322189; cv=none; b=ECxPtpOUHjGHyFHQiEw7A48Rne/tZ9Bi6Gq5F74vLBou/tSmiQQjfswpYl5OrZRRPzdfCLflvY52FgB5sAb6oN1RsG+MKmGAXLca46/fLOI9AD4LkgexiyseFCt5lPODUXGMuKUG1rJeza8pP6WUz1DRLZnXevsZZ/tAOyOZxII= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322189; c=relaxed/simple; bh=+PdrBJMRzycgYFnlFnd67YpMk8LzfKBbpLIvVMOmQGw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rovhOUoW//xKoLtFlDyxmA1mkapkFjWYznM6rnlCW1mx0KN0oa2tTQJdFjSzxoeztOmhsll6zkPPtGFxHQvsZps+zDoi1+w4eiMmPIeuflvt/LcCVxjOlI1uM7OLumQV2F2wSoDDxvxWYlyDsBMJTmre8rF4WYvvv4M2Lxe17AE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WF8KMHqP; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 015FAC433C7; Sun, 24 Mar 2024 23:16:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711322187; bh=+PdrBJMRzycgYFnlFnd67YpMk8LzfKBbpLIvVMOmQGw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WF8KMHqPX87QN5C6Qw+XXFgRHeLJ7jzsjyZprdECk2eHpSUkTqA6fIF8OryA6Q5hk ZWATW9tdSG73286ubANUgSpe0+Fs9asMWtRYG0LhMded5dRmsqaLOW+QYAQCPIhS9D xb3ZfkkCztUwlTYVqPU3bw7Jub1LCrDqorITFT1VsUXb3tkHPSn7cawOwXQwbfPHzK R+XTZb8/jwrJiI/Okz7TYp3t5BzKSSCNUcxlS6x7wm0dX5RqQR6+TeE0HwhnJCAqv3 17plsItoCZWvO+pJ1/wZxwrJbOwQHMZdMFD2F7jfTl3gvNoD4E4Y2zrbqdYJrkNvdt tXsK/N/rAH98w== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhipeng Lu , Hans Verkuil , Sasha Levin Subject: [PATCH 6.1 266/451] media: v4l2-tpg: fix some memleaks in tpg_alloc Date: Sun, 24 Mar 2024 19:09:02 -0400 Message-ID: <20240324231207.1351418-267-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324231207.1351418-1-sashal@kernel.org> References: <20240324231207.1351418-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 303d02b1d71c9..fe30f5b0050dd 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