Are these vulnerable to DoS (StackOverflow) via comparison? [code] and [code] and [code]
- User
Are these vulnerable to DoS (StackOverflow) via comparison?
codepublic class Test1 { final int id; final String name; public Test1(int id, String name) { this.id = id; this.name = name; } @Override public boolean equals(Object o) { return equals(this, o); } public boolean equals(Object a, Object b) { if (a == null || b == null) { return false; } if(Test1.class != a.getClass() || Test1.class != b.getClass()){ return false; } if(((Test1) a).id != ((Test1) b).id || !((Test1) a).name.equals(((Test1) b).name)){ return false; } return (a.equals(b) || a == b); } }and
codepublic class Test2 { final int id; final String name; public Test2(int id, String name) { this.id = id; this.name = name; } @Override public boolean equals(Object o) { return equals(o, this); } public boolean equals(Object a, Object b) { if (a == null || b == null) { return false; } if(Test2.class != a.getClass() || Test2.class != b.getClass()){ return false; } if(((Test2) a).id != ((Test2) b).id || !((Test2) a).name.equals(((Test2) b).name)){ return false; } return (a.equals(b) || a == b); } }and
codepublic class Test3 { final int id; final String name; public Test3(int id, String name) { this.id = id; this.name = name; } @Override public boolean equals(Object o) { return equals(o, this); } public boolean equals(Object a, Object b) { if (a == null || b == null) { return false; } if(Test3.class != a.getClass() || Test3.class != b.getClass()){ return false; } if(((Test3) a).id != ((Test3) b).id || !((Test3) a).name.equals(((Test3) b).name)){ return false; } return ( a == b || a.equals(b)); } } - Assistant
All three implementations are vulnerable to a potential stack overflow caused by recursive calls to the
equals()method. This is because each implementation calls another version of theequals()method with the arguments in reverse order, which can result in an infinite loop of recursive calls if the two objects being compared also have this implementation ofequals().For example, in
Test1andTest2, theequals()method callsequals(this, o)andequals(o, this), respectively, which can cause an infinite loop if both objects have the same implementation of . Similarly, in , the method calls and , which can also cause an infinite loop if both objects have the same implementation of .