Java


JAVA.STRUCT.BW.ORC : Bitwise OR on Boolean Constant (Java)

Summary

An | operation operates on a Boolean constant.

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.

Properties

Class Name Bitwise OR on Boolean Constant (Java)
Significance reliability
Mnemonic JAVA.STRUCT.BW.ORC
Categories
CWE CWE:480 Use of Incorrect Operator
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Bitwise OR on Boolean Constant (Java)"

Example

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);
      // ...
  }
}

Resolution

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

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.