check-format.pl: Add check for constant left of comparison operator
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Wed, 28 Apr 2021 19:45:07 +0000 (21:45 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Thu, 20 May 2021 14:29:13 +0000 (16:29 +0200)
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15077)

util/check-format-test-negatives.c
util/check-format-test-positives.c
util/check-format.pl

index c9f77ecf6ceb8ef6ad61b8ba0b172c901124d399..01216718fd8d7a0c9249d8a82c4afc319460733c 100644 (file)
@@ -150,6 +150,10 @@ int f(void) /*
         hanging_stmt;
 }
 
+/* should not trigger: constant on LHS of comparison or assignment operator */
+X509 *x509 = NULL;
+int y = a + 1 < b;
+
 const OPTIONS passwd_options[] = {
     {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
index 5174a2b530f36eb427c89cd4d72616ee3fe49b7c..89dcf073cee73e0501b1fe8915fd083bac277d8f 100644 (file)
@@ -75,7 +75,8 @@ int f (int a,       /*@ space after fn before '(', reported unless sloppy-spc */
        long l)      /*@ one-letter name 'l' */
 { int               /*@ code after '{' opening a block */
     xx = 1) +       /*@ unexpected closing parenthesis */
-        2] -        /*@ unexpected closing bracket */
+        0L <        /*@ constant on LHS of comparison operator */
+        a] -        /*@ unexpected closing bracket */
         3: *        /*@ unexpected ':' (without preceding '?') within expr */
         4};         /*@ unexpected closing brace within expression */
     char y[] = {    /*@0 unclosed brace within initializer/enum expression */
@@ -91,7 +92,7 @@ int f (int a,       /*@ space after fn before '(', reported unless sloppy-spc */
            b,       /*@ expr indent as on line above, accepted if sloppy-hang */
     b, /*@ expr indent off -8 but @ extra indent accepted if sloppy-hang */
    "again aligned" /*@ expr indent off by -9 (left of stmt indent, */ "right",
-            123 == /*@ .. so reported also with sloppy-hang; this line is too long */ 456
+            abc == /*@ .. so reported also with sloppy-hang; this line is too long */ 456
 # define MAC(A) (A) /*@ nesting indent of preprocessor directive off by 1 */
              ? 1    /*@ hanging expr indent off by 1 */
               : 2); /*@ hanging expr indent off by 2, or 1 for leading ':' */
index 2a9adc6fb8bb19a39099047b035931286c2d0948..481eda8b36d9f5e1811aa7db9ff50e1646a016b0 100755 (executable)
@@ -846,7 +846,12 @@ while (<>) { # loop over all lines of all input files
         }
     }
 
-    report("one-letter name '$2'") if (m/(^|.*\W)([lIO])(\W.*|$)/); # single-letter name 'l', 'I', or 'O'
+    report("single-letter name '$2'") if (m/(^|.*\W)([IO])(\W.*|$)/); # single-letter name 'I' or 'O' # maybe re-add 'l'?
+    # constant on LHS of comparison or assignment, e.g., NULL != x or 'a' < c, but not a + 1 == b
+    report("constant on LHS of '$2'")
+        if (m/(['"]|([\+\-\*\/\/%\&\|\^<>]\s*)?\W[0-9]+L?|NULL)\s*([\!<>=]=|[<=>][^<>])/ && $2 eq "");
+
+    # TODO report #if 0 and #if 1
 
     # TODO report empty line within local variable definitions