You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 32 Next »

According to the Java Language Specification [[JLS 05]] section 11.2 "Compile-Time Checking of Exceptions":

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.

Unchecked exception classes such as Error and its subclasses are not subject to compile time checking as it is tedious to account for all exceptional conditions and recovery is generally difficult. However, most often recovery is not impossible, or at least a graceful exit that logs the error is feasible.

Noncompliant Code Example

This noncompliant code example generates a StackOverflowError as a result of infinite recursion. This exhausts the available stack space and may result in denial of service.

public class StackOverflow {
  public static void main(String[] args) {
    infiniteRun();
    System.out.println("Continuing...");
  }
    
  private static void infiniteRun() {
    infiniteRun();
  }
}

Compliant Solution

This compliant solution shows a try-catch block that can be used to capture java.lang.Error or java.lang.Throwable. A log entry can be made at this point followed by attempts to free key system resources in the finally block.

public class StackOverflow {
  public static void main(String[] args) {
    try {
     infiniteRun();
    } catch(Throwable t) {
      System.out.println("Handling error...");
      // Log error to file
    } finally {
      // Free cache, release resources
    }
    System.out.println("Continuing...");
  }
    
  private static void infiniteRun() {
    infiniteRun();    
  }
}

Note that this solution catches Throwable in an attempt to handle the error and is an exception to EXC14-J. Catch specific exceptions as opposed to the more general RuntimeException.

Risk Assessment

Allowing a system error to propagate out of a Java program may result in a denial-of-service attack.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXC08- J

low

unlikely

medium

P2

L3

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C++ Secure Coding Standard as ERR30-CPP. Try to recover gracefully from unexpected errors.

References

[[JLS 05]] Section 11.2, Compile-Time Checking of Exceptions
[[Kalinovsky 04]] Chapter 16, Intercepting Control Flow - Intercepting System Errors


EXC07-J. Prevent exceptions while logging data      13. Exceptional Behavior (EXC)      EXC09-J. Prevent inadvertent calls to System.exit() or forced shutdown

  • No labels