r/programming • • May 10 '18

Java: Executing code in comments?!

http://programming.guide/java/executing-code-in-comments.html
260 Upvotes

84 comments sorted by

48

u/flaghacker_ May 10 '18

Isn't this just a chapter completely ripped from the Java Puzzlers book?

2

u/duckvimes_ May 11 '18

There are many gotchas on this theme and Java Puzzlers by Joshua Bloch and Neal Gafter included the following variant

1

u/flaghacker_ May 11 '18

Looks like that and the quote blocks were edited in after the fact.

163

u/kking254 May 10 '18

Actually executing code NOT in comments.

20

u/KnightMiner May 10 '18

Well, maybe we should say executing code no longer in comments, since it was in a comment at one time in its life

4

u/stronglikedan May 10 '18 edited May 10 '18

Since it was typed after the newline character characters, it was never in the comment. Comment slashes --> space (the comment) --> newline --> code

20

u/KnightMiner May 10 '18

Except technically it was not a newline character when it was typed, it was a collection of characters to be replaced with a newline.

1

u/stronglikedan May 10 '18

Fixed my previous comment. It was intended to represent a new line, same as pressing the enter key, so it was intended to end the comment. Therefore, the code was never part of the comment, as the author intended.

4

u/[deleted] May 11 '18

The code was after // and before a newline so yes it was IN comments.

7

u/kking254 May 11 '18

The code was after // then after a newline so no it wasn't IN comments.

FTFY

-1

u/[deleted] May 11 '18

I don't know if you know this but // \u000d is literally 9 characters and none of them has a decimal value of 10

3

u/coladict May 11 '18

I know you don't know this, but the article explains exactly why you're wrong! You would have known if you bothered to even skim it beyond the actual code in question.

-2

u/[deleted] May 11 '18

If you can read you'd understand I am saying the code is literally in the comment. I have other comments stating why it makes zero sense to decode the above and shows they're doing it wrong

3

u/recursive May 11 '18

Java compilers follow a specification, not what reddit posters say makes sense.

0

u/[deleted] May 12 '18

I'm not saying Java is doing something other than what it does. I'm saying not only does it make no sense it's doing what it is doing poorly and wrong

2

u/recursive May 12 '18

Correctness is defined by the spec.

0

u/[deleted] May 12 '18 edited May 12 '18

If the spec said + is subtraction and * is addition noone would say it's correct. Just like noone says only allowing one null in a unique column is a good idea (One of SQL Server biggest complaints)

→ More replies (0)

0

u/[deleted] May 10 '18

[deleted]

90

u/spacejack2114 May 10 '18
// \u000d App.initialize();
// Don't remove! ^ This comment is needed for some reason???

22

u/zergling_Lester May 10 '18

You can put it into what looks like a string literal even. As an example of sanitising unprintable characters, for example.

18

u/TotallyNotAdamWest May 10 '18

I feel dirty after reading that.

48

u/StillNoNumb May 10 '18

28

u/Ascomae May 10 '18

Somewhere in the class:

/* \u002a\u002f\u007d\u002f\u002a */

11

u/XplittR May 10 '18

}

4

u/Ascomae May 10 '18

Mean, right? The most evil I can imagine.

13

u/__konrad May 10 '18

I think it was a hack in Java 1.0 for text editors without Unicode support. This feature should be deprecated and compiler should print a warning if used outside string/char literal.

2

u/Kered13 May 12 '18

There's a lot of characters you may want to use (usually on a strong) but you don't want to use the literal character. Like all non printing and whitespace characters and other characters that don't make any sense in a fixed width don't environment.

1

u/vytah May 13 '18

All the other escape sequences work only inside strings. I don't know any good reason to let \u to be an exception.

12

u/JerksToSistersFeet May 10 '18

Why is it u000d instead of u000a. Isn't newline 0x0a?

3

u/[deleted] May 10 '18

0x0a is CR (carriage return). Newline is 0x0d, which is LF (line feed). On unix, LF (0x0d) is the newline. On windows, you need both CR and LF (0x0a, followed by 0x0d).

25

u/JerksToSistersFeet May 10 '18

This ASCII table says the exact opposite. LF is 10, CR is 13. http://www.asciitable.com/

16

u/[deleted] May 10 '18

D'oh... you're absolutely right! I'm guessing javac must be rather liberal in what it accepts as a line terminator. I know older Macs used only CR as a line delimiter. It's a big mess.

