r/programming • • Jun 05 '18

Code golfing challenge leads to discovery of string concatenation bug in JDK 9+ compiler

https://stackoverflow.com/questions/50683786/why-does-arrayin-i-give-different-results-in-java-8-and-java-10
2.2k Upvotes

355 comments sorted by

View all comments

930

u/lubutu Jun 05 '18

Summary: array[i++] += "a" is compiled as array[i++] = array[i++] + "a", which increments i twice.

199

u/Philipp Jun 05 '18

And from the comments there:

"By the way, this applies to the entire left hand side expression, not only the index providing sub-expression. This expression may be arbitrarily complex. See for example IntStream.range(0, 10) .peek(System.out::println).boxed().toArray()[0] += "";"

28

u/ThisIs_MyName Jun 05 '18

Thankfully this bug only works on string references like that. If this happened to integers, that would have been scary. But I guess in that case it would have been discovered much faster thanks to bounds checking.

0

u/[deleted] Jun 05 '18

[deleted] — view deleted comment

154

u/Tarmen Jun 05 '18

Most places where += for String is relevant StringBuilder would be the idiomatic solution. This is because String in java is immutable so a loop like

for (int i = 0; i < n; i++) {
    s += "hi";
}

Has O(no) runtime.

206

u/Herbstein Jun 05 '18

Oh no :D

61

u/landonepps Jun 05 '18

A big oh no!

24

u/eckesicle Jun 05 '18

A O(no!)

12

u/CheezyXenomorph Jun 05 '18

no factorial = nonononononononononononononononononononononono

→ More replies (1)
→ More replies (1)

21

u/mirhagk Jun 05 '18

If it's in a loop yes. But if you're just doing `+=` a couple times then there's no need for StringBuilder. Of course `i++` wouldn't be used there, but that's still very weird that nobody noticed.

→ More replies (1)

37

u/Luvax Jun 05 '18

Isn't this one of these cases in which the Java Runtime will automatically use a StringBuilder even if you didn't?

Edit: Or the compiler, interpreter or which kind of godly entity is actually doing the optimisation.

17

u/Steveadoo Jun 05 '18

Yeah. The compiler will just use StringBuilder for that, but I'm not sure if it will hoist it out of the loop though. I'm pretty sure it will allocate once per iteration.

→ More replies (1)
→ More replies (23)

178

u/moekakiryu Jun 05 '18

tbh I wouldn't be shocked if someone has, but it was probably just written off as some unsolvable bug and they rewrote the script because they couldn't be bothered working out what was causing it

225

u/ClownFundamentals Jun 05 '18

"Hey guys I think there's a bug in the compiler - I'm sure my code is right but it isn't working!"

christ fucking Steve again "Look just try to rewrite it and see if it goes away, k?"

69

u/cmsimike Jun 05 '18

Who hasn't been in that situation!?

32

u/-ghostinthemachine- Jun 05 '18

I've been down that hole about 8 times, but on two occasions it really was a compiler bug! Unfortunately that usually means it won't be fixed in time for your project. I still don't have the chutzpah to actually file a new bug report against a compiler though. ;)

28

u/Ksevio Jun 05 '18

On the rare occasion it's happened to me, I've found someone else has reported it already which is a relief to know you're not insane.

Of course it still sucks if it's marked on the roadmap to be fixed in Java 12 or something

→ More replies (1)
→ More replies (11)

7

u/PM__YOUR__GOOD_NEWS Jun 05 '18

Bugs in the libraries/frameworks/etc. I use are the worst to troubleshoot because 99% of the time the fault is my code so as a rule of thumb I take external dependencies as perfect until proven flawed.

→ More replies (1)
→ More replies (3)

10

u/13steinj Jun 05 '18

On the other hand, I also wouldn't be surprised if it hasn't. Combined with the fact that JDK 9 was a relatively major change from 8 (modules, getting rid of the Observer pattern, so on), I don't think many companies would make the switch. In fact there are still some stuck on 6/7. That combined with

The issue seems to be limited to the string concatenation and assignment operator (+=) with an expression with side effect(s) as the left operand, like in array[test()]+="a", array[ix++]+="a", test()[index]+="a", or test().field+="a"

Makes it seem less likely to occur in production code that needs to readable and more like the OP's situation-- code golf.

→ More replies (2)
→ More replies (1)

6

u/kynovardy Jun 05 '18

I mean when your code doesn't work, do you generally blame the compiler?

→ More replies (2)
→ More replies (21)

65

u/f03nix Jun 05 '18

The example of array[test()] += "a" makes it very clear, test() is called twice.

→ More replies (1)
→ More replies (84)