2014-10-22 10:44:05

by Jani Nikula

[permalink] [raw]
Subject: regression: checkpatch.pl fails when called via symlink


Since commit 66b47b4a9dad00e45c049d79966de9a3a1f4d337
Author: Kees Cook <[email protected]>
Date: Mon Oct 13 15:51:57 2014 -0700

checkpatch: look for common misspellings

checkpatch.pl dies with

checkpatch.pl: Can't open /path/to/spelling.txt for reading: No such file or directory

if it's called through a symlink in /path/to/checkpatch.pl pointing at
the actual script.

Please fail graciously, and just ignore spelling if you can't find
spelling.txt. Or better yet, follow the links if you can.


Thanks,
Jani.


--
Jani Nikula, Intel Open Source Technology Center


2014-10-22 12:20:23

by Joe Perches

[permalink] [raw]
Subject: Re: regression: checkpatch.pl fails when called via symlink

On Wed, 2014-10-22 at 13:43 +0300, Jani Nikula wrote:
> Since commit 66b47b4a9dad00e45c049d79966de9a3a1f4d337
> Author: Kees Cook <[email protected]>
> Date: Mon Oct 13 15:51:57 2014 -0700
>
> checkpatch: look for common misspellings
>
> checkpatch.pl dies with
>
> checkpatch.pl: Can't open /path/to/spelling.txt for reading: No such file or directory
>
> if it's called through a symlink in /path/to/checkpatch.pl pointing at
> the actual script.
>
> Please fail graciously, and just ignore spelling if you can't find
> spelling.txt. Or better yet, follow the links if you can.

I agree it should fail better, but I'm also curious.
Why you want to use a symlink?

2014-10-22 13:26:06

by Jani Nikula

[permalink] [raw]
Subject: Re: regression: checkpatch.pl fails when called via symlink

On Wed, 22 Oct 2014, Joe Perches <[email protected]> wrote:
> On Wed, 2014-10-22 at 13:43 +0300, Jani Nikula wrote:
>> Since commit 66b47b4a9dad00e45c049d79966de9a3a1f4d337
>> Author: Kees Cook <[email protected]>
>> Date: Mon Oct 13 15:51:57 2014 -0700
>>
>> checkpatch: look for common misspellings
>>
>> checkpatch.pl dies with
>>
>> checkpatch.pl: Can't open /path/to/spelling.txt for reading: No such file or directory
>>
>> if it's called through a symlink in /path/to/checkpatch.pl pointing at
>> the actual script.
>>
>> Please fail graciously, and just ignore spelling if you can't find
>> spelling.txt. Or better yet, follow the links if you can.
>
> I agree it should fail better, but I'm also curious.
> Why you want to use a symlink?

Simply because I think having $HOME/bin in $PATH and a symlink there is
better than the alternatives.

I don't want to add scripts to $PATH because there's so much cruft. I
don't want to copy checkpatch.pl (and now spelling.txt) because I
generally want it to stay up-to-date. I don't want to use an alias
because some maintainer scripts would break. I don't want to use a full
path because it's tedious, and potentially different on different
machines.

Reference: http://xkcd.com/1172/


BR,
Jani.

--
Jani Nikula, Intel Open Source Technology Center

2014-10-22 17:13:40

by Joe Perches

[permalink] [raw]
Subject: Re: regression: checkpatch.pl fails when called via symlink

On Wed, 2014-10-22 at 16:25 +0300, Jani Nikula wrote:
> On Wed, 22 Oct 2014, Joe Perches <[email protected]> wrote:
> > On Wed, 2014-10-22 at 13:43 +0300, Jani Nikula wrote:
> >> Since commit 66b47b4a9dad00e45c049d79966de9a3a1f4d337
> >> Author: Kees Cook <[email protected]>
> >> Date: Mon Oct 13 15:51:57 2014 -0700
> >> checkpatch: look for common misspellings
> >> checkpatch.pl dies with
> >> checkpatch.pl: Can't open /path/to/spelling.txt for reading: No such file or directory
> >>
> >> if it's called through a symlink in /path/to/checkpatch.pl pointing at
> >> the actual script.
> >>
> >> Please fail graciously, and just ignore spelling if you can't find
> >> spelling.txt. Or better yet, follow the links if you can.
> >
> > I agree it should fail better, but I'm also curious.
> > Why you want to use a symlink?
>
> Simply because I think having $HOME/bin in $PATH and a symlink there is
> better than the alternatives.
[]
> Reference: http://xkcd.com/1172/

