If the purpose of a store is intentionally to overwrite data, for example when a password is being removed from memory, dead store optimizations can cause the write not to happen, leading to a security issue.[1] Some system libraries have specific functions designed to avoid such dangerous optimizations, e.g. explicit_bzero
on OpenBSD.[2]
// DeadStoreExample.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DeadStoreExample {
public static void main(String[] args) {
List<String> list = new ArrayList<String>(); // This is a Dead Store, as the ArrayList is never read.
list = getList();
System.out.println(list);
}
private static List<String> getList() {
return new ArrayList<String>(Arrays.asList("Hello"));
}
}
function func(a, b) {
var x;
var i = 300;
while (i--) {
x = a + b; // dead store
}
}
The code in the loop repeatedly overwrites the same variable, so it can be reduced to only one call.[3]