The patch titled Subject: checkpatch: check for functions with 'passed by value' structs or unions has been added to the -mm tree. Its filename is checkpatch-check-for-functions-with-passed-by-value-structs-or-unions.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/checkpatch-check-for-functions-with-passed-by-value-structs-or-unions.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/checkpatch-check-for-functions-with-passed-by-value-structs-or-unions.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Joe Perches <joe@xxxxxxxxxxx> Subject: checkpatch: check for functions with 'passed by value' structs or unions I was cc'd on a patch where structs were passed by value instead of using pointers to the structs. This can cause defects when the called function modifies the 'passed by value' struct instead of the calling function's struct. There are what seems to be some false positives for a few of the .h files in include/linux/... where the false positives are for very small structs where the indirection via a pointer might be slower than than the passed by value use. Link: http://lkml.kernel.org/r/84972e19138f1f8eafe74c6b5f1ad874f8525706.camel@xxxxxxxxxxx Signed-off-by: Joe Perches <joe@xxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- scripts/checkpatch.pl | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) --- a/scripts/checkpatch.pl~checkpatch-check-for-functions-with-passed-by-value-structs-or-unions +++ a/scripts/checkpatch.pl @@ -6092,8 +6092,10 @@ sub process { # check for function definitions if ($perl_version_ok && defined $stat && - $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { + $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*([\{;])/s) { $context_function = $1; + my $args = $2; + my $term = $3; # check for multiline function definition with misplaced open brace my $ok = 0; @@ -6104,12 +6106,26 @@ sub process { $herectx .= $rl . "\n"; $ok = 1 if ($rl =~ /^[ \+]\{/); $ok = 1 if ($rl =~ /\{/ && $n == 0); - last if $rl =~ /^[ \+].*\{/; + last if ($rl =~ /^[ \+].*[\{;]/); } - if (!$ok) { + if (!$ok && $term eq '{') { ERROR("OPEN_BRACE", "open brace '{' following function definitions go on the next line\n" . $herectx); } + +# check for 'passed by value' uses of a struct or a union that might +# be better 'passed by reference' + + while ($args =~ /(?:$Storage\s+)?($Type)\s*($Ident(?:\s*\[\s*\])?)?\s*,?/g) { + my $type = trim($1); + my $ident = defined($2) ? trim($2) : ""; + if ($type =~ /\b(?:union|struct)\b/ && + !($type =~ /(?:\*|\bconst|\])$/ || + $ident =~ /\]$/)) { + WARN("AGGREGATE_BY_VALUE", + "Unusual 'passed by value' use of '$type'\n" . $herectx); + } + } } # checks for new __setup's _ Patches currently in -mm which might be from joe@xxxxxxxxxxx are spdxcheck-work-with-current-head-licenses-directory.patch checkpatch-add-a-strict-test-for-structs-with-bool-member-definitions.patch checkpatch-add-fix-for-concatenated_string-and-string_fragments.patch checkpatch-improve-runtime-execution-speed-a-little.patch checkpatch-fix-macro-argument-reuse-test.patch checkpatch-validate-spdx-license-with-spdxcheckpy.patch checkpatch-fix-krealloc-reuse-test.patch checkpatch-check-for-functions-with-passed-by-value-structs-or-unions.patch -- To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html