The following commit has been merged in the master branch:
commit 7bab3e6f319e1031e42a681c96aaa7799e60f622
Author: Adam D. Barratt <[email protected]>
Date:   Fri Jan 2 16:57:22 2009 +0000

    Make the removal of comments from shell script lines more intelligent
    
    + [ADB] More intelligently remove comments from shell scripts, to avoid
      removing some constructs that aren't comments, such as those in
      [ $# -gt 2 ] and "foo # bar".

diff --git a/checks/scripts b/checks/scripts
index 3a9becc..146249c 100644
--- a/checks/scripts
+++ b/checks/scripts
@@ -343,7 +343,7 @@ for my $filename (sort keys %{$info->scripts}) {
        while (<FH>) {
            next if m,^\s*$,;  # skip empty lines
            next if m,^\s*\#,; # skip comment lines
-           s/\#.*$//;         # eat comments
+           $_ = remove_comments($_);
            chomp;
 
            # Check for running init scripts directly instead of via
@@ -540,7 +540,7 @@ while (<SCRIPTS>) {
 
        next if m,^\s*$,;  # skip empty lines
        next if m,^\s*\#,; # skip comment lines
-       s/\#.*$//;         # eat comments
+       $_ = remove_comments($_);
 
        # Concatenate lines containing continuation character (\) at the end
        if ($shellscript && /\\$/) {
@@ -976,6 +976,30 @@ sub check_script_syntax {
     return $?;
 }
 
+sub remove_comments {
+    local $_;
+
+    my $line = shift || '';
+    $_ = $line;
+
+    # Remove quoted strings so we can more easily ignore comments
+    # inside them
+    s/(^|[^\\](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
+    s/(^|[^\\](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
+ 
+    # If the remaining string contains what looks like a comment,
+    # eat it. In either case, swap the unmodified script line
+    # back in for processing (if required) and return it.
+    if (m/(?:^|[^[\\])[\s\&;\(\)](\#.*$)/) {
+       $_ = $line;
+       s/\Q$1\E//;  # eat comments
+    } else {
+       $_ = $line;
+    }
+
+    return $_;
+}
+
 1;
 
 # Local Variables:
diff --git a/debian/changelog b/debian/changelog
index d886b9e..48f2e3a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -90,6 +90,9 @@ lintian (2.1.4) UNRELEASED; urgency=low
       without using invoke-rc.d.  Based on a patch by Raphael Geissert.
       (Closes: #381485)
     + [RA] Allow dpkg-dev to satisfy a make dependency.  (Closes: #510190)
+    + [ADB] More intelligently remove comments from shell scripts, to avoid
+      removing some constructs that aren't comments, such as those in
+      [ $# -gt 2 ] and "foo # bar".
   * checks/watch-file:
     + [RA] Recognize additional SourceForge URLs and recommend the
       redirector.  Thanks, Riccardo Stagni.  (Closes: #510398)
diff --git a/testset/maintainer-scripts/debian/postinst 
b/testset/maintainer-scripts/debian/postinst
index d840aa5..c4a5c4b 100644
--- a/testset/maintainer-scripts/debian/postinst
+++ b/testset/maintainer-scripts/debian/postinst
@@ -171,3 +171,12 @@ case $mainscript in
 esac
 
 /usr/bin/baz; echo bar
+
+# Some comment checking
+# This should flag $RANDOM
+test $# -gt 2 && echo $RANDOM
+# But these shouldn't
+test 1=1 # echo $RANDOM
+(test 1=1)# echo $RANDOM
+test 1=1;# echo $RANDOM
+backgroundtask &#echo $RA
diff --git a/testset/tags.maintainer-scripts b/testset/tags.maintainer-scripts
index efc8acb..314a269 100644
--- a/testset/tags.maintainer-scripts
+++ b/testset/tags.maintainer-scripts
@@ -69,9 +69,11 @@ W: maintainer-scripts: possible-bashism-in-maintainer-script 
postinst:156 'test
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:157 
'$RANDOM'
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:16 '. 
/usr/share/lintian/shell foo'
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:17 '. 
"$(dirname $0)/shell"'
+W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:177 
'$RANDOM'
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:20 'read'
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:22 
'H[0]='
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:23 
'${H[0]}'
+W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:24 
'${#H[0]}'
 W: maintainer-scripts: possible-bashism-in-maintainer-script postinst:25 
'$...@]}'
 W: maintainer-scripts: possible-bashism-in-maintainer-script prerm:102 '  echo 
"abc\nxyz"'
 W: maintainer-scripts: possible-bashism-in-maintainer-script prerm:103 '  echo 
'xyz\rabc''

-- 
Debian package checker


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to