8

u/CopperBag May 11 '18

+/u/compilebot Java

\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020\u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020\u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b\u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d

3

u/lischni_tschelowek May 11 '18

small error there, missing space between static & void

\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020\u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020\u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b\u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d

3

u/sleepsort May 10 '18

Quick question. Would this work in C/C++? If I recall correctly, the CPP (C preprocessor) strips the comments from the file/code being executed.

5

u/scorcher24 May 10 '18

2

u/sleepsort May 10 '18

Thanks! Was going to test this out as soon as I got home but that adds a lot of explanation!

5

u/Drisku11 May 11 '18

You can do the opposite with C/C++ (pre-C++17) though: surprise coworkers by turning non-comment lines into comments with trigraphs.

3

u/[deleted] May 11 '18

\u000d, where’s my code?

5

u/gvargh May 10 '18

More languages need #zap and #restore.

2

u/inmatarian May 10 '18

The last place I'd expect to see a ZZT reference.

2

u/gurenkagurenda May 11 '18

I'm glad to see this getting attention again. This is an opportunity for a clever attacker to sneak a backdoor into a code base, potentially right under a code reviewer's nose. All you need is an excuse to put some apparently commented out "example" or "debug" code containing some unicode escapes. As long as the syntax highlighter thinks the code is commented out, there's a good chance a reviewer won't give it a second glance.

2

u/Almoturg May 11 '18 edited May 11 '18

Aww, I was hoping this was about something more fun, like RTL overrides.

2

u/kitd May 11 '18

Reminds me of RustDoc which compiles and runs example code in comments.

5

u/ShadowPouncer May 11 '18

... No, just, no.

I'm sorry, but the benefits that you get from applying unicode translations that early in the game are not nearly enough to justify side-effects like this.

Parse out comments first, then handle things like escapes, unicode translations, and the like.

This also means that // Some text \

Really shouldn't result in the comment continuing on in the next line.

I kind of get their argument, but I'm seriously opposed to the result existing. :)

1

u/flaghacker_ May 11 '18

What? How often has this been a problem to you?

0

u/ShadowPouncer May 11 '18

I don't do Java in general.

But this kind of thing makes reading the code that much more difficult, and it makes it significantly easier to write things that look like they are doing one thing when they are actually doing another.

That is bad enough as an accident, and is really bad when it is intentional.

I may be biased by my work environment, but at least 50% of the job of any program is to be fully readable at 3am after an hour of sleep. If the code works but doesn't pass that test, it's just as broken as code that works but which you can't understand.

When you're faced with regulatory environments that require you to actually review code and say that it does what it says it does, you have a different set of 'even the less experienced programmers need to be able to read this reliably' requirements.

I'm having a lot of trouble finding any benefit to the compiler parsing unicode before comments, but I'm finding plenty of ways to abuse it.

3

u/[deleted] May 11 '18

Why the hell is Java decoding/unescaping source code? It literally makes no sense. It doesn't even do it properly. Putting in \z gets me an error because it doesn't exist. Putting \z in a comment works fine. If it's going to decode my source code why the hell isn't it giving me errors on invalid decodes. Java is not doing the wrong thing correctly

0

u/[deleted] May 11 '18

[deleted]

1

u/[deleted] May 11 '18

It's defined but makes no sense. It's also doing it wrong. //\z isn't a correct escape so it should get an error. If you say it shouldn't then //\u0ashouldn't either because it's also an invalid escape however it does.

Its an unnecessary and incorrect step. If 'Hello 💩' works then I don't see why it needs to do decoding outside of string literals. In fact it can't even decode that correct either as \U0001F4A9 gets a compile error.

0

u/[deleted] May 11 '18

[deleted]

1

u/[deleted] May 11 '18

I'm calling bullshit. 1) Noone reads or writes \u1234 themselves 2) I can't imagine the compiler throwing a fit that 8bit characters are in the source file in a comment (ANSI is 8bit IIRC do this pushes my point) so only the IDE would care what encoding the file is

2

u/i_feel_really_great May 10 '18

A favourite of Smalltalkers. Write code to demo your method in the comments so you can just select and execute.

1

u/bloody-albatross May 10 '18

Shouldn't it be \u000a? \u000d is carriage return (\r), \u000a is new line (\n).

1

u/alparsla May 11 '18

Roedy Green should be proud :)

How to Write Unmaintainable Code

http://www.mindprod.com/jgloss/unmain.html

