Re: [PATCH] checkpatch: Fix the usage of capture group ( ... )

From: Lukas Bulwahn
Date: Mon Jul 13 2020 - 15:32:13 EST




On Mon, 13 Jul 2020, Mrinal Pandey wrote:

> The usage of "capture group (...)" in the immediate condition after `&&`
> results in `$1` being uninitialized. This issues a warning "Use of
> uninitialized value $1 in regexp compilation at ./scripts/checkpatch.pl
> line 2638".
>
> I noticed this bug while running checkpatch on the set of commits from
> v5.7 to v5.8-rc1 of the kernel on the commits with a diff content in
> their commit message.
>
> This bug was introduced in the script by commit e518e9a59ec3
> ("checkpatch: emit an error when there's a diff in a changelog"). It has
> been in the script since then.
>
> The author intended to store the match made by capture group in variable
> `$1`. This should have contained the name of the file as `[\w/]+` matched.
> However, this couldn't be accomplished due to usage of capture group and
> `$1` in the same regular expression.
>
> Fix this by placing the capture group in the condition before `&&`.
> Thus, `$1` can be initialized to the text that capture group matches
> thereby setting it to the desired and required value.
>
> Signed-off-by: Mrinal Pandey <mrinalmni@xxxxxxxxx>
> ---

I have discussed the change on the mentees list before. So:

Reviewed-by: Lukas Bulwahn <lukas.bulwahn@xxxxxxxxx>

I ran ./scripts/checkpatch.pl on:

commit 6b3e0e2e0461 ("perf tools: Support CAP_PERFMON capability")
commit 19ce2321739d ("perf flamegraph: Use /bin/bash for report and record scripts")

before and after applying this patch.
On those two patches, checkpatch.pl emits the warning before,
and does not emit after this change. So:

Tested-by: Lukas Bulwahn <lukas.bulwahn@xxxxxxxxx>

Lukas

> scripts/checkpatch.pl | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 4c820607540b..e73e998d582a 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -2636,8 +2636,8 @@ sub process {
>
> # Check if the commit log has what seems like a diff which can confuse patch
> if ($in_commit_log && !$commit_log_has_diff &&
> - (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
> - $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
> + (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
> + $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
> $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
> $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
> ERROR("DIFF_IN_COMMIT_MSG",
> --
> 2.25.1
>
>