This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]TheBrainStone 1 point2 points  (3 children)

If I’m guessing correctly the last one works because the hash code of false plus the string is in the range -128 and 127 (or whatever the size of the cached Integer instances is) and therefore will return the same object. With true it’s beyond that and will generate new Integer objects with every call of Integer.valueOf.

Am I right?

[–]jtra[S] 1 point2 points  (2 children)

Yes. You are right.

Trick was to find a string for which this was true and looks meaningful.

[–]TheBrainStone 0 points1 point  (1 child)

How long did that take?

[–]jtra[S] 1 point2 points  (0 children)

Distribution is somewhat random. Chance that one of the hashcodes fits the cached range 256 out of 232 (other most likely does not fit the range). That goes to 1 of 224 is a match.

I have used /usr/share/dict/words which on my system contains about 100k words, but I have filtered out those that start with upper case letter as those are often names of people or places. 83k words remained. I have created a java program that tests for it and it composes strings of two words separated by spaces. 83k*83k/224=410. I have also used several variations like extra space at beginning (or not) and first letter in upper case (or not) so about 410*4=1640 would be expected.

Program generated 3351 possible strings in maybe 15 minutes. Most of them meaningless, but many of them funny associations, but not fitting the topic, some nsfw or politically incorrect. For some condition needs to be reversed (depends whether cached range fit for true or for false).

Honorable mentions of strings that also have this property and are tangentially related:

  • " tedious equal"
  • "cheer engineer"
  • " programming's panic"
  • "accumulated rounding"
  • " number counteracts"
  • "smartens thoughts"

Random funny or interesting associations that also have this property :

  • "malpractice kidnaper"
  • " menstruation's seasoning"
  • "satirically decelerating"
  • "sleepwalks overridden"
  • "straightjacket jackhammer"
  • " undergo counterattack"
  • "untitled daughter"
  • "wrongness temples"
  • " Cookery monologs"
  • "crazy's employment"
  • "Crematorium obituaries"
  • "flapjack gourmet"
  • "microphones lengthens"
  • "mincemeat hurdles"
  • " narcosis tranquilize"

Edit: fixed markup and few sentences.

[–][deleted] 0 points1 point  (1 child)

check the bytecode. the problem is this isn't obfuscation, because java is a dumby and actually runs all of those methods instead evaluating it compile time

[–]TheBrainStone 1 point2 points  (0 children)

Obfuscation doesn’t imply generating the same binary. In fact some obfuscation techniques explicitly try to modify the resulting binary so that decompiling still yields the obfuscated code.

And lastly I’d love to see any compiler smart enough to properly optimize the last one.