108
\$\begingroup\$

The challenge is to find a string of characters that cannot appear in any legal program in your programming language of choice. That includes comments, strings, or other "non-executable" parts.

Challenge

  • Your program may be specific to a particular version or implementation of your language's compiler/interpreter/runtime environment. If so, please specify the particulars.
  • Only standard compiler/interpreter/runtime options are permitted. You cannot pass some weird flag to your compiler to get a specific result (e.g. passing a flag to convert warnings into errors).
  • If your programming language requires a specific encoding (e.g. UTF-8), your string must also be correctly encoded (i.e. strings which fail solely due to character decoding errors are not allowed).
  • Every individual character in your submission must be admissible in a legal program; that is, you can't just use a character which is always rejected.
  • The compiler/interpreter/runtime must give an error when given any source code that contains your string as a substring. The error does not have to be the same across programs - one embedding of your string might cause a syntax error, while another might cause a runtime error.

Scoring

  • Shortest illegal string for each language wins.
  • You should explain why your string is illegal (why it cannot appear anywhere in a legal program).
  • Dispute incorrect solutions in the comments. More specifically, you should provide a link to TIO or equivalent demonstrating a legal program (i.e. one that doesn't produce any errors) that contains the proposed substring.
  • Some languages (e.g. Bash, Batch, Perl) allow arbitrary binary data to be appended to a program without affecting validity (e.g. using __DATA__ in Perl). For such languages, you may submit a solution that can appear only in such a trailing section. Make sure to make a note of that in your answer. (The definition of this "trailing section" is language-dependent, but generally means any text after the parser has entirely stopped reading the script).

Example

In Python, I might submit

x
"""
'''

but this can be embedded into the larger program

"""
x
"""
'''
y
'''

so it isn't admissible.

\$\endgroup\$
17
  • 3
    \$\begingroup\$ Can a counter-example rely on input from STDIN? \$\endgroup\$
    – Adalynn
    Commented Jul 20, 2017 at 14:17
  • 8
    \$\begingroup\$ Would this make a good CnR? \$\endgroup\$ Commented Jul 20, 2017 at 16:59
  • 10
    \$\begingroup\$ My condolences to the Perl attempts. :) \$\endgroup\$
    – Kaz
    Commented Jul 23, 2017 at 15:40
  • 6
    \$\begingroup\$ I'm pretty sure it's completely impossible in non-literate Haskell, thanks to nested comments. \$\endgroup\$
    – dfeuer
    Commented Mar 19, 2019 at 1:50
  • 6
    \$\begingroup\$ @dfeuer That was my conclusion too. I even looked through GHC's lexer code to see if there was a loophole, but it seems very diligent about allowing absolutely every byte sequence inside sufficiently nested {- -} brackets. \$\endgroup\$ Commented Apr 4, 2019 at 1:32

65 Answers 65

74
\$\begingroup\$

Changeling, 2 bytes




That's two linefeeds. Valid Changeling must always form a perfect square of printable ASCII characters, so it cannot contain two linefeeds in a row.

The error is always a parser error and always the same:

This shape is unpleasant.

accompanied by exit code 1.

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ This also works with 2Col. Try it online!. But the reason this breaks in 2Col is that every line needs to consist of exactly 2 characters, but the empty line breaks that. \$\endgroup\$
    – user41805
    Commented Jul 20, 2017 at 6:10
  • 7
    \$\begingroup\$ +1 because this is automatically the winner, since 1-byte solutions are not allowed because "you can't just use a character which is always rejected." \$\endgroup\$
    – Adalynn
    Commented Jul 20, 2017 at 14:46
  • 1
    \$\begingroup\$ @Cowsquack tfw I forgot about my own language \$\endgroup\$
    – Mayube
    Commented Jul 21, 2017 at 10:54
  • 1
    \$\begingroup\$ @Skidsdev tfw I re-forgot about my own language, and forgot about me forgetting about my own language \$\endgroup\$
    – Mayube
    Commented Nov 19, 2018 at 18:19
  • 1
    \$\begingroup\$ @Zacharý I know just the esolang for the job! \$\endgroup\$ Commented Nov 20, 2018 at 1:17
45
\$\begingroup\$

Java, 4 bytes

;\u;

Try it online!

This is an invalid Unicode escape sequence and will cause an error in the compiler.

error: illegal unicode escape
\$\endgroup\$
6
  • \$\begingroup\$ Doesn't work - one could have a string literal like "\\u;". \$\endgroup\$
    – feersum
    Commented Jul 20, 2017 at 6:57
  • \$\begingroup\$ @feersum Fixed at the cost of one byte \$\endgroup\$
    – user41805
    Commented Jul 20, 2017 at 7:07
  • 26
    \$\begingroup\$ @TheLethalCoder: Java preprocesses source code to alter \uXXXX escapes before doing anything else, so yes, this will work even inside comments.za \$\endgroup\$
    – nneonneo
    Commented Jul 20, 2017 at 8:58
  • 14
    \$\begingroup\$ I think this is the shortest Java answer in the history of this site still.. \$\endgroup\$ Commented Mar 2, 2018 at 18:31
  • 1
    \$\begingroup\$ @MagicOctopusUrn Actually, there is this 0 bytes Java answer (which isn't relevant anymore in the current meta, since it outputs to STDERR instead of STDOUT). Although both are pretty amazing and clever. :) \$\endgroup\$ Commented Mar 20, 2018 at 16:13
43
\$\begingroup\$

COBOL (GNU), 8 bytes


THEGAME

First, a linefeed to prevent you from putting my word in a commented line.

Then, historically, COBOL programs were printed on coding sheets, the compiler relies heavily on 80-character limited lines, there are no multiline comments and the first 6 characters are comments (often used as editable line numbers), you can put almost anything there, AFAIK. I chose THEGAM at the beginning of the next line.

Then, the 7th symbol in any line only accepts a very restricted list of characters : Space (no effect), Asterisk (comments the rest of the line), Hyphen, Slash, there may be others, but certainly not E.

The error given by GnuCobol, for instance, is :

error: invalid indicator 'E' at column 7

Try it online!

Also, you just lost the game.

\$\endgroup\$
1
  • 55
    \$\begingroup\$ Also, you just lost the game. I almost downvoted \$\endgroup\$
    – Stephen
    Commented Jul 21, 2017 at 13:30
28
\$\begingroup\$

JavaScript, 7 bytes


;*/\u)

Note the leading newline.

  • \u) is an invalid Unicode escape sequence and this is why this string is invalid
  • Adding a // at the beginning will still not work because of the leading newline, leaving the second line uncommented
  • Adding a /* will not uncomment the string completely because of the closing */ that completes it, leaving the \u) exposed
  • As stated by @tsh, the bottom line can be turned into a regex by having a / after the string, so by having the ) in front of the \u, we can ensure that the regex literal will always be invalid
  • As stated by @asgallant, one could do 1||1(string)/ to avoid having to evaluate the regex. The semi-colon at the beginning of the second line stops that from happening by terminating the expression 1||1 before it hits the second line, thus forcing a SyntaxError with the ;*.

Try it!

clicky.onclick=a=>{console.clear();console.log(eval(before.value+"\n;*/\\u)"+after.value));}
textarea {
  font-family: monospace;
}
<button onclick="console.clear();">Clear console</button>
<br>
<textarea id=before placeholder="before string"></textarea>
<pre><code>
 */\u)</code></pre>
<textarea id=after placeholder="after string"></textarea>
<br>
<button id=clicky>Evaluate</button>

\$\endgroup\$
19
  • 2
    \$\begingroup\$ /* */\u0045 = 3 seems valid JavaScript code. \$\endgroup\$
    – tsh
    Commented Jul 20, 2017 at 5:36
  • 2
    \$\begingroup\$ 3 */\u;/ is still valid. \$\endgroup\$
    – tsh
    Commented Jul 20, 2017 at 5:55
  • 3
    \$\begingroup\$ Interesting to note that as of ES2018 (which won't be official until the end of this year) you can just wrap the whole thing in backticks due to this. You could probably fix this though just by inserting a backtick after the / (not that you need to fix it). (Also, the ; doesn't force the parsing of the bad regex, it forces a SyntaxError with the *.) \$\endgroup\$ Commented Jul 20, 2017 at 15:28
  • 1
    \$\begingroup\$ @Leushenko But this doesn't work against #if 0 as seen here: Try it online! \$\endgroup\$
    – user41805
    Commented Jul 21, 2017 at 18:17
  • 6
    \$\begingroup\$ In newer JS versions, String.raw with a template string can make this not break, because the invalid escape fails. Would be: String.raw`code here` \$\endgroup\$
    – iovoid
    Commented Feb 14, 2018 at 4:23
24
\$\begingroup\$

Python, 10 bytes (not cpython)


?"""?'''?

Edit:

Due to @feersum's diligence in finding obscure ways to break the Python interpreter, this answer is completely invalidated for any typical cpython environment as far as I can tell! (Python 2 and 3 for both Windows and Linux) I do still believe that these cracks will not work for Pypy on any platform (the only other Python implementation I have tested).

Edit 2:

In the comments @EdgyNerd has found this crack taking advantage of a non-ascii encoding declaration! This seems to decode to print(""). I don't know exactly how this was found but I imagine the way to fix this sort of exploit would maybe be to try different combinations of any invalid characters where the ?s are, and find one that doesn't behave well with any encoding.


Note the leading newline. Cannot be commented out due to the newline, and no combination of triple quoted strings should work if I thought about this correctly.

@feersum in the comments seems to have completely broken any cpython program on Windows as far as I can tell by adding the 0x1A character to the beginning of a file. It seems that maybe (?) this is due to the way this character is handled by the operating system, apparently being a translated to an EOF as it passes through stdin because of some legacy DOS standard.

In a very real sense this isn't an issue with python but with the operating system. If you create a python script that reads the file and uses the builtin compile on it, it gives the more expected behavior of throwing a syntax error. Pypy (which probably does just this internally) also throws an error.

\$\endgroup\$
19
  • 1
    \$\begingroup\$ @officialaimm consider """?'''""" \$\endgroup\$
    – KSab
    Commented Jul 20, 2017 at 5:20
  • 4
    \$\begingroup\$ I made a program with this substring that runs on my machine. However, I think it does not run on many interpreters/platforms/versions. Can you specify which version of Python interpreter and OS this answer is targeting? \$\endgroup\$
    – feersum
    Commented Jul 20, 2017 at 7:54
  • 1
    \$\begingroup\$ Python 3 on Windows 7 happens to be exactly where my crack is working. Pastebin of base64-encoded program \$\endgroup\$
    – feersum
    Commented Jul 20, 2017 at 18:19
  • 1
    \$\begingroup\$ I can crack this one as well. Simply put a 0x1A character at the beginning of the file, and all the rest of it is ignored (this actually works for Python 3 as well). \$\endgroup\$
    – feersum
    Commented Jul 24, 2017 at 2:55
  • 5
    \$\begingroup\$ I know this is really old, but after working with some people over in the Python Discord, we found this crack, although I don't know if changing the encoding can be considered cheating \$\endgroup\$
    – EdgyNerd
    Commented Oct 5, 2019 at 18:40
20
\$\begingroup\$

C (clang), 16 bytes

 */
#else
#else

Try it online!

*/ closes any /* comment, and the leading space makes sure we didn’t just start one. The newline closes any // comment and breaks any string literal. Then we cause an #else without #if or #else after #else error (regardless of how many #if 0s we might be inside).

\$\endgroup\$
8
  • 6
    \$\begingroup\$ Cracked again. \$\endgroup\$
    – feersum
    Commented Jul 20, 2017 at 7:18
  • 3
    \$\begingroup\$ Also since C++11 raw strings seem to work, a solution is impossible with gcc. \$\endgroup\$
    – feersum
    Commented Jul 20, 2017 at 7:22
  • 1
    \$\begingroup\$ @feersum Huh, TIL that GCC accepts those in C code. I could specify -std=c99, but let’s try switching to clang. \$\endgroup\$ Commented Jul 20, 2017 at 7:28
  • 4
    \$\begingroup\$ I'm really surprised that gcc accepts C++11 raw strings. Specifying the compiler version or implementation is perfectly OK, so if it's illegal in Clang, it's fair game. \$\endgroup\$
    – nneonneo
    Commented Jul 20, 2017 at 17:29
  • 2
    \$\begingroup\$ @l4m2 I can't parse your question (who is they, and what do you mean by again?), but note that a C++ raw string literal supports a custom delimeter: R"foobar(...)foobar", and only a right paren followed by the matching delimeter and a quote will close it. \$\endgroup\$ Commented Nov 2, 2018 at 0:41
20
+100
\$\begingroup\$

Rust, 65540 bytes

`"###... a lot more #s ###‪

That is a backtick, quote, 65535 #s, and an invisible U+202A Per this advisory there is an issue with Unicode bidirectional overrides that can cause Unicode-aware editors to misrender the code, allowing bad actors to hide malicious code in plain sight. Starting in rust 1.56.1, rustc contains mitigation for that vulnerability, preventing the use of the relevant codepoints by default anywhere in the source code. However, the text_direction_codepoint_in_literal lint can be set to allow or warn to let use it in string literals. String literals are tricky to break out of, mainly because raw string literals are designed to allow any valid utf8 within them. However, take a look at rustc's lexer, and you will find something interesting- no more than 65535 hashtags are permitted in delimiters. Simply adding the quote followed by the long string of hashtags is enough to break any attempts at surrounding it in strings, and the U+202A is always invalid anywhere but a string. However there is another problem- someone could use the starting quote to begin a string, and hide the nastiness that way. This is where the backtick comes into play- it is an error anywhere but a literal or a comment, thus making it truly impossible for rust to contain this code. If you're wondering if there is an easier solution that fails after the lexer you'd be disappointed- anything that passes the lexer can be thrown away by a macro, and tacking on a #[cfg(nope)] will make anything that parses disappear.

edit: an actually valid answer this time

\$\endgroup\$
5
  • \$\begingroup\$ This solution is incorrect. \r by itself is allowed in regular comments (however not doc-comments). \$\endgroup\$
    – null
    Commented Nov 4, 2021 at 13:03
  • \$\begingroup\$ @KonradBorowski fixed, an update to rust made that strategy completely obsolete. \$\endgroup\$
    – Aiden4
    Commented Nov 4, 2021 at 17:04
  • \$\begingroup\$ "Every individual character in your submission must be admissible in a legal program; that is, you can't just use a character which is always rejected." If this is only a single U+202A, wouldn't this violate that rule? \$\endgroup\$
    – Bbrk24
    Commented Nov 4, 2021 at 21:05
  • 1
    \$\begingroup\$ @Bbrk24 should be fixed- after a little digging, I figured out that if you try hard enough you can put U+202A inside your code. \$\endgroup\$
    – Aiden4
    Commented Nov 4, 2021 at 22:05
  • 1
    \$\begingroup\$ I was going to say that it's possible to allow a lint with #![allow(text_direction_codepoint_in_comment)], but as it happens, in Rust 1.56.1 this lint was incorrectly declared and as such the compiler is not aware of it, see github.com/rust-lang/rust/issues/90614. Due to that, this is a valid solution. \$\endgroup\$
    – null
    Commented Nov 5, 2021 at 15:10
17
\$\begingroup\$

Ada - 2 bytes

I think this should work:


_

That's newline-underscore. Newline terminates comments and isn't allowed in a string. An underscore cannot follow whitespace; it used to be allowed only after letters and numbers, but the introduction of Unicode made things complicated.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$
    – DJMcMayhem
    Commented Jul 21, 2017 at 16:57
15
\$\begingroup\$

Pyth, 6 bytes

¡¡$¡"¡

¡ is an unimplemented character, meaning that if the Pyth parser ever evaluates it, it will error out with a PythParseError. The code ensures this will happen on one of the ¡s.

There are three ways a byte can be present in a Pyth program, and not be parsed: In a string literal (" or .", which are parsed equivalently), in a Python literal ($) and immediately after a \.

This code prevents \ from making it evaluate without error, because that only affects the immediately following byte, and the second ¡ errors.

$ embeds the code within the $s into the compiled Python code directly. I make no assumptions about what might happen there.

If the program reaches this code in a $ context, it will end at the $, and the ¡ just after it will make the parser error. Pyth's Python literals always end at the next $, regardless of what the Python code might be doing.

If the program starts in a " context, the " will make the string end, and the final ¡ will make the parser error.

\$\endgroup\$
13
\$\begingroup\$

x86 32-bit machine code, 11 bytes (and future-proof 64-bit)

90 90 90 90 90 90 90 90 90 0f 0b

This is times 9 nop / ud2. It's basically a NOP sled, so it still runs as 0 or more nops and then ud2 to raise an exception, regardless of how many of the 0x90 bytes were consumed as operands to a preceding opcode. Other single-byte instructions (like times 9 xchg eax, ecx) would work, too.

x86 64-bit machine code, 10 bytes (current CPUs)

There are some 1-byte illegal instructions in 64-bit mode, until some future ISA extension repurposes them as prefixes or parts of multi-byte opcodes in 64-bit mode only, separate from their meaning in 32-bit mode. 0x0e is illegal in 64-bit mode on current CPUs (tested on Intel Skylake). It's push cs in 32-bit mode; AMD64 freed up a bunch of opcodes for future use but as of yet neither Intel nor AMD have added any 64-bit-only extension to use them. But they still could, which is why this isn't future-proof; some future CPU could decode 0x0e as an opcode or prefix.

0e 0e 0e 0e 0e 0e 0e 0e 0e 0e

Rules interpretation for executable machine code:

  • The bytes can't be jumped over (like the "not parsed" restriction), because CPUs don't raise exceptions until they actually try to decode/execute (non-speculatively).

  • Illegal means always raises an exception, for example an illegal-instruction exception. (Real programs can catch that with an exception handler on bare metal, or install an OS signal handler, but I think this captures the spirit of the challenge.)


It works because a shorter byte-string ending in ud2 could appear as an imm32 and/or part of the addressing mode for another instruction, or split across a pair of instructions. It's easiest to think about this in terms of what you could put before the string to "consume" the bytes as part of an instruction, and leave something that won't fault.

I think an instruction can consume at most 9 bytes of arbitrary stuff: a SIB byte, a disp32, and an imm32. i.e. the first 2 bytes of this instruction can consume 8 NOPs and a ud2, but not 9.

c7 84 4b 00 04 00 00 78 56 34 12        mov dword [rbx+rcx*2+0x400],0x12345678

Can't beat 9 nops:

    db 0xc7, 0x84   ; opcode + mod/rm byte: consumes 9 bytes (SIB + disp32 + imm32)
    times 9 nop          ; 1-byte xchg eax, ecx or whatever works, too
    ud2
  ----
   b:   c7 84 90 90 90 90 90 90 90 90 90        mov    DWORD PTR [rax+rdx*4-0x6f6f6f70],0x90909090
  16:   0f 0b                   ud2    

64-bit mode:

 c7 84 0e 0e 0e 0e 0e 0e 0e 0e 0e        mov    DWORD PTR [rsi+rcx*1+0xe0e0e0e],0xe0e0e0e
 0e                      (bad)  

But the bytes for 8 NOPs + ud2 (or times 9 db 0x0e) can appear as part of other insns:

    db 0xc7, 0x84   ; defender's opcode + mod/rm that consumes 9 bytes

    times 8 nop          ; attacker code
    ud2

    times 10 nop    ;; defenders's padding to be consumed by the 0b opcode (2nd half of ud2)
----
  18:   c7 84 90 90 90 90 90 90 90 90 0f        mov    DWORD PTR [rax+rdx*4-0x6f6f6f70],0xf909090
  23:   0b 90 90 90 90 90       or     edx,DWORD PTR [rax-0x6f6f6f70]
  29:   90                      nop
  2a:   90                      nop
  ...
\$\endgroup\$
5
  • \$\begingroup\$ The rules here weren't really clear enough for me to consider posting an asm/machine code answer. For example, why can't you just do ud2? It seems you're saying that you interpret the rules as forbidding jumping over the bytes, so ud2 would work just fine on its own, no? Oh…I guess you're saying the issue is that ud2 can appear as a prefix to a valid instruction? The second part of this answer was a little difficult for me to understand. \$\endgroup\$
    – Cody Gray
    Commented Jul 21, 2017 at 6:02
  • \$\begingroup\$ @CodyGray: Right, the 2 bytes that encode ud2 can appear in the imm32 of any instruction. I was thinking about this in terms of what bytes can you put before such a string that "consume" 0f 0b as part of an earlier instruction instead of decoding as ud2. I wasn't totally happy with how I ended up presenting it, but I wanted to illustrate why only 8 nops wasn't enough, and what happened with 9 nops + ud2. \$\endgroup\$ Commented Jul 21, 2017 at 7:58
  • \$\begingroup\$ @CodyGray: An asm source program would be a totally different answer. That would have to error the parser used by the assembler, not produce faulting machine-code. So something like %else / %else might work to defeat %if 0, which can normally protect any invalid text from being parsed. (idea from a CPP answer) \$\endgroup\$ Commented Jul 21, 2017 at 8:02
  • 1
    \$\begingroup\$ Don't quite satisfy. Your solution may be just in .data. (though it makes it impossible) \$\endgroup\$
    – l4m2
    Commented Nov 1, 2018 at 2:29
  • \$\begingroup\$ @l4m2: To make the question answerable / interesting, I had to limit it to code that's executed (and not jumped over). See the rules interpretation bullet points in my answer. That would also rule out static data, of course. Because then it's not machine code at all, it's just data. This question required more adaptation than most for a machine-code answer to make sense, because there is no compile/assemble stage where you can error the parser, we're just talking about bytes already in memory. \$\endgroup\$ Commented Nov 1, 2018 at 2:38
11
\$\begingroup\$

Shakespeare Programming Language, 2 bytes

.:

Explanation: If this string is in the title of the play, the . ends it and the : is not a valid character name. Similar problems occur in an act and scene name. No character can speak a line beginning with :, and the . will end a Recall statement, which can otherwise create a comment.

\$\endgroup\$
10
\$\begingroup\$

INTERCAL, 12 bytes

DOTRYAGAINDO

Try to crack it online!

INTERCAL's approach to syntax errors is a bit special. Essentially, an invalid statement won't actually error unless the program tries to execute it. In fact, the idiomatic syntax for comments is to start them with PLEASE NOTE, which really just starts a statement, declares that it isn't to be executed, and then begins it with the letter E. If your code has DODO in the middle of it, you could prepend DOABSTAINFROM(1)(1) and tack any valid statement onto the end and you'll be fine, if it's DODODO you can just bend execution around it as (1)DON'TDODODOCOMEFROM(1). Even though INTERCAL lacks string literal syntax for escaping them, there's no way to use syntax errors to create an illegal string, even exhausting every possible line number with (1)DO(2)DO...(65535)DODODO, since it seems that it's plenty possible to have duplicate line numbers with COME FROM working with any of them.

To make an illegal string, we actually need to use a perfectly valid statement: TRY AGAIN. Even if it doesn't get executed, it strictly must be the last statement in a program if it's in the program at all. 12 bytes is, to my knowledge, the shortest an illegal string can get using TRY AGAIN, because it needs to guarantee that there is a statement after it (executed or not) so DOTRYAGAIN is just normal code, and it needs to make sure that the entire statement is indeed TRY AGAIN, so TRYAGAINDO doesn't work because it can easily be turned into an ignored, normal syntax error: DON'TRYAGAINDOGIVEUP, or PLEASE DO NOT TRY TO USE TRYAGAINDO NOT THAT IT WOULD WORK. No matter what you put on either side of DOTRYAGAINDO, you'll error, with either ICL993I I GAVE UP LONG AGO, ICL079I PROGRAMMER IS INSUFFICIENTLY POLITE, or ICL099I PROGRAMMER IS OVERLY POLITE.

\$\endgroup\$
2
  • \$\begingroup\$ There might be a few other compile-time errors which can fire before ICL993I I GAVE UP LONG AGO. \$\endgroup\$ Commented Apr 4, 2019 at 1:47
  • \$\begingroup\$ If, while using every line label, you also COME FROM every line label, it might be a bit difficult to divert control flow around the block, but there's nothing at all stopping you from just GIVING UP! \$\endgroup\$ Commented Apr 8, 2019 at 1:19
9
\$\begingroup\$

Commodore 64 Basic, 2 bytes


B

(that's a newline followed by the letter "B").

Any line in a Commodore 64 program must begin with either a line number or a BASIC keyword, and stored programs only permit line numbers. There are no keywords beginning with "B" (or "H", "J", "K", "Q", "X", "Y", or "Z").

\$\endgroup\$
2
  • \$\begingroup\$ If I append =0 then this just becomes an assignment statement... \$\endgroup\$
    – Neil
    Commented Jul 21, 2017 at 9:54
  • 2
    \$\begingroup\$ @Neil, that would be a valid immediate-mode command, but not a valid program. \$\endgroup\$
    – Mark
    Commented Jul 21, 2017 at 10:05
9
\$\begingroup\$

APL and MATL and Fortran, 3 bytes


'

Newline, Quote, Newline always throws an error since block comments do not exist:

\$\endgroup\$
13
  • \$\begingroup\$ This applies to MATL too \$\endgroup\$
    – Luis Mendo
    Commented Jul 20, 2017 at 7:39
  • \$\begingroup\$ I think this would work in Fortran too. \$\endgroup\$
    – Steadybox
    Commented Jul 21, 2017 at 2:27
  • \$\begingroup\$ Also J I believe \$\endgroup\$
    – Jonah
    Commented Nov 16, 2019 at 4:45
  • 1
    \$\begingroup\$ @Jonah Nope. \$\endgroup\$
    – Adám
    Commented Nov 16, 2019 at 19:16
  • \$\begingroup\$ Ah. Would \n)\n'\n work for J? \$\endgroup\$
    – Jonah
    Commented Nov 16, 2019 at 19:38
7
\$\begingroup\$

C#, 16 bytes


*/"
#endif<#@#>

Works because:

  • // comment won't work because of the new line
  • /* comment won't work because of the */
  • You can't have constants in the code alone
  • Adding #if false to the start won't work because of the #endif
  • The " closes any string literal
  • The <#@#> is a nameless directive so fails for T4 templates.
  • The new line tricks it so having / at the start won't trick the */

Each variation fails with a compilation error.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Weird that you decided to include T4 templates in your code. Isn't T4 considered a separate language? \$\endgroup\$ Commented Jul 20, 2017 at 21:07
  • 1
    \$\begingroup\$ @ArturoTorresSánchez I don't know I had never heard of them. Someone commented this didn't work when you included T4 templates so I added the fix. \$\endgroup\$ Commented Jul 21, 2017 at 7:59
7
\$\begingroup\$

Desmos, 3 bytes


\

(linefeed, backslash, linefeed)

I recently said "Pasting in invalid-formatted text [into Desmos] simply does nothing." As I recently discovered, this isn't quite true. When pasting in multiple lines, it's happy to throw errors if one of them is invalid. Now, this works even if some of those lines other are blank, causing them to be ignored. Therefore, we make a \ on its own line, which is guaranteed to throw an error.

Logically, we should be able to knock this down to two bytes (\, LF), as no legal line can end with a \. However, Desmos strangely interprets this as an empty list (normally represented as []), for reasons I don't understand. This makes that two-byte string an unusual almost-illegal string, where the only valid program containing that string is the string itself.

\$\endgroup\$
2
  • \$\begingroup\$ "However, Desmos strangely interprets this as an empty list (normally represented as [])" -- I had to test this, and it's even weirder than I thought. Desmost doesn't just interpret it as [], but renders it as []. \$\endgroup\$
    – Bbrk24
    Commented Sep 23, 2021 at 16:19
  • 3
    \$\begingroup\$ Not only does it parse (where ¶ is newline) as an empty list, it literally parses anything in the form [invalid expression]¶ (replace [invalid expression] with something that would normally error) as an empty list too. Try f(n)=a¶ for example. \$\endgroup\$
    – Aiden Chow
    Commented Mar 23, 2022 at 4:33
5
\$\begingroup\$

Literate Haskell, 15 bytes

Repairing a deleted attempt by nimi.


\end{code}
5
>

Try it online!

nimi's original attempt is the last two lines, based on Literate Haskell not allowing > style literate code to be on a neighboring line to a literate comment line (5 here). It failed because it can be embedded in a comment in the alternate ("LaTeX") literate coding style:

\begin{code}
{-
5
>
-}
\end{code}

However, the \begin{code} style of Literate Haskell does not nest, neither in itself nor in {- -} multiline comments, so by putting a line with \end{code} just before the line with the 5, that workaround fails, and I don't see a different one.

\$\endgroup\$
4
\$\begingroup\$

Free Pascal, 18 bytes


*)}{$else}{$else}

First close all possible comments, then handle conditional compile.

Please comment here if I forgot something.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ @user902383 Does your example contain the leading newline of his snippet? \$\endgroup\$
    – Brian J
    Commented Jul 20, 2017 at 18:44
  • \$\begingroup\$ @BrianJ nope, i thought it was just formatting issue, my bad \$\endgroup\$
    – user902383
    Commented Jul 21, 2017 at 9:23
  • \$\begingroup\$ I don't think it's possible in Free Pascal. Just put them after begin end.. \$\endgroup\$
    – jimmy23013
    Commented Jul 24, 2017 at 15:28
  • \$\begingroup\$ @jimmy23013 but it seems that codes after the end. become valid is allowed by the question. \$\endgroup\$
    – tsh
    Commented Jul 25, 2017 at 1:13
4
\$\begingroup\$

CJam, 7 bytes

"e#"
:"

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Brain-Hack (a variation of Brain-Flak), 3 2 bytes

Thanks to Wheat Wizard for pointing out that Brain-Hack doesn't support comments, saving me a byte.

(}

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ How do you do comments in Brain-Flak? I don't know of any way to do those. \$\endgroup\$ Commented Jul 20, 2017 at 18:26
  • \$\begingroup\$ @EriktheOutgolfer # TIO \$\endgroup\$
    – Riley
    Commented Jul 20, 2017 at 18:27
  • \$\begingroup\$ Huh undocumented behavior. \$\endgroup\$ Commented Jul 20, 2017 at 18:29
  • \$\begingroup\$ @EriktheOutgolfer I always assumed they were documented somewhere. I'll look at adding them. \$\endgroup\$
    – Riley
    Commented Jul 20, 2017 at 18:34
  • \$\begingroup\$ You don't need the newline in BrainHack or Craneflak, Rain-Flak is the only one of the three versions that has line comments. Although Craneflak parses on the fly so its impossible to solve this in Craneflak, any solution could be beaten by prepending (()){()}. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 7, 2017 at 23:26
4
\$\begingroup\$

JavaScript (Node.js), 9 8 bytes

`*/
\u`~

Try it online!

I think this should be illegal enough.

Previous JS attempts in other answers


;*/\u)

By @Cows quack

As an ES5 answer this should be valid, but in ES6 wrapping the code with a pair of backticks wrecks this. As a result valid ES6 answers must involve backticks.

`
`*/}'"`\u!

By @iovoid

This is an improved version involving backticks. However a single / after the code breaks this (It becomes a template literal being multiplied by a regex, useless but syntactically valid.) @Neil made a suggestion that changing ! to ). This should theoretically work because adding / at the end no longer works (due to malformed regex.)

Explanation

`*/
\u`~

This is by itself illegal, and also blocks all single and double quotes because those quotes cannot span across lines without a \ at the end of a line

//`*/
\u`~

and

/*`*/
\u`~

Blocks comments by introducing illegal escape sequences

``*/
\u`~

Blocks initial backtick by introducing non-terminated RegExp literal

console.log`*/
\u`~

Blocks tagged template literals by introducing an expected operator between two backticks

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "Template literal... multiplied by a regex." Only in JavaScript would that be a legal expression. \$\endgroup\$ Commented Nov 26, 2019 at 20:52
4
\$\begingroup\$

Go, 6 bytes


*/```

Try to crack it online!

The grave accent (`) marks a raw string literal, inside which all characters except `, including newlines and backslashes, are interpreted literally as part of the string. Three `'s in a row are the core: adjacent string literals are invalid and ` always closes a ` string, so there's no way to make sense of them. I had to use 3 more bytes for anti-circumvention, a newline so we can't be inside a single-line comment or a normal quoted string, and a */ so we can't be inside a multi-line comment.

\$\endgroup\$
4
\$\begingroup\$

Google Sheets, Excel; 3 bytes

For a single-cell Formula (starts with + or =). Otherwise, you can just put whatever arbitrary text you want into a cell.

Here it is: 1"1

  • A string has to be enclosed in "s. A double quote must be escaped by "".
  • The " has to be a string token boundary.
  • If it's the end, following a string with a 1 is illegal.
  • If it's the beginning, preceding a string with a 1 is also illegal.
\$\endgroup\$
4
\$\begingroup\$

Swift,  4  3 bytes

There's control characters in here, so I'll just give their ASCII values: 0A 22 00.

Most of the ways that other C-family languages do this don't work in Swift, because Swift allows nested block comments.

The null character always generates at least a warning. However, if it's in the middle of whitespace or a line comment, it's only a warning, not an error. Additionally, Xcode lets you put most control characters inside of a block comment and just rolls with it.

Emphasis on most. For some reason, if you have a null byte in the middle of a block comment or string literal, the Swift compiler gets lost and can't find the end, no matter where it is. This causes an error (and also breaks Xcode's syntax highlighting). So, this is three bytes:

  • 0A -- a newline, to make sure we're not in a line comment or single-line string literal.
  • 22 -- ", the start of a single-line string literal, unless it's inside a multi-line string literal or block comment. This does not end a multi-line string literal if it started inside of one.
  • 00 -- a null byte, to prevent the block comment or string literal from terminating.

The original answer used /* (open block comment) rather than ".

\$\endgroup\$
4
\$\begingroup\$

Jelly, 3 4 bytes

»»
€

Try it online!

Alternative 4 bytes:

»

€

+1 byte because emanresu A found an issue with the old approach

Jelly has no comments, and every line in the code is parsed whether or not it is reachable. The only way to prevent some code from being executed is by putting it in a string literal. There are different types of string literals based on the ending character, but almost all start with . This is countered by », which terminates a string (and interprets it as a dictionary-compressed string).

The only two string literal types that do not terminate at the first » are and , which each process precisely the next two characters. However this still leaves the newline unescaped.

Thus (each/map) will always be executed on a link (line) of its own. It tries to get a link from its left; since none exists, the interpreter errors.

\$\endgroup\$
2
  • \$\begingroup\$ Nope \$\endgroup\$
    – emanresu A
    Commented Aug 19, 2022 at 22:40
  • \$\begingroup\$ @emanresuA fixed \$\endgroup\$ Commented Aug 20, 2022 at 10:57
3
\$\begingroup\$

VBA, 2 Bytes

A linefeed followed by an underscore - the _ functions as the line continuation character in VBA, and as there is nothing in the line directly to the left or above the line continuation, coupled with VBA's lack of multiline comments means that this will always throw the compile time error Compile Error: Invalid character


_
\$\endgroup\$
10
  • \$\begingroup\$ You do depend on your pattern starting on a new line... so, add a newline. \$\endgroup\$ Commented Jul 20, 2017 at 19:18
  • \$\begingroup\$ @Deduplicator it already has a new line, - it does not matter what follows the _, only that there is no valid line to the left or above it \$\endgroup\$ Commented Jul 20, 2017 at 19:48
  • \$\begingroup\$ What if it is embedded like this: myfunction( \n_ )? \$\endgroup\$ Commented Jul 20, 2017 at 20:00
  • \$\begingroup\$ @Deduplicator the line continuation character must be on the same line as it is continuing ie Public Function Foo( ByVal bar as Integer, _ (new line) bas as long) as double - so yes, it would result in an error if you called the function you described \$\endgroup\$ Commented Jul 20, 2017 at 21:00
  • \$\begingroup\$ Ok, in that case it's more like myfunction( _ \n_ ). Sorry for the confusion. To put it another way, you should have used two newlines. \$\endgroup\$ Commented Jul 20, 2017 at 21:34
3
\$\begingroup\$

SmileBASIC, 2 bytes


!

Nothing continues past the end of a line, so all you need is a line break followed by something which can't be the start of a statement. ! is the logical not operator, but you aren't allowed to ignore the result of an expression, so even something like !10 would be invalid (while X=!10 works, of course)

Similar things will work in any language where everything ends at the end of a line, as long as it parses the code before executing it.

There are a lot of alternative characters that could be used here, so I think it would be more interesting to list the ones that COULD be valid.

@ is the start of a label, for example, @DATA; ( could be part of an expression like (X)=1 which is allowed for some reason; any letter or _ could be a variable name X=1, function call LOCATE 10,2, or keyword WHILE 1; ' is a comment; and ? is short for PRINT.

\$\endgroup\$
1
  • \$\begingroup\$ oh, for some reason when I edited the post it was duplicated... \$\endgroup\$
    – 12Me21
    Commented Mar 1, 2018 at 20:11
3
\$\begingroup\$

Rockstar, 4 5 bytes

Crossed out 4 is still 4 :(

)
"""

Rockstar is a very... wordy language.
While " can be used to define a string, such as Put "Hello" into myVar, to my knowledge there is no way for 3 quotes to appear outside of a comment, and the close paren ensures that won't happen either (Comments in Rockstar are enclosed in parentheses, like this).

Rockstar also has a poetic literal syntax, in which punctuation is ignored, so the newline makes sure that the 3 quotes are the start of a line of code, which should always be invalid

\$\endgroup\$
4
  • \$\begingroup\$ What about (()"""), wouldn't that be a no-op? \$\endgroup\$ Commented Nov 21, 2018 at 23:46
  • \$\begingroup\$ @BMO first paren opens a comment, 2nd paren does nothing because it's commented, 3rd paren closes the comment, then you have """) being parsed as code, which is invalid \$\endgroup\$
    – Mayube
    Commented Nov 22, 2018 at 1:20
  • \$\begingroup\$ Hmm, nested comments are not in the specs. Comments seem to be discouraged anyways. But you oversaw poetic string literals which allow any string, so Goethe says )""" is valid. \$\endgroup\$ Commented Nov 22, 2018 at 17:13
  • \$\begingroup\$ @BMO good point, can be fixed by inserting a newline inbetween ) and """ \$\endgroup\$
    – Mayube
    Commented Nov 23, 2018 at 18:21
