Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 36

Wiki Markup Never use assertions to validate arguments of public methods. According to the _Java Language Specification_ , [§14 The Java Language Specification, §14.10, "The {{assert}} Statement"|http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10] \[[JLS 2005|AA. Bibliography#JLS 05]\]: [JLS 2015], states that

...assertions should not be used for argument - checking in public methods. Argument - checking is typically part of the contract of a method, and this contract must be upheld whether assertions are enabled or disabled.

Another A secondary problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime run-time exception (such as IllegalArgumentException, IndexOutOfBoundsException, or NullPointerException). An assertion failure will not throw an appropriate exception.

...

The method getAbsAdd() computes and returns the sum of the absolute value of parameters x and y. It lacks argument validation, in violation of rule MET00-J. Validate method arguments. Consequently, it can produce incorrect results because of integer overflow or when either or both of its arguments are Integer.MIN_VALUE.

Code Block
bgColor#FFcccc

public static int getAbsAdd(int x, int y) {
  return Math.abs(x) + Math.abs(y);
}
getAbsAdd(Integer.MIN_VALUE, 1);

...

This noncompliant code example uses assertions to validate arguments of a public method.:

Code Block
bgColor#FFcccc

public static int getAbsAdd(int x, int y) {
  assert x != Integer.MIN_VALUE;
  assert y != Integer.MIN_VALUE;
  int absX = Math.abs(x);
  int absY = Math.abs(y);
  assert (absX <= Integer.MAX_VALUE - absY);
  return absX + absY;
}

...

This compliant solution validates the method arguments by ensuring that values passed to Math.abs() exclude Integer.MIN_VALUE and also by checking for integer overflow. :

Code Block
bgColor#ccccff

public static int getAbsAdd(int x, int y) {
  if (x == Integer.MIN_VALUE || y == Integer.MIN_VALUE) {
    throw new IllegalArgumentException();
  }
  int absX = Math.abs(x);
  int absY = Math.abs(y);
  if (absX > Integer.MAX_VALUE - absY) {
    throw new IllegalArgumentException();
  }
  return absX + absY;
}

Alternatively, the addition could be performed using type long and the result of the addition stored in a local variable of type long. This alternate implementation would require a check to ensure that the resulting long can be represented in the range of the type int. Failure of this latter check would indicate that an int version of the addition would have overflowed.

Risk Assessment

Failure Using assertions to validate method arguments can result in inconsistent computations, runtime exceptions, and control flow vulnerabilities.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MET01-J

medium Medium

probable Probable

medium Medium

P8

L2

Related Guidelines

MITRE CWE

CWE-617. , Reachable assertion Assertion

Bibliography

Android Implementation Details

The assert statement is supported on the Dalvik VM but is ignored under the default configuration. Assertions may be enabled by setting the system property debug.assert via adb shell setprop debug.assert 1 or by sending the command-line argument --enable-assert to the Dalvik VM.

Bibliography

[Daconta 2003]

Item 7, "My Assertions Are Not gratuitous"

[ESA 2005]

Rule 68,

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7aad04c3-7c1e-4827-8ff8-195bf1d72d7d"><ac:plain-text-body><![CDATA[

[[Daconta 2003

AA. Bibliography#Daconta 03]]

Item 7. My assertions are not gratuitous

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="51e69d4a-e8d0-4fc8-9f36-ddacfcdf95d8"><ac:plain-text-body><![CDATA[

[[ESA 2005

AA. Bibliography#ESA 05]]

Rule 68. Explicitly check method parameters for validity, and throw an adequate exception in case they are not valid. Do not use the assert statement for this purpose]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6a300955-0a1d-4729-a4d1-bfa6345dd51e"><ac:plain-text-body><![CDATA[

[[JLS 2005AA. Bibliography#JLS 05]] JLS 2015]

§14.10, "The assert Statement

]]></ac:plain-text-body></ac:structured-macro>

"

 

...

Image Added Image Added Image Removed      05. Methods (MET)