1

u/immibis May 11 '18

I will be very impressed if there's an IDE that correctly highlights these.

1

u/AdrianJMartin May 11 '18

Isn't this a major security risk?

2

u/Uncaffeinated May 11 '18

It's one of many ways that you can attempt to slip malicious code past a code review.

1

u/UnfrightenedAjaia May 11 '18

The problem here is that every text editor has it wrong so this is misleading in practice.

1

u/DynamicTextureModify May 11 '18

So I'm not a Java guy... can someone explain to me the rationale behind parsing unicode expressions in comments? This doesn't seem like it should reasonably exist.

3

u/drysart May 11 '18

Java effectively does a pre-pass over a source file to replace all \u0000 sequences with the actual character that the escape references to. That it's in a comment is irrelevant; the escape sequences are parsed through the entire source file independent of any other context.

It's pretty idiotic, but it's Java so some level of brain damage is to be expected.

0

u/DynamicTextureModify May 11 '18

That's ridiculous. Brain damage is right.

I don't care if it's a "pre-pass" or not, commented lines should always be ignored.1


  1. Don't get me started on nonsense like those PHP interpreting engines that use fucking docblocks programmatically for database definitions and shit. I almost had an anyuerism when I started reading about that.

-2

u/recursive May 11 '18

That's how it already works. What you probably object to is how java defines "lines".

1

u/DynamicTextureModify May 12 '18

// \u000d System.out.println("Hello World!"); Is a line. It only becomes 2 lines after Java parses the unicode in this line. The fact that it reaches the point where it could be considered 2 lines is the problem in the first place.

1

u/recursive May 12 '18

I think we're all in agreement on that.

1

u/aioobe May 11 '18

This is basically the first thing the article addresses.

1

u/DynamicTextureModify May 12 '18

It really doesn't.

You don’t even need to figure out where comments begin and end!

Is not a good thing. Why would anyone want that?

1

u/aioobe May 12 '18

You asked for the rationale behind parsing unicode expressions in comments. The rationale is that it makes it trivial to transform a program written in Unicode to ASCII.

Why anyone (except the person writing the code for transforming between encodings) would want that is not answered in the article, but that is a different question.

Also, I don't think anyone here argues that it is a good thing.

1

u/DynamicTextureModify May 12 '18

I guess you're right, I was just expecting the rational to be... well, rational. Hah.

-5

u/pdp10 May 10 '18
/*
 * Always better.
 */

2

u/gurenkagurenda May 11 '18

I think you're saying this will prevent the issue. It won't. You can just use unicode escapes to terminate the block comment, then use more to start a new one.

2

u/recursive May 11 '18
/*
 * blah blah blah
 * \u002a\u002f System.out.println("gotcha"); \u002f\u002a
 * even more something
 */

-12

u/dethb0y May 10 '18

Unicode: The Mistake That Keeps On Giving

5

u/slavik262 May 11 '18

What should we use instead?

2

u/Cuddlefluff_Grim May 11 '18

IBM ASCII2, no question.

-2

u/dethb0y May 11 '18

Nothing? why is there a need for there to ever be unicode in source code files, and "so some guy who doesn't want to use ascii can type the weird characters in his native language" doesn't count as a valid reason to introduce this kind of vulnerability.

1

u/nathreed May 11 '18

It’s not a vulnerability. This is only a problem in java because java is designed poorly. If the preprocessor/compiler stripped comments out before doing the Unicode to ascii conversion pass, this wouldn’t be an issue. So your “guy” who wants to use the “weird characters in his native language” (when your characters appear just as weird to him) would be able to do that if the idiots at Sun/Oracle had thought this through a bit better.

0

u/[deleted] May 11 '18

[deleted]

-3

u/dethb0y May 11 '18

if some russian API is spewing bad characters, then the solution's not to use the broken API, not to break everything else so it's as broken as the API.

-2

u/ProFalseIdol May 11 '18

While trying to test this in eclipse I get this while renaming my class file to Ugly:

Waiting for JSP Index Manager

What does JSP have to do with my plain java project named 'x'?

edit: waited forever, cancelled renaming, I click on the file, press alt+enter, click show in explorer, then rename there, back to eclipse and press F5

-2

u/shevegen May 11 '18

Java keeps on becoming more and more verbose.

Not only in real code but now also putting verbose code into comments - it's awesome.

-6

u/[deleted] May 11 '18

Java: Worse than Cascade Style Sheets