14
\$\begingroup\$

Everyone knows the phrase "Rules are made to be broken!", so here is your task:

Write some code in any language that breaks a standard. This can be anything, from putting 240 volts down a USB cable, to pinging your router (NOT somebody else's website!) with a 10mb packet! (Don't get any ideas, both of these will probably cause all sorts of doom!)

The most upvoted answer after 10 days (i.e the most serious violation) will win!

Rules:

  1. The code must be explained.
  2. The standard that you are breaking must be linked to, and you must explain what you are doing (implies 1) to break it. Otherwise answers will not be accepted.
  3. Answers that just crash the interpreter, or make it stop working will not be accepted. I would like some creative answers, not just pasting something like 𐒢 into IDLE (which crashes it)
  4. Exploiting bugs are not allowed, unless they break a standard. Otherwise answers will not be accepted

Begin, and have fun!

\$\endgroup\$
6
  • 18
    \$\begingroup\$ The majority of answers on this site violate at least one coding standard... \$\endgroup\$
    – Comintern
    Mar 1, 2014 at 1:52
  • 2
    \$\begingroup\$ This isn't original with me, but... animated JPEG! \$\endgroup\$ Mar 1, 2014 at 7:38
  • \$\begingroup\$ Hey, you're right about 𐒢 \$\endgroup\$
    – TheDoctor
    Mar 2, 2014 at 3:55
  • 4
    \$\begingroup\$ My students tend to violate any QA standards when writing code. They deserve to win! \$\endgroup\$
    – bot47
    Mar 3, 2014 at 9:42
  • 1
    \$\begingroup\$ Inspiration can be had at "How to implement GOTO in Java": stackoverflow.com/questions/2430782/… \$\endgroup\$
    – Abulafia
    Apr 21, 2014 at 19:49

17 Answers 17

68
\$\begingroup\$

Python

print 'Hello World'

Explanation:

The standard of the Programming Puzzles & Code Golf Stack Exchange is to

Read the question carefully. What, specifically, is the question asking for? Make sure your answer provides that


This code is not answering the question, so it breaks the standard.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Or to break a standard in python 2, print("hello world") \$\endgroup\$
    – TheDoctor
    Mar 1, 2014 at 14:53
  • 10
    \$\begingroup\$ @TheDoctor but if I break the python standard, I can't break the codegolf.SE standard \$\endgroup\$
    – user12205
    Mar 1, 2014 at 15:11
  • 18
    \$\begingroup\$ But if this question breaks the standard for this question, and explains it, it is not breaking the standard. But then it is invalid and must be breaking a Python standard, which it is not! A paradox – it breaks the very standards of reality! \$\endgroup\$
    – kojiro
    Mar 4, 2014 at 17:47
  • 2
    \$\begingroup\$ As this is a popularity contest, I have to go with this incredibly creative answer which has the most votes! Even though it doesn't cause anything to break/restart :(. However good answer. Well done :) \$\endgroup\$
    – George
    Mar 14, 2014 at 16:34
  • 5
    \$\begingroup\$ This is a valid answer, by virtue of not being a valid answer. \$\endgroup\$
    – primo
    Sep 9, 2014 at 13:31
24
\$\begingroup\$

C
Here's a factorial program that compiles and runs succcessfully (with gcc4.6.3 on Ubuntu 12.04), but invokes as much undefined behaviour according to the C standard as I could cram in. Most are inspired from here. A lot of the remaining legal code is just bad.

int read(char** src, int into){
  int _r;                             //leading underscores reserved, and
  if (!--into) sscanf(*src,"%d",&into); //_r uninitalized
  *(*(--src)+into)=_r>>360;            //shifting more bits than we have
  while (into-->0) (*src)[into]='.'; //modifying const char argv
  printf(*src); // no return statement
}

main(int argc, const char** const argv){
  union  { int x; float y;} result;
  int f,minus1=0xFFFFFFFF,r,a[]={0};
  r=a[3]&2;                     //accessing past end of array
  result.x=read(&argv[r],--r);  //relying on order of arguments
  for(f=*(int*)&result.y;f;f+=minus1) //type punning/invalid union access,
    r*=f;                            //and unsigned overflow
  printf("%d\n",(&r+2)[-2]); //negative array indexes
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Negative indices are perfectly legal if the pointer doesn't point to the start of the array. Heck, it's legal to index a number with an array instead of the other way around. \$\endgroup\$ May 31, 2014 at 6:20
17
\$\begingroup\$

XHTML

<p>
   <div></div>
</p>

The W3C specification (http://www.w3.org/TR/html-markup/p.html#p):

p – paragraph

The p element represents a paragraph.

Permitted contents

Phrasing content

Oh I feel dirty!

Edit: @xfix pointed out that the error I was displaying was actually XHTML. The HTML error this causes is cooler and less obvious such that:

<p><div></div><p> becomes <p /><div></div></p> because the <div> causes the <p> to self close. Thus resulting in an error because we are attempting to close a paragraph that doesn't exist.

\$\endgroup\$
5
  • \$\begingroup\$ This is not an issue here. The real issue is that </p> tag exists without closing it, as <div> automatically closes <p>. \$\endgroup\$ Apr 21, 2014 at 16:03
  • \$\begingroup\$ @xfix Do you have a reference to the specification where this behaviour is defined? The W3 validator will choke on this with the error I would expect, I think you are describing a browser implementation and not HTML itself. \$\endgroup\$ Apr 21, 2014 at 17:27
  • 1
    \$\begingroup\$ You may be thinking of XHTML. In HTML4 and HTML5, <p> is closed by certain tags. For HTML4 validator, I get "end tag for element "P" which is not open". \$\endgroup\$ Apr 21, 2014 at 17:30
  • \$\begingroup\$ @xfix Right you are! I will edit this later. Cheers \$\endgroup\$ Apr 21, 2014 at 18:31
  • \$\begingroup\$ Is it that you've never seen typical Java dev HTML or do you just not want to have to list that many broken standards? \$\endgroup\$ May 30, 2014 at 22:12
12
\$\begingroup\$

HTTP/1.1: Response - Status Code and Reason Phrase

The Status-Code element is a 3-digit...

Need I go any further? This status code isn't one of the codes defined in the standard. It doesn't even begin with one of the required category digits. It's not even 3 digits long.

My browser still manages to load the page fine.

In addition, this answer breaks one of this site's "standards" :)

Output (status line):

HTTP/1.1 0 :)
var http = require("http");
var server = http.createServer(onHttpRequest);
server.listen(80);

function onHttpRequest(request, response)
{
    response.writeHead(0, ":)", { "Content-Type": "text/plain" });
    response.write("Hello, World!");
    response.end();
}

JavaScript (Node)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Out of curiosity, did you try a four-digit status code? Five-digit? \$\endgroup\$
    – Tyler
    Mar 2, 2014 at 6:26
  • \$\begingroup\$ @MatrixFrog I did try a 4-digit code. The browser was fine with it. I went with a 0 because it breaks more of the standard. \$\endgroup\$ Mar 3, 2014 at 13:17
12
\$\begingroup\$

How many standards did I just break?

I did some awful programming indeed here.

PHP

a: goto lol; begin();
b: 
c: echo j; goto h;
d: echo u;
e: echo s;
f: echo t;
g: 
h: echo k; goto o;
i: echo i; goto c;
j: echo l;
k: echo l;
l: echo e;
m: echo d;
n:
o: echo s;
p: echo t; goto u;
q: echo a; goto z;
r: echo n;
s: echo d;
t: echo a;
u: echo r; goto q;
v: echo D; goto i;
w: echo s;
x: 
y:
z: die("!");
lol: goto v;

Easter Egg: The echoed letters, if read vertically, will read just killed standarDs!.

Sidenote: Running this program will output the name of a guy who really loved gotos.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you immediately tell it, it's not an "easter egg"... \$\endgroup\$ Mar 15, 2014 at 19:36
9
\$\begingroup\$

C# - breaking Unicode

Simple algorithm to reverse a string:

public string Reverse(string s)
{
    StringBuilder builder = new StringBuilder();
    for (int i = s.Length - 1; i >= 0; i--)
    {
        builder.Append(s[i]);
    }
    return builder.ToString();
}

This breaks the Unicode standard, because it does not correctly keep surrogate pairs together, creating an invalid string. In .NET and many other platforms/programming languages, a char is not really a character, but a UTF-16 code unit.

NB: It also changes which letter is combined with subsequent combining marks (e.g. diacritics), which may or may not be intended.

\$\endgroup\$
8
\$\begingroup\$

JavaScript

Run it in the console on this page.

var items = [], p = 1, finish = false, intr = setInterval(function() {
    if (p >= 10) finish = true
    $.get(unescape(escape('http://api.stackexchange.com/2.2/answers?page=' + (p++) + '&pagesize=100&order=desc&sort=votes&site=codegolf&filter=!*LVwAFZ.YnaK-KS*')), function(x) {
        items = items.concat(x.items)
        if (finish) {
            clearInterval(intr)
            onFinish()
        }
    })
}, 500)

function onFinish() {
    var item = items[Math.floor(Math.random() * items.length)]
    document.write(item.body)
}

Inspired by

The majority of answers on this site violate at least one coding standard... – Comintern 1 hour ago

What it does is output a random answer out of the top 1000 voted from codegolf.SE (i.e., solving the problem in a very meta fashion!), complete with formatting and all, on your page!


Unfortunately, this technically doesn't satisfy the rules, since the output is the code with the broken standards, so I did break a standard in this code - I used document.write (ewwww). I also have to provide a link, so here: Why is document.write considered a "bad practice"?

If that doesn't count as "breaking the standards," just in case, I wrapped my string in unescape(escape()), which is deprecated, as per MDN.

Note that I'm really just doing all this to get around the rules, and the main point of this answer is its output.


Sample run (click image to enlarge):

screenshot

\$\endgroup\$
1
  • \$\begingroup\$ You should also remove the var statements to make it go against the standards more. \$\endgroup\$
    – Scimonster
    Mar 25, 2015 at 20:06
6
\$\begingroup\$

XML

Not my "invention", I actually have to work with XML's like this that we get from a very secret place:

<?xml version="1.0"?>
<!DOCTYPE data [
  <!ELEMENT data (field2)>
  <!ELEMENT field2 (#PCDATA)>
]>
<data>
  <field1>Rock & Roll</field1>
</data>
<data>
  <field1>Something else</field1>
</data>

Doesn't validate against its own embedded DTD, contains multiple roots and unescaped ampersands. (There are also other higher level errors, ambiguous content model, etc., not demonstrated here.) Very sad.

\$\endgroup\$
3
  • \$\begingroup\$ What's the very secret place? Sounds like some great programming goes on there! ;) \$\endgroup\$
    – George
    Mar 2, 2014 at 20:47
  • \$\begingroup\$ Is it top secret FBI code? \$\endgroup\$
    – user10766
    Mar 2, 2014 at 22:38
  • \$\begingroup\$ hungarian stuff probably :) \$\endgroup\$
    – masterX244
    Mar 3, 2014 at 1:01
6
\$\begingroup\$

Setting the netmask to non-contigous bitmasks was allowed but highly discouraged by RFC 950, but is now forbidden as of RFC 1219. Nevertheless, BSD-likes allow you to do this.

IPv4 netmasks are a combination of four bytes, just like an IP address. It is used to calculate, if two hosts with known IP addresses and netmasks are on the same network. In general an IP address consists of two parts: The network and the host part. Say your network at home is 192.168.1.1 - 192.168.1.254. The host part are the first three bytes of the IP, namely 192.168.1.x. This means the netmask is 255.255.255.0, meaning the first 24 bits are the network part. In binary the mask looks like 11111111.11111111.11111111.00000000. The 1-bits are continous. In my example, the netmask would be 00010111.00000000.00000000.00101010. So what would happen, if the netmask is non-continous? Instead of being at the right end, the host part is scattered over the whole IP address, making it really hard to read, just like my explanation here.

DO NOT expect anything to work after this!

root@Gotthold /v/root# ifconfig en0 inet 47.11.23.42 netmask 23.0.0.42
root@Gotthold /v/root# ifconfig en0 inet
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 8192
    options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
    inet 47.11.23.42 netmask 0x1700002a broadcast 239.255.255.255
\$\endgroup\$
3
  • \$\begingroup\$ Nice, but what is a non-contiguous bitmask? Google does not return anything that's meaningful to me. (Sorry!) \$\endgroup\$
    – George
    Mar 3, 2014 at 14:09
  • \$\begingroup\$ Added some explantions, maybe this helps, I'm not good in explaining networking things. \$\endgroup\$
    – bot47
    Mar 3, 2014 at 22:34
  • 1
    \$\begingroup\$ I've actually encountered (and had to fix a program to disallow) non-continuous netmasks on Windows, due to a user making a typo (225 instead of 252). \$\endgroup\$
    – dan04
    May 30, 2014 at 12:56
6
\$\begingroup\$

C

How many errors can you find, which probably would cause most companies to reject your code (especially good ones). Most of these are probably style mistakes (that I hope nobody does), but some of these are undefined behavior.

    #define BEGIN {
    #define END }
    #define print printf
    void main(int argv, char* argc, char **args) BEGIN
print("hELLO,"); printf("WORLD");
    END

1. It defines macros that make C feel like another language (most codestyle guidelines).
2. The main function returns void, when it should return int (implementation-defined behavior).
3. main uses a form with three arguments, when it's not standard (undefined behavior).
4. Those arguments have incorrect names (most codestyle guidelines).
5. argc (should be argv) has incorrect type. It should be char **, but it's char * (undefined behavior).
6. Mixing C and C++ declaration styles, which differ in * position (most codelines guidelines want consistent code).
7. Strange coding style where the deeper code is deintended (most codestyle guidelines).
8. Using a function (printf) without importing correct header (undefined behavior, as it's a variadic function).
9. Using Caps Lock to write messages (most language guidelines).
10. No space after comma (most language guidelines).
11. Multiple statements on one line (most codestyle guidelines).
12. No new line printed at end, causing the prompt to be drawn on the end of program (implementation-defined behavior).
14. The return value is not defined (implementation-defined behavior).

\$\endgroup\$
5
\$\begingroup\$

Go

package main

func main() {
    println(whereas "Standards are important")
    println("But sometimes it's fun to break them" despiteallobjections)
}

http://play.golang.org/p/DrDHF9EMgu

Explanation is here. The "whereas" and "despiteallobjections" rules don't appear in the spec, but the lexer just skips over them, as kind of an easter egg. I guess the linter is more standards-compliant because if you click "Format" on that Go Playground link, you get a parse error.

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

JavaScript

standards:
while(1)
   break standards

Explanation:

A while loop labeled "standards" is exited.

Standards broken:

standards: <-- that one
while(1)
   break standards
\$\endgroup\$
3
\$\begingroup\$

JavaScript

var a = 1;
var b = a + 2;

It breaks a standard because it doesn't use enough jQuery
The proper way to write this code can be seen here

\$\endgroup\$
2
\$\begingroup\$

GML breaks a ton of standards. One being the beauty of not allowing for use of Ternary operators. Instead of using Ternary Operators, in GML I'd do this:

z = ( y * ( z > 0 ) ) + ( x * ( z <= 0 ) );

Where the Ternary equivalent is:

z = ( z > 0 ) ? y : x;

The first is pretty nasty especially when you start adding in other operations.

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

Haskell

My program doesn't really break any standards, but just randomly selects one to use. It tries to be fair across the different standards. Here is my code.

import System.Random
import Control.Applicative
newlines=["\n", "\r\n", "\r"]
pick::[a]->IO a
pick lst=fmap (lst !!) $ randomRIO (0, length lst - 1)

fairUnlines::[String]->IO String
fairUnlines []         = pure ""
fairUnlines [str]      = pure str
fairUnlines (str:strs) = (\x y z->x++y++z) <$>
    pure str <*> pick newlines <*> fairUnlines strs

fairUnlines will take a list of Strings, and join them using random newline character standards. Also, it is the first time I have actually used the applicative style, on IO none the less.

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

Bash

Taking from your example:

ping 192.168.0.1 -c 1 -s 10000

Assuming your router is at 192.168.0.1

Note: the max ping size is 65kb, so i did 10 kb in stead of 10 mb

\$\endgroup\$
3
  • \$\begingroup\$ Great answer! Is there a Windows version of this (Apart from Cygwin)? \$\endgroup\$
    – George
    Mar 2, 2014 at 20:49
  • \$\begingroup\$ @GeorgeH - not that i know of \$\endgroup\$
    – TheDoctor
    Mar 2, 2014 at 21:05
  • \$\begingroup\$ Ok - Ill have to fire up Virtualbox again (Its been a long time!) \$\endgroup\$
    – George
    Mar 2, 2014 at 21:09
0
\$\begingroup\$

Java

Oh dear, someone forgot the getter again...

import java.lang.reflect.*;

class Main {
    public static void main(String[] args) throws Exception {

        // Brian forgot to implement the getter again.
        // He's on vacation, so this will have to do.

        // TODO: figure out what he named the fields
        Field field = UsefulObject.class.getDeclaredFields()[1];
        Field objField = UsefulObject.class.getDeclaredFields()[0];

        field.setAccessible(true);
        objField.setAccessible(true);

        Object obj = objField.get(null);
        String s = (String) field.get(obj);

        System.out.println(s);
    }
}
class UsefulObject {

    private static UsefulObject Useful;
    private String usefulField;

    static {
        Useful = new UsefulObject("useful field");
    }
    private UsefulObject(String s) {
        this.usefulField = s;
    }

    public String getUsefulField() { return usefulField; }

    public static UsefulObject getUsefulObject() {
        throw new UnsupportedOperationException("TODO");
    }
}

The Useful field should be lowercase: useful

Not sure this is a standard: useless wildcard import (should just be java.lang.reflect.Field since that's all that's being used).

\$\endgroup\$

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