[PATCH] checkpatch: Check for missing sentinels in ID arrays

Brian Norris posted 1 patch 12 months ago
There is a newer version of this series
scripts/checkpatch.pl | 75 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
[PATCH] checkpatch: Check for missing sentinels in ID arrays
Posted by Brian Norris 12 months ago
All of the ID tables based on <linux/mod_devicetable.h> (of_device_id,
pci_device_id, ...) require their arrays to end in an empty sentinel
value. That's usually spelled with an empty initializer entry (e.g.,
"{}"), but also sometimes with explicit 0 entries, field initializers
(e.g., '.id = ""'), or even a macro entry (like PCMCIA_DEVICE_NULL).

Without a sentinel, device-matching code may read out of bounds.

I've found a number of such bugs in driver reviews, and we even
occasionally commit one to the tree. See commit 5751eee5c620 ("i2c:
nomadik: Add missing sentinel to match table") for example.

Teach checkpatch to find these ID tables, and complain if it looks like
there wasn't a sentinel value.

Test output:

  $ git format-patch -1 a0d15cc47f29be6d --stdout | scripts/checkpatch.pl -
  ERROR: missing sentinel in ID array
  #57: FILE: drivers/i2c/busses/i2c-nomadik.c:1073:
  +static const struct of_device_id nmk_i2c_eyeq_match_table[] = {
   	{
   		.compatible = "XXXXXXXXXXXXXXXXXX",
   		.data = (void *)(NMK_I2C_EYEQ_FLAG_32B_BUS | NMK_I2C_EYEQ_FLAG_IS_EYEQ5),
   	},
   };

  total: 1 errors, 0 warnings, 66 lines checked

  NOTE: For some of the reported defects, checkpatch may be able to
        mechanically convert to the typical style using --fix or --fix-inplace.

  "[PATCH] i2c: nomadik: switch from of_device_is_compatible() to" has style problems, please review.

  NOTE: If any of the errors are false positives, please report
        them to the maintainer, see CHECKPATCH in MAINTAINERS.

When run across the entire tree (scripts/checkpatch.pl -q --types
MISSING_SENTINEL -f ...), false positives primarily exist (approx 2 or 3
instances) where macros are used that hide the table from analysis
(e.g., drivers/gpu/drm/radeon/radeon_drv.c / radeon_PCI_IDS).

Signed-off-by: Brian Norris <briannorris@chromium.org>
---

 scripts/checkpatch.pl | 75 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9eed3683ad76..079586fa6cf7 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -666,6 +666,61 @@ our $tracing_logging_tags = qr{(?xi:
 	[\.\!:\s]*
 )};
 
+# Device ID types from include/linux/mod_devicetable.h.
+our $dev_id_types = qr{(?x:
+	acpi |
+	ap |
+	apr |
+	auxiliary |
+	bcma |
+	ccw |
+	cdx |
+	coreboot |
+	css |
+	dfl |
+	eisa |
+	fsl_mc |
+	hda |
+	hid |
+	hv_vmbus |
+	input |
+	ipack |
+	isapnp |
+	ishtp |
+	mcb |
+	mdio |
+	mei_cl |
+	mhi |
+	mips_cdmm |
+	of |
+	parisc |
+	pci |
+	pci_epf |
+	pcmcia |
+	platform |
+	pnp |
+	pnp_card |
+	rio |
+	rpmsg |
+	sdio |
+	sdw |
+	serio |
+	slim |
+	spi |
+	spmi |
+	ssam |
+	ssb |
+	tee_client |
+	typec |
+	ulpi |
+	usb  |
+	vchiq |
+	vio |
+	virtio |
+	wmi |
+	zorro
+)_device_id};
+
 sub edit_distance_min {
 	my (@arr) = @_;
 	my $len = scalar @arr;
@@ -7676,6 +7731,26 @@ sub process {
 			WARN("DUPLICATED_SYSCTL_CONST",
 				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
 		}
+
+# Check that *_device_id tables have sentinel entries.
+		if (defined $stat && $line =~ /struct $dev_id_types .*\[\] = \{/) {
+			my $stripped = $stat;
+
+			# Strip diff line prefixes.
+			$stripped =~ s/(^|\n)./$1/g;
+			# Line continuations.
+			$stripped =~ s/\\\n/\n/g;
+			# Strip whitespace, empty strings, zeroes, and commas.
+			$stripped =~ s/""//g;
+			$stripped =~ s/0x0//g;
+			$stripped =~ s/[\s$;,0]//g;
+			# Strip field assignments.
+			$stripped =~ s/\.$Ident=//g;
+
+			if (!(substr($stripped, -4) eq "{}};" or $stripped =~ /PCMCIA_DEVICE_NULL};$/)) {
+				ERROR("MISSING_SENTINEL", "missing sentinel in ID array\n" . "$here\n$stat\n");
+			}
+		}
 	}
 
 	# If we have no input at all, then there is nothing to report on
-- 
2.47.1.613.gc27f4b7a9f-goog