...
Code Block | ||
---|---|---|
| ||
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; } |
The conditions condition checked by the assertions are reasonableJava assertion is reasonable. However, the validation code is not executed when assertions are disabled.
...
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 | Probable | Medium | P8 | L2 |
Related Guidelines
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
Item 7, "My Assertions Are Not gratuitous" | |
[ESA 2005] | 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 |
[JLS 2015] |
...
...