:)

Try this:

scripts/checkpatch.pl | 38 +++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d94f5d8..a6354ec 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7,10 +7,11 @@

use strict;
use POSIX;
+use File::Basename;
+use Cwd 'abs_path';

my $P = $0;
-$P =~ s@(.*)/@@g;
-my $D = $1;
+my $D = dirname(abs_path($P));

my $V = '0.32';

@@ -438,26 +439,29 @@ our $allowed_asm_includes = qr{(?x:

# Load common spelling mistakes and build regular expression list.
my $misspellings;
-my @spelling_list;
my %spelling_fix;
-open(my $spelling, '<', $spelling_file)
- or die "$P: Can't open $spelling_file for reading: $!\n";
-while (<$spelling>) {
- my $line = $_;

- $line =~ s/\s*\n?$//g;
- $line =~ s/^\s*//g;
+if (open(my $spelling, '<', $spelling_file)) {
+ my @spelling_list;
+ while (<$spelling>) {
+ my $line = $_;

- next if ($line =~ m/^\s*#/);
- next if ($line =~ m/^\s*$/);
+ $line =~ s/\s*\n?$//g;
+ $line =~ s/^\s*//g;

- my ($suspect, $fix) = split(/\|\|/, $line);
+ next if ($line =~ m/^\s*#/);
+ next if ($line =~ m/^\s*$/);
+
+ my ($suspect, $fix) = split(/\|\|/, $line);

- push(@spelling_list, $suspect);
- $spelling_fix{$suspect} = $fix;
+ push(@spelling_list, $suspect);
+ $spelling_fix{$suspect} = $fix;
+ }
+ close($spelling);
+ $misspellings = join("|", @spelling_list);
+} else {
+ warn "No typos will be found - file '$spelling_file': $!\n";
}
-close($spelling);
-$misspellings = join("|", @spelling_list);

sub build_types {
my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
@@ -2246,7 +2250,7 @@ sub process {
}

# Check for various typo / spelling mistakes
- if ($in_commit_log || $line =~ /^\+/) {
+ if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
my $typo = $1;
my $typo_fix = $spelling_fix{lc($typo)};

2014-10-23 17:17:03

by Jani Nikula

[permalink] [raw]
Subject: Re: regression: checkpatch.pl fails when called via symlink

On Wed, 22 Oct 2014, Joe Perches <[email protected]> wrote:
> On Wed, 2014-10-22 at 16:25 +0300, Jani Nikula wrote:
>> On Wed, 22 Oct 2014, Joe Perches <[email protected]> wrote:
>> > On Wed, 2014-10-22 at 13:43 +0300, Jani Nikula wrote:
>> >> Since commit 66b47b4a9dad00e45c049d79966de9a3a1f4d337
>> >> Author: Kees Cook <[email protected]>
>> >> Date: Mon Oct 13 15:51:57 2014 -0700
>> >> checkpatch: look for common misspellings
>> >> checkpatch.pl dies with
>> >> checkpatch.pl: Can't open /path/to/spelling.txt for reading: No such file or directory
>> >>
>> >> if it's called through a symlink in /path/to/checkpatch.pl pointing at
>> >> the actual script.
>> >>
>> >> Please fail graciously, and just ignore spelling if you can't find
>> >> spelling.txt. Or better yet, follow the links if you can.
>> >
>> > I agree it should fail better, but I'm also curious.
>> > Why you want to use a symlink?
>>
>> Simply because I think having $HOME/bin in $PATH and a symlink there is
>> better than the alternatives.
> []
>> Reference: http://xkcd.com/1172/
>
> :)
>
> Try this:

Tested-by: Jani Nikula <[email protected]>

Thanks,
Jani.


>
> scripts/checkpatch.pl | 38 +++++++++++++++++++++-----------------
> 1 file changed, 21 insertions(+), 17 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index d94f5d8..a6354ec 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -7,10 +7,11 @@
>
> use strict;
> use POSIX;
> +use File::Basename;
> +use Cwd 'abs_path';
>
> my $P = $0;
> -$P =~ s@(.*)/@@g;
> -my $D = $1;
> +my $D = dirname(abs_path($P));
>
> my $V = '0.32';
>
> @@ -438,26 +439,29 @@ our $allowed_asm_includes = qr{(?x:
>
> # Load common spelling mistakes and build regular expression list.
> my $misspellings;
> -my @spelling_list;
> my %spelling_fix;
> -open(my $spelling, '<', $spelling_file)
> - or die "$P: Can't open $spelling_file for reading: $!\n";
> -while (<$spelling>) {
> - my $line = $_;
>
> - $line =~ s/\s*\n?$//g;
> - $line =~ s/^\s*//g;
> +if (open(my $spelling, '<', $spelling_file)) {
> + my @spelling_list;
> + while (<$spelling>) {
> + my $line = $_;
>
> - next if ($line =~ m/^\s*#/);
> - next if ($line =~ m/^\s*$/);
> + $line =~ s/\s*\n?$//g;
> + $line =~ s/^\s*//g;
>
> - my ($suspect, $fix) = split(/\|\|/, $line);
> + next if ($line =~ m/^\s*#/);
> + next if ($line =~ m/^\s*$/);
> +
> + my ($suspect, $fix) = split(/\|\|/, $line);
>
> - push(@spelling_list, $suspect);
> - $spelling_fix{$suspect} = $fix;
> + push(@spelling_list, $suspect);
> + $spelling_fix{$suspect} = $fix;
> + }
> + close($spelling);
> + $misspellings = join("|", @spelling_list);
> +} else {
> + warn "No typos will be found - file '$spelling_file': $!\n";
> }
> -close($spelling);
> -$misspellings = join("|", @spelling_list);
>
> sub build_types {
> my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
> @@ -2246,7 +2250,7 @@ sub process {
> }
>
> # Check for various typo / spelling mistakes
> - if ($in_commit_log || $line =~ /^\+/) {
> + if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
> while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
> my $typo = $1;
> my $typo_fix = $spelling_fix{lc($typo)};
>
>

--
Jani Nikula, Intel Open Source Technology Center

2014-10-23 17:29:16

by Joe Perches

[permalink] [raw]
Subject: [PATCH] checkpatch: fix use via symlink, make missing spelling file non-fatal

Commit 66b47b4a9dad ("checkpatch: look for common misspellings")
made it difficult to use checkpatch via a symlink.

Fix that and make a missing spelling.txt file non-fatal.
Emit a warning when the spelling.txt file can not be opened.

Reference: http://xkcd.com/1172/

Signed-off-by: Joe Perches <[email protected]>
Reported-by: Jani Nikula <[email protected]>
Tested-by: Jani Nikula <[email protected]>
---
scripts/checkpatch.pl | 38 +++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d94f5d8..a6354ec 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7,10 +7,11 @@

use strict;
use POSIX;
+use File::Basename;
+use Cwd 'abs_path';

my $P = $0;
-$P =~ s@(.*)/@@g;
-my $D = $1;
+my $D = dirname(abs_path($P));

my $V = '0.32';

@@ -438,26 +439,29 @@ our $allowed_asm_includes = qr{(?x:

# Load common spelling mistakes and build regular expression list.
my $misspellings;
-my @spelling_list;
my %spelling_fix;
-open(my $spelling, '<', $spelling_file)
- or die "$P: Can't open $spelling_file for reading: $!\n";
-while (<$spelling>) {
- my $line = $_;

- $line =~ s/\s*\n?$//g;
- $line =~ s/^\s*//g;
+if (open(my $spelling, '<', $spelling_file)) {
+ my @spelling_list;
+ while (<$spelling>) {
+ my $line = $_;

- next if ($line =~ m/^\s*#/);
- next if ($line =~ m/^\s*$/);
+ $line =~ s/\s*\n?$//g;
+ $line =~ s/^\s*//g;

- my ($suspect, $fix) = split(/\|\|/, $line);
+ next if ($line =~ m/^\s*#/);
+ next if ($line =~ m/^\s*$/);
+
+ my ($suspect, $fix) = split(/\|\|/, $line);

- push(@spelling_list, $suspect);
- $spelling_fix{$suspect} = $fix;
+ push(@spelling_list, $suspect);
+ $spelling_fix{$suspect} = $fix;
+ }
+ close($spelling);
+ $misspellings = join("|", @spelling_list);
+} else {
+ warn "No typos will be found - file '$spelling_file': $!\n";
}
-close($spelling);
-$misspellings = join("|", @spelling_list);

sub build_types {
my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
@@ -2246,7 +2250,7 @@ sub process {
}

# Check for various typo / spelling mistakes
- if ($in_commit_log || $line =~ /^\+/) {
+ if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
my $typo = $1;
my $typo_fix = $spelling_fix{lc($typo)};


2014-10-23 18:52:26

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: fix use via symlink, make missing spelling file non-fatal

On Thu, Oct 23, 2014 at 10:29 AM, Joe Perches <[email protected]> wrote:
> Commit 66b47b4a9dad ("checkpatch: look for common misspellings")
> made it difficult to use checkpatch via a symlink.
>
> Fix that and make a missing spelling.txt file non-fatal.
> Emit a warning when the spelling.txt file can not be opened.
>
> Reference: http://xkcd.com/1172/
>
> Signed-off-by: Joe Perches <[email protected]>
> Reported-by: Jani Nikula <[email protected]>
> Tested-by: Jani Nikula <[email protected]>

Thanks for fixing this. :)

Reviewed-by: Kees Cook <[email protected]>

-Kees

--
Kees Cook
Chrome OS Security

2014-10-24 09:02:45

by Andy Whitcroft

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: fix use via symlink, make missing spelling file non-fatal

On Thu, Oct 23, 2014 at 10:29:11AM -0700, Joe Perches wrote:

[...]
> my $P = $0;
> -$P =~ s@(.*)/@@g;
> -my $D = $1;
> +my $D = dirname(abs_path($P));

That changes the value of $P, I don't know if that is intended:

my $D = dirname(abs_path($0));

or

my $D = abs_path($1);

perhaps to keep $P as it was.

-apw

2014-10-24 09:31:57

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: fix use via symlink, make missing spelling file non-fatal

On Fri, 2014-10-24 at 10:02 +0100, Andy Whitcroft wrote:
> On Thu, Oct 23, 2014 at 10:29:11AM -0700, Joe Perches wrote:
>
> [...]
> > my $P = $0;
> > -$P =~ s@(.*)/@@g;
> > -my $D = $1;
> > +my $D = dirname(abs_path($P));
>
> That changes the value of $P, I don't know if that is intended:
>
> my $D = dirname(abs_path($0));
>
> or
>
> my $D = abs_path($1);
>
> perhaps to keep $P as it was.

I did it to show the executable passed on the cmdline better.

It changes --help and some warn/die messages.

It's not particularly important either way.

2014-10-24 10:39:35

by Andy Whitcroft

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: fix use via symlink, make missing spelling file non-fatal

On Fri, Oct 24, 2014 at 02:31:50AM -0700, Joe Perches wrote:
> On Fri, 2014-10-24 at 10:02 +0100, Andy Whitcroft wrote:
> > On Thu, Oct 23, 2014 at 10:29:11AM -0700, Joe Perches wrote:
> >
> > [...]
> > > my $P = $0;
> > > -$P =~ s@(.*)/@@g;
> > > -my $D = $1;
> > > +my $D = dirname(abs_path($P));
> >
> > That changes the value of $P, I don't know if that is intended:
> >
> > my $D = dirname(abs_path($0));
> >
> > or
> >
> > my $D = abs_path($1);
> >
> > perhaps to keep $P as it was.
>
> I did it to show the executable passed on the cmdline better.
>
> It changes --help and some warn/die messages.
>
> It's not particularly important either way.

If it was intended then great. Either way is fine with me.

Acked-by: Andy Whitcroft <[email protected]>

-apw