Poor code quality leads to unpredictable behavior. From a user's perspective that often manifests itself as poor usability. For an attacker it provides an opportunity to stress the system in unexpected ways.
assert
Solidity function to check on a false statement.assert
Solidity function is intended to only check on statements that evaluate to true
. Having a false
statement passed into this function points at the code not functioning correctly or the function being misused, for instance, to validate input.assert
function to check on a false statement.
contract A {
B b = new B(7);
function checkWithAssert(){
assert(b.retValue() == 21);
...
}
}
contract B {
uint _par;
constructor(uint par){
_par = par;
}
function retValue() returns(uint){
return _par;
}
}
getChunk == NULL
will always be false because getChunk
is the name of a function defined in the program.
if (getChunk == NULL)
return ERR;
char* getName() {
char name[STR_MAX];
fillInName(name);
return name;
}
class AccessLevel{
public static final int ROOT = 0;
//...
public static final int NONE = 9;
}
//...
class User {
private static int access;
public User(){
access = AccessLevel.ROOT;
}
public static int getAccessLevel(){
return access;
}
//...
}
class RegularUser extends User {
private static int access;
public RegularUser(){
access = AccessLevel.NONE;
}
public static int getAccessLevel(){
return access;
}
public static void escalatePrivilege(){
access = AccessLevel.ROOT;
}
//...
}
//...
class SecureArea {
//...
public static void doRestrictedOperation(User user){
if (user instanceof RegularUser){
if (user.getAccessLevel() == AccessLevel.ROOT){
System.out.println("doing a privileged operation");
}else{
throw new RuntimeException();
}
}
}
}
getAccessLevel()
against the instance user
and not against the classes User
or RegularUser
, it will mean that this condition will always return true
, and the restricted operation will be performed even though instanceof
was used in order to get into this part of the if/else
block.serialPersistentFields
correctly, it must be declared private
, static
, and final
.serialPersistentFields
array. This feature will only work if serialPersistentFields
is declared as private
, static
, and final
.serialPersistentFields
will not be used to define Serializable
fields because it is not private
, static
, and final
.
class List implements Serializable {
public ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("myField", List.class) };
...
}
Object.equals()
on an array instead of java.util.Arrays.equals().
Object.equals()
against an array is a mistake in most cases, since this will check the equality of the arrays' addresses, instead of the equality of the arrays' elements, and should usually be replaced by java.util.Arrays.equals()
.Object.equals()
function.
...
int[] arr1 = new int[10];
int[] arr2 = new int[10];
...
if (arr1.equals(arr2)){
//treat arrays as if identical elements
}
...
pthread_cleanup_push()
to push the function routine
onto the calling thread's cleanup stack and returns. Since pthread_cleanup_push()
and its partner function pthread_cleanup_pop()
are implemented as macros on platforms other than IBM AIX, the data structure created by pthread_cleanup_push()
will not be accessible to subsequent calls to pthread_cleanup_pop()
. The code will either fail to compile or behave incorrectly at runtime on all platforms where these functions are implemented as macros.
void helper() {
...
pthread_cleanup_push (routine, arg);
}
void clean_up()
{
char tmp[256];
...
free(tmp);
return;
}
System.Object.Equals()
:
public boolean Equals(string obj) {
...
}
System.Object.Equals()
takes an argument of type object
, the method is never called.Object.equals()
:
public boolean equals(Object obj1, Object obj2) {
...
}
Object.equals()
only takes a single argument, the method in Example 1
is never called.ISerializable
interface but do not declare the [Serializable]
attribute will not be serialized.[Serializable]
attribute. If the class can be serialized using the default serialization methods defined by the .NET framework, this is both necessary and sufficient for the object to be correctly serialized. If the class requires custom serialization methods, it must also implement the ISerializable
interface. However, the class must still declare the [Serializable]
attribute.CustomStorage
class implements the ISerializable
interface. However, because it fails to declare the [Serializable]
attribute, it will not be serialized.
public class CustomStorage: ISerializable {
...
}
java.io.Serializable
may cause problems and leak information from the outer class.
...
class User implements Serializable {
private int accessLevel;
class Registrator implements Serializable {
...
}
}
Example 1
, when the inner class Registrator
is serialized, it will also serialize the field accessLevel
from the outer class User
.synchronized
, guaranteeing correct behavior when multiple threads access the same instance. All overriding methods should also be declared synchronized
, otherwise unexpected behavior may occur.Foo
overrides the class Bar
but does not declare the method synchronizedMethod
to be synchronized
:
public class Bar {
public synchronized void synchronizedMethod() {
for (int i=0; i<10; i++) System.out.print(i);
System.out.println();
}
}
public class Foo extends Bar {
public void synchronizedMethod() {
for (int i=0; i<10; i++) System.out.print(i);
System.out.println();
}
}
Foo
could be cast to type Bar
. If the same instance is given to two separate threads and synchronizedMethod
is executed repeatedly, the behavior will be unpredictable.obj.Equals(null)
should always be false.Equals()
method to compare an object with null
. The contract of the Equals()
method requires this comparison to always return false.obj.equals(null)
will always be false.equals()
method to compare an object with null
. This comparison will always return false, since the object is not null
. (If the object is null
, the program will throw a NullPointerException
).pthread_create()
from the main()
function of the parent process will be terminated prematurely if the parent process finishes execution before any the threads it has spawned without calling pthread_exit()
. Calling pthread_exit()
guarantees that the parent process will be kept alive until all of its threads have finished execution. Alternatively, the parent process can call pthread_join
on all of the child threads and ensure they will complete before the process concludes.pthread_create()
to create a thread and then exits normally. If the child thread has not completed its execution by the time the main()
function returns, then it will be terminated prematurely.
void *Simple(void *threadid)
{
...
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
int rc;
pthread_t pt;
rc = pthread_create(&pt, NULL, Simple, (void *)t);
if (rc){
exit(-1);
}
}
readObject()
method within the class calls a function that may be overridden.readObject()
acts like a constructor, so object initialization is not complete until this function ends. Therefore when a readObject()
function of a Serializable
class calls an overridable function, this may provide the overriding method access to the object's state prior to it being fully initialized.readObject()
function calls a method that can be overridden.
...
private void readObject(final ObjectInputStream ois) throws IOException, ClassNotFoundException {
checkStream(ois);
ois.defaultReadObject();
}
public void checkStream(ObjectInputStream stream){
...
}
checkStream()
and its enclosing class are not final
and public, it means that the function can be overridden, which may mean that an attacker may override the checkStream()
function in order to get access to the object during deserialization.private readonly
list variable from a getter-only property allows the calling code to modify the contents of the list, effectively giving the list write access and contradicting the intentions of the programmer who made it private readonly
._item
which is declared as private readonly
.
class Order
{
private readonly List<string> _item = new List<string>();
public IEnumerable<string> Item { get { return _item; } }
public Order()
{
/*class initialize */
}
/*some important function......*/
}
msg.sender.call.value
(Interaction).
function withdraw(uint amount) public{
if (credit[msg.sender] >= amount) {
require(msg.sender.call.value(amount)());
credit[msg.sender]-=amount;
}
}
Example 1
breaks the Checks-Effects-Interaction pattern by doing Checks-Interaction-Effects instead. This can lead to reentrancy if an attacking smart contract receives the Ether in a fallback function and immediately calls back withdraw
, creating a recursive situation where Ether is drained because the line of code that decreases the balance (aka the Effect) never gets executed.
Marker child = MarkerManager.getMarker("child");
Marker parent = MarkerManager.getMarker("parent");
child.addParents(parent);
parent.addParents(child);
String toInfinity = child.toString();
toString()
, which includes a recursive processing method, it triggers a stack overflow exception (stack exhaustion). This exception occurs due to the circular link between child and parent.String
object is unreliable and should not be done.String
object, it must first be changed into a String
object, typically via a function such as Double.toString()
. Depending on the type and value of the floating-point variable, when converted to a String
object, it could be "NaN", "Infinity", "-Infinity", have a certain amount of trailing decimal places containing zeroes, or may contain an exponent field. If converted to a hexadecimal String, the representation may differ greatly as well.String
.
...
int initialNum = 1;
...
String resultString = Double.valueOf(initialNum/10000.0).toString();
if (s.equals("0.0001")){
//do something
...
}
...