3
\$\begingroup\$

TI-Basic (83+/84+/SE, 24500 bytes)

A

(24500 times)

TI(-83+/84+/SE)-Basic does syntax checking only on statements that it reaches, so even 5000 End statements in a row can be skipped with a Return. This, in contrast, cannot fit into the RAM of a TI-83+/84+/SE, so no program can contain this string. Being a bit conservative with the character count here.

The original TI-83 has 27000 bytes of RAM, so you'll need 27500 As in that case.

TI-Basic (89/Ti/92+/V200, 3 bytes)

"

Newline, quote, newline. The newline closes any comments (and disallows embedding the illegal character in a string, since AFAIK multiline string constants are not allowed), the other newline disallows closing the string, and the quote gives a syntax error.

You can get to 2 bytes with

±

without the newline, but I'm not sure whether this counts because ± is valid only in string constants.

\$\endgroup\$
1
  • \$\begingroup\$ Done, thanks :) \$\endgroup\$
    – bb94
    Commented Apr 5, 2019 at 7:52
3
\$\begingroup\$

AWK, 4 bytes



/

Try it online!

Since AWK doesn't have a method to do multi-line comments, need 2 newlines before and 1 after / to prevent commenting out or turning this into a regex, e.g. add 1/. The most common message being `unexpected newline or end of string.

With previous crack

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I enjoyed this. \$\endgroup\$
    – stephanmg
    Commented Nov 15, 2019 at 15:48
  • \$\begingroup\$ Thanks. I enjoyed coming up with it :) \$\endgroup\$ Commented Nov 18, 2019 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.