JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.STRUCT.BW.AND : Bitwise AND on Boolean (Java)

要旨

There is a suspicious use of & instead of &&.

Java has a bitwise and a logical-AND operation on Booleans, that is, & and &&. Similarly, Java has a bitwise and a logical-OR operation | and ||. The difference is that the logical operations have a short circuit semantics, that is, if the evaluation of the left-hand side is enough to determine the outcome of the operation, then the right-hand side is not evaluated; the bitwise operations, instead, evaluate both sides, always, which might be incorrect is most cases, or at least inefficient.

プロパティ

クラス名 Bitwise AND on Boolean (Java)
日本語クラス名 Bitwise AND on Boolean (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.STRUCT.BW.AND
カテゴリー
CWE CWE:768 Incorrect Short Circuit Evaluation
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Bitwise AND on Boolean (Java)"

public class ShortCircuitExamples {
  public static void test1(String[] args) {
      if (args.length == 0 | isOption(args[0]))     /* Bitwise OR on Boolean (Java) warning issued here. 
                                                     * IsOption(args[0]) is always evaluated, even if args.Length==0 evaluates to TRUE, 
                                                     * but if args.Length==0 then IsOption(args[0]) results in an ArrayIndexOutOfBoundsException. 
                                                     */ 
          System.out.println("option expected");
      // ...
  }

  public static void test2(String[] args) {
      if (args.length == 1 & isOption(args[0]))     /* Bitwise AND on Boolean (Java) warning issued here. 
                                                     * IsOption(args[0]) is always evaluated, even if args.Length==1 evaluates to FALSE, 
                                                     * including in the case where args.Length==0 and IsOption(args[0]) results in an ArrayIndexOutOfBoundsException. 
                                                     */ 
           System.out.println("option expected");
      // ...
   }

  private static boolean isOption(String s) {
      return s.equals("option0");
  }

  public void Test3(boolean a) {
      bool x = true;
      if (x & a)                                     // Bitwise AND on Boolean Constant (Java) warning issued here.
          System.out.println("hello");
      // ...
  }

  public static void Test4(boolean a, String s) {
      bool x = false;
      if (x | a)                                    // Bitwise OR on Boolean Constant (Java) warning issued here.
          System.out.println("hello");
      // ...
  }

  public static void Test5(boolean a, String s) {
      a &= isOption(s);                             // Inefficient Bitwise AND (Java) warning issued here.
      // ...
   }

  public static void Test6(boolean b, String s) {
      b |= isOption(s);                             // Inefficient Bitwise OR (Java) warning issued here. 
      // ...
  }
}

In this example, the programmer should rework the program as follows.

// ShortCircuitExamples.java, after modification 
public class ShortCircuitExamples {
  public static void test1(String[] args) {
      if (args.length == 0 || isOption(args[0]))
          System.out.println("option expected");
      // ...
  }

  public static void test2(String[] args) {
      if (args.length == 1 && isOption(args[0]))
          System.out.println("option expected");
      // ...
  }

  private static boolean isOption(String s) {
      return s.equals("option0");
  }

  public void test3(boolean a) {
      bool x = true;
      if (a)
          System.out.println("hello");
      // ...
  }

  public static void test4(boolean a, string s) {
      bool x = false;

      if (a)
          System.out.println("hello");
        // ...
  }

  public static void test5(boolean a, String s) {
      a = a && isOption(s);
      // ...
  }

  public static void test6(boolean b, String s) {
      b = b || isOption(s);
      // ...
  }
}

解決法

Use the logical (short-circuit) version of the operators on Booleans.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.