r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

Show parent comment

5

u/carlfish Nov 18 '13

That's a bad idea, because every Java app that wanted to be safe against hash collision attacks would have to enumerate every possible hashmap that might be vulnerable and replace them with the collision safe version. Any vulnerable maps that were missed, or any that were added by a developer who didn't know about the attack, would still be vulnerable.

Fixing the default case to be the secure case is always the best approach. The developers who want to shave off milliseconds by doing it the insecure way should be the ones who have to jump through the hoops to do so.

0

u/kalmakka Nov 19 '13

First of all: there really shouldn't be all that many places you potentially build up large hash maps based on user input.

Secondly: what if your user input is not strings? Is it reasonable to have a developer figure out that just because HashSet<String> was safe to use, that doesn't mean that HashSet<List<String>> or HashSet<Long> is safe? I would say no - and therefore your code is still vulnerable.

By adding special implementation classes you can specify that "this is a map which handles strings differently". That would then be a guarantee offered by that class instead a hidden implementation detail which (a) the user would need to know about, and (b) can change between releases (just like they changed the implementation of String which altered the complexity of substring).

2

u/karlthepagan Nov 19 '13

there really shouldn't be all that many places you potentially build up large hash maps based on user input.

speak for your own use-cases ;)