16
\$\begingroup\$

Given the following C# program outputting False, inject a 'malicious' line of code such that the program outputs True.

class Program
{
    static void Main()
    {
        System.Console.Write("False");
        ;
    }
}

Your answer should consist of a string that replaces the second semicolon, and causes the program to output True, the whole True and nothing but True (not even a newline). It must do this if stdout is printed to the console, and if stdout is redirected to a file.

The shortest answer wins.

\$\endgroup\$
7
  • \$\begingroup\$ What's your objective winning criterion? \$\endgroup\$
    – J B
    Commented Jan 3, 2014 at 14:14
  • \$\begingroup\$ @JB Do puzzles need a winning criterion? Will edit. \$\endgroup\$ Commented Jan 3, 2014 at 14:15
  • \$\begingroup\$ I'm mostly reacting to the code-golf remark, but the point stands: which will be the accepted answer? \$\endgroup\$
    – J B
    Commented Jan 3, 2014 at 14:16
  • \$\begingroup\$ @JB Shortest answer is best answer. \$\endgroup\$ Commented Jan 3, 2014 at 14:16
  • \$\begingroup\$ Well it is code-golf, then, isn't it? \$\endgroup\$
    – J B
    Commented Jan 3, 2014 at 14:18

6 Answers 6

18
\$\begingroup\$

52 characters

}static Program(){System.Console.Write(0<1);for(;;);

so the whole thing becomes:

class Program
{
    static void Main()
    {
        System.Console.Write( "False" );
    }
    static Program()
    {
        System.Console.Write( 0 < 1 ); 
        for ( ; ; ) ;
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Sorry for editing the question in the middle of your answer. Newlines are no longer allowed, you'll have to use Write. \$\endgroup\$ Commented Jan 3, 2014 at 15:16
  • \$\begingroup\$ Ok, I've changed WriteLine to Write \$\endgroup\$ Commented Jan 3, 2014 at 15:18
  • 1
    \$\begingroup\$ You could probably save some bytes by replacing your exit with for(;;);, nothing I've seen requires that the program terminates...? \$\endgroup\$ Commented Jan 3, 2014 at 16:19
  • \$\begingroup\$ @JoachimIsaksson Yes, that meets the requirements as specified. \$\endgroup\$ Commented Jan 3, 2014 at 16:28
  • 5
    \$\begingroup\$ }static Program(){System.Console.Write(0<1);for(;;); could always become }static Program(){for(System.Console.Write(0<1);;); \$\endgroup\$
    – NPSF3000
    Commented Feb 18, 2014 at 5:33
7
\$\begingroup\$

C#, 51 characters

Console.Write("\b\b\b\b\b");Console.Write("True ");

Will only work on standard output

\$\endgroup\$
5
  • \$\begingroup\$ First thing I tried, unfortunately it won't work because of the initial WriteLine. \$\endgroup\$
    – Vereos
    Commented Jan 3, 2014 at 15:09
  • 3
    \$\begingroup\$ Now it is Write :/ I don't get how we would manage to solve this if it keeps changing... \$\endgroup\$
    – Vereos
    Commented Jan 3, 2014 at 15:12
  • \$\begingroup\$ @Vereos I wanted to remove the ambiguity of 'newline or no newline'. \$\endgroup\$ Commented Jan 3, 2014 at 15:13
  • 4
    \$\begingroup\$ You don't need 2 instances of Console.Write, and you could use \r instead of 5 \bs. Console.Write("\rTrue "); \$\endgroup\$
    – Brian S
    Commented Jan 3, 2014 at 21:10
  • \$\begingroup\$ I don't think it meets the criteria. It actually outputs False first, which is then overwtitten by True later. \$\endgroup\$
    – microbian
    Commented Feb 4, 2014 at 7:44
2
\$\begingroup\$

You can generalize RamonBoza's answer to still work if stdout is redirected to a file.

Assuming the program runs under Mono on Linux, with the assembly Mono.Posix.dll loaded:

if(Mono.Unix.Native.Syscall.isatty(1))
{
    Console.Write("\rTrue ");
}
else
{
    // Truncate the output file first
    Console.OpenStandardOutput().SetLength(0);
    Console.Write("True");
}

I doubt this works on Windows, but there's probably an equivalent to isatty().

\$\endgroup\$
1
\$\begingroup\$

83... WIP

I was hoping this'd work, but apparently "False" isn't being interned as I'd hoped in my compiler

//}unsafe static Program(){fixed(char*f="False"){*(long*)f=0x65007500720054;*(f+4)=' ';}
}unsafe static Program(){fixed(char*f="False")for(int i=5;i-->0;)*(f+i)="True "[i];

ungolfed

class Program
{
    static void Main(string[] args)
    {
        Console.Write("False");
    }

    unsafe static Program()
    {
        fixed (char* f = "False") for (int i = 5; i-- > 0; ) *(f + i) = "True "[i];
    }
}
\$\endgroup\$
1
\$\begingroup\$
class Program
{
    static void Main()
    {
        System.Console.Write("False");

        System.Console.Clear(); System.Console.Write("True");

        Console.ReadLine();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ This won't work if stdout is redirected. \$\endgroup\$ Commented Feb 19, 2014 at 13:03
0
\$\begingroup\$

Not sure if this is accepted.
The question doesn't say anything about compiler options so I assume I can change the entry point of the app.
55 characters ,3 more than the accepted answer

}class P{static void Main(){System.Console.Write(1>0);}

Full

class Program
{
    static void Main()
    {
        System.Console.Write("False");
    }
    class P
    {
        static void Main() { System.Console.Write(1 > 0); }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the community! \$\endgroup\$ Commented Jul 22, 2016 at 9:48
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Thanks. This is a fantastic site!!! \$\endgroup\$ Commented Jul 22, 2016 at 9:49

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.