lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220820214120.497971-1-klee33@uw.edu>
Date:   Sat, 20 Aug 2022 14:41:20 -0700
From:   Kenneth Lee <klee33@...edu>
To:     apw@...onical.com, joe@...ches.com, dwaipayanray1@...il.com,
        lukas.bulwahn@...il.com
Cc:     linux-kernel@...r.kernel.org, Kenneth Lee <klee33@...edu>
Subject: [PATCH] checkpatch: add ALLOC_UNNECESSARY_ARRAY test

Using calloc|malloc_array(1, ...) can be simplified to zalloc|malloc(...)
and improves semantics. This is because we are only allocating one element
so there is no need to use the array version of the methods.

This can be applied to kvmalloc_array, kvcalloc, kcalloc, devm_kcalloc,
and devm_kmalloc_array. Note that there is no devm_kvmalloc_array or
devm_kvcalloc, so the regex does not check for this.

Signed-off-by: Kenneth Lee <klee33@...edu>
---
This is my first patch that is not a trivial cleanup, so please let me
know if I am approaching something wrong. Also unsure if the warning
name should be something else besides ALLOC_UNNECESSARY_ARRAY
---
 scripts/checkpatch.pl | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 79e759aac543..83d3a9f308ea 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7079,6 +7079,19 @@ sub process {
 			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
 		}
 
+# check for use of unnecessary array alloc methods
+# calloc|malloc_array(1, ...) should be zalloc|malloc(...)
+		if ($line =~ /(\b(?:devm_(?:kcalloc|kmalloc_array))|\b(?:kv|k)(?:calloc|malloc_array))\s*\(\s*1\s*,/) {
+			my $newfunc = "kmalloc";
+			$newfunc = "kvmalloc" if ($1 eq "kvmalloc_array");
+			$newfunc = "kvzalloc" if ($1 eq "kvcalloc");
+			$newfunc = "kzalloc" if ($1 eq "kcalloc");
+			$newfunc = "devm_kzalloc" if ($1 eq "devm_kcalloc");
+			$newfunc = "devm_kmalloc" if ($1 eq "devm_kmalloc_array");
+			WARN("ALLOC_UNNECESSARY_ARRAY",
+			     "$1(1, ...) can be simplified to $newfunc(...)\n" . $herecurr);
+		}
+
 # check for multiple semicolons
 		if ($line =~ /;\s*;\s*$/) {
 			if (WARN("ONE_SEMICOLON",
-- 
2.31.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