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.SE.ASSERT : Assertion Contains Side Effects (Java)

Summary

An assertion checks a condition with side-effects.

If an assertion has side effects, the semantics of the program will change depending on whether or not assertions are enabled.

Side-effects are updates to heap memory, such as variable, field or array assignment operations. They are essential in object-oriented code. However, there are situations where side-effects are unexpected or suspicious and should be avoided. This checker identifies unexpected or suspicious side-effects.

Properties

Class Name Assertion Contains Side Effects (Java)
Significance reliability
Mnemonic JAVA.STRUCT.SE.ASSERT
Categories
CWE CWE:665 Improper Initialization
CERT-Java CERT-Java:DCL00-J Prevent class initialization cycles
  CERT-Java:EXP06-J Expressions used in assertions must not produce side effects
Availability Available for Java and Kotlin.
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="Assertion Contains Side Effects (Java)"

Example

The assert statements in the following code fragment check expressions whose evaluations have side effects: each checked expression updates a variable or field.

public class AssertionsWithSideEffects {
  public void assert1() {
      Object obj = null;
      assert (obj = new Object()) != null;       // "Assertion Contains Side Effects (Java)" warning issued here
  }

  public void assert2() {
      assert (list = new ArrayList<>()) != null; // "Assertion Contains Side Effects (Java)" warning issued here
  }

  public void assert3() {
      assert counter++ > 17;                     // "Assertion Contains Side Effects (Java)" warning issued here
  }

  public List<String> list = new ArrayList<>();
  private static int counter;
}

There are generally two options for resolving a situation where an assertion contains a side effect: either remove the side effect entirely, or move it outside the assert statement. For example, method assert1() could be rewritten as follows.

public void assert1() {
    Object obj = new Object();
    assert obj != null;
}

Resolution

Check if the side-effect is actually wrong and avoid it, for instance by moving the expression with side-effect into a separate, isolated statement.

Relevant Configuration File Parameters

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

 

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