Skip to content

Commit 15662b3

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add a --strict check for utf-8 in commit logs
Some find using utf-8 in commit logs inappropriate. Some patch commit logs contain unintended utf-8 characters when doing things like copy/pasting compilation output. Look for the start of any commit log by skipping initial lines that look like email headers and "From: " lines. Stop looking for utf-8 at the first signature line. Signed-off-by: Joe Perches <joe@perches.com> Suggested-by: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 67d0a07 commit 15662b3

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

scripts/checkpatch.pl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ sub help {
240240
our $Type;
241241
our $Declare;
242242

243-
our $UTF8 = qr {
244-
[\x09\x0A\x0D\x20-\x7E] # ASCII
245-
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
243+
our $NON_ASCII_UTF8 = qr{
244+
[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
246245
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
247246
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
248247
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
@@ -251,6 +250,11 @@ sub help {
251250
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
252251
}x;
253252

253+
our $UTF8 = qr{
254+
[\x09\x0A\x0D\x20-\x7E] # ASCII
255+
| $NON_ASCII_UTF8
256+
}x;
257+
254258
our $typeTypedefs = qr{(?x:
255259
(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
256260
atomic_t
@@ -1330,6 +1334,9 @@ sub process {
13301334
my $signoff = 0;
13311335
my $is_patch = 0;
13321336

1337+
my $in_header_lines = 1;
1338+
my $in_commit_log = 0; #Scanning lines before patch
1339+
13331340
our @report = ();
13341341
our $cnt_lines = 0;
13351342
our $cnt_error = 0;
@@ -1497,7 +1504,6 @@ sub process {
14971504
if ($line =~ /^diff --git.*?(\S+)$/) {
14981505
$realfile = $1;
14991506
$realfile =~ s@^([^/]*)/@@;
1500-
15011507
} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
15021508
$realfile = $1;
15031509
$realfile =~ s@^([^/]*)/@@;
@@ -1536,6 +1542,7 @@ sub process {
15361542
# Check the patch for a signoff:
15371543
if ($line =~ /^\s*signed-off-by:/i) {
15381544
$signoff++;
1545+
$in_commit_log = 0;
15391546
}
15401547

15411548
# Check signature styles
@@ -1613,6 +1620,21 @@ sub process {
16131620
"Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
16141621
}
16151622

1623+
# Check if it's the start of a commit log
1624+
# (not a header line and we haven't seen the patch filename)
1625+
if ($in_header_lines && $realfile =~ /^$/ &&
1626+
$rawline !~ /^(commit\b|from\b|\w+:).+$/i) {
1627+
$in_header_lines = 0;
1628+
$in_commit_log = 1;
1629+
}
1630+
1631+
# Still not yet in a patch, check for any UTF-8
1632+
if ($in_commit_log && $realfile =~ /^$/ &&
1633+
$rawline =~ /$NON_ASCII_UTF8/) {
1634+
CHK("UTF8_BEFORE_PATCH",
1635+
"8-bit UTF-8 used in possible commit log\n" . $herecurr);
1636+
}
1637+
16161638
# ignore non-hunk lines and lines being removed
16171639
next if (!$hunk_line || $line =~ /^-/);
16181640

0 commit comments

Comments
 (0)