Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59aecaa

Browse files
computersforpeaceintel-lab-lkp
authored andcommitted
checkpatch: Check for missing sentinels in ID arrays
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 5751eee ("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 a0d15cc --stdout | scripts/checkpatch.pl - ERROR: missing sentinel in ID array torvalds#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 exist: * where macros are used that hide the table from analysis (e.g., drivers/gpu/drm/radeon/radeon_drv.c / radeon_PCI_IDS). There are fewer than 5 of these. * where such tables are processed correctly via ARRAY_SIZE() (fewer than 5 instances). This is by far not the typical usage of *_device_id arrays. * some odd parsing artifacts, where ctx_statement_block() seems to quit in the middle of a block due to #if/#else/#endif. Signed-off-by: Brian Norris <[email protected]>
1 parent 6670175 commit 59aecaa

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

scripts/checkpatch.pl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,64 @@ sub hash_show_words {
685685
[\.\!:\s]*
686686
)};
687687

688+
# Device ID types from include/linux/mod_devicetable.h.
689+
our $dev_id_types = qr{(?x:
690+
acpi |
691+
ap |
692+
apr |
693+
auxiliary |
694+
bcma |
695+
ccw |
696+
cdx |
697+
coreboot |
698+
css |
699+
dfl |
700+
eisa |
701+
fsl_mc |
702+
hda |
703+
hid |
704+
hv_vmbus |
705+
i2c |
706+
i3c |
707+
ieee1394 |
708+
input |
709+
ipack |
710+
isapnp |
711+
ishtp |
712+
mcb |
713+
mdio |
714+
mei_cl |
715+
mhi |
716+
mips_cdmm |
717+
of |
718+
parisc |
719+
pci |
720+
pci_epf |
721+
pcmcia |
722+
platform |
723+
pnp |
724+
pnp_card |
725+
rio |
726+
rpmsg |
727+
sdio |
728+
sdw |
729+
serio |
730+
slim |
731+
spi |
732+
spmi |
733+
ssam |
734+
ssb |
735+
tee_client |
736+
typec |
737+
ulpi |
738+
usb |
739+
vchiq |
740+
vio |
741+
virtio |
742+
wmi |
743+
zorro
744+
)_device_id};
745+
688746
sub edit_distance_min {
689747
my (@arr) = @_;
690748
my $len = scalar @arr;
@@ -7678,6 +7736,30 @@ sub process {
76787736
WARN("DUPLICATED_SYSCTL_CONST",
76797737
"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
76807738
}
7739+
7740+
# Check that *_device_id tables have sentinel entries.
7741+
if (defined $stat && $line =~ /struct $dev_id_types .*\[\] = \{/) {
7742+
my $stripped = $stat;
7743+
7744+
# Strip diff line prefixes.
7745+
$stripped =~ s/(^|\n)./$1/g;
7746+
# Line continuations.
7747+
$stripped =~ s/\\\n/\n/g;
7748+
# Strip whitespace, empty strings, zeroes, and commas.
7749+
$stripped =~ s/""//g;
7750+
$stripped =~ s/0x0//g;
7751+
$stripped =~ s/[\s$;,0]//g;
7752+
# Strip field assignments.
7753+
$stripped =~ s/\.$Ident=//g;
7754+
7755+
if (!(substr($stripped, -4) eq "{}};" ||
7756+
substr($stripped, -6) eq "{{}}};" ||
7757+
$stripped =~ /PCMCIA_DEVICE_NULL};$/ ||
7758+
$stripped =~ /ISAPNP_DEVICE_SINGLE_END}};$/ ||
7759+
$stripped =~ /ISAPNP_CARD_END}};$/)) {
7760+
ERROR("MISSING_SENTINEL", "missing sentinel in ID array\n" . "$here\n$stat\n");
7761+
}
7762+
}
76817763
}
76827764

76837765
# If we have no input at all, then there is nothing to report on

0 commit comments

Comments
 (0)