Write Buggy Code

17

1

Now is the time to show off your abilities to write bad code. I am trying out a new sort of programming puzzle, most similar, I think, to the underhanded C contest. The main difference is that this is not nearly as nefarious: it's just some good clean fun. The goal of the puzzle is to pack as many bugs as you can into a program. The winner of this contest is the one who writes the program with the most bugs per character.

To avoid a huge thread of comments asking for clarification, I should define right now what I consider to be qualifying bugs.

First, a bug is not an error. If it is a problem that can be detected by the interpreter as an error (e.g. mismatched delimeters, badly-formed syntax, acessing a property of a null object, etc.) or if it prevents the program from executing or continuing, it is not a bug. Otherwise, you could type in four characters and the interpreter could list eight syntax errors and you could claim a bug-character ratio of 2.

Second, the bug must not be obviously wrong and a bug is not an easter egg. This is certainly a subjective criterion, but I think essential to this sort of contest. This means that you cannot have conditional code that specifically mangles the code in obvious ways. (Read: use a turing pit language, because nobody will know the difference).

Third, the bug must be plausible. This is subjective, like the one above, but the bug must look like it could have been written by a less-than-meticulous or perhaps ignorant person, or someone who just made a mistake. This includes, for example, off-by-one errors or syntax that is valid and looks correct but causes undesired behavior (say, using square brackets instead of parentheses).

The bug can cause any sort of undesired behavior to the program, including, but certainly not limited to, undesired output for some exceptional cases, have different behavior based on something that is seemingly unrelated (e.g. output displays differently depending on whether the current time ends with an odd or even number of seconds), memory leaks, loss of data, and so on.

Example Problem:

Make a program that displays all of the ASCII characters in ascending order of their numerical value.

Example answer:

Brainf***, 5 chars, 1 bug, 0.2 bug-char ratio

+[+.]

Bug: does not display the ASCII character for 1. Could be fixed by changing to .+[.+].

Ok, I think you should mostly have gotten it by now, here is your puzzle:

Decode a Caesar Cipher and Sort the Words Alphabetically

A caesar cipher is created by taking a series of letters and shifting them n letters over in the alphabet. If it goes all the way to the beginning or end of the alphabet, A comes after Z, and Z comes before A. For example:

Mannequin
Nboofrvjo //Shifted over 1 or -25
Wkxxoaesx //Shifted over 10 -16
Ftggxjnbg //Shifted over -7 or 19

You will be given two inputs (you can get input however is most convenient for you, within reason). The first input is the words, and the second input is the value it is shifted over. Your task is to output the decoded words, and then output the decoded words after they have been sorted alphabetically.

Example (no offense to bad boys, it's just an example):

First input: gtdx wjbfwiji. ljy Gfi hfssty

Second input: 5

First output: boys rewarded. get Bad cannot

Second output: Bad boys cannot get rewarded.

Good luck!

Peter Olson

Posted 2011-04-26T05:31:38.857

Reputation: 7 412

Question was closed 2017-02-18T19:12:05.513

Your example's second input is not -5 ? – YOU – 2011-04-26T06:55:25.320

@S.Mark: The input is 5 because the task is to decode the cipher. – Nabb – 2011-04-26T07:26:08.703

Ah, I see. @Nabb, Thanks! – YOU – 2011-04-26T07:30:22.127

Do we have to support both capital and lowercase letters? – Joey Adams – 2011-04-26T16:21:58.343

@Joey, No, but you can try and put in a capitalization bug if you want. – Peter Olson – 2011-04-26T19:08:08.690

1

@Peter: I've opened a new discussion on meta that concerns this question (among others). Perhaps you would like to comment.

– dmckee --- ex-moderator kitten – 2011-04-27T02:26:47.420

Answers

14

Ruby, 136 characters, 7 bugs, ratio = 0.051

o=gets[/\d+/].to_i
$,='\s'
a=gets.split(/ /).map{|w|w.gsub(/[^.,:;?!]/){(97+($&.ord-97-o)%26).chr}}
print a.join,a.sort_by{|a|a[0]}.join
  1. [/\d+/]: negative numbers have their sign removed (a seeming attempt at input validation)

  2. '\s': backlash escapes are only interpreted within double-quoted strings, so this won't generate a space but rather a literal \s

  3. split(/ /): unlike a plain split, this won't split on newlines (so the last word will keep the newline)

  4. /[^.,:;?!]/: this regex excludes punctation, but not uppercase characters, digits or underscores, and, most critically, newlines

  5. 97: anything other than punctation or lowercase letters will get garbled

  6. sort_by{|a|a[0]}: apparently the programmer didn't know about sort, and instead used this ridiculous method, which doesn't sort words starting with the same letter

  7. print: unlike puts, doesn't print a newline between each argument (so the strings come out glued together)

Lowjacker

Posted 2011-04-26T05:31:38.857

Reputation: 4 466

9

I'm not going to accept my own answer, but I thought I would show you the ultimate buggy sorting decipherer. The great thing about it is I didn't even plan most of the bugs.

Brainf***: 483 chars, 11 bugs

,------------------------------------------------[>,---------]<[<]>[>[
->]<[<]>-]>[+++++++++.>]<[---------------------------<]>>[[>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-]>]>[[>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<-]>]>[[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<-]>]>[[>>>>>>>>>>>+<<<<<<<<<<<-]>]++++++++++.>[[>>>>>>
>>>>>>>>>>+<<<<<<<<<<<<<<<<-]>]>[>+++++++++++++++++++++++++++.]

Input:

5 gtdx wjbfwiji. ljy Gfi hfssty 

Output:

bo_sre]arded)getBadcannot
adbo_scannotgetre]arded)

List of bugs:

Input/Display bugs:

  1. Nonpositive numbers or numbers that are more than one digit break the program.

  2. The decipherer does not make Z come before A. It just subtracts the ASCII character value.

  3. Spaces appear as the ASCII ESC (27) character.

  4. If the input is not terminated by a tab, the program does not continue after the input instructions.

  5. The program has to be manually terminated. It will continually display the ESC character until stopped.

  6. The program will break if the input file is not ASCII encoded.

  7. The program does not display the first character of the sorted output.

Sorting bugs:

I implemented the sorting extremely naively.

  1. The program breaks when the number of words does not equal 5.

  2. The program breaks if the number of bytes of input exceeds 60.

  3. The program can only sort correctly if the alphabetical order is identical to the example input.

  4. The program adds extra spaces if any of the words are smaller than the example input and overwrites characters if any of the words are longer.

I have a bug-char ratio of 0.0228. Admittedly, Joey beat me, but I take pride in the fact I only used 8 different characters in my program, and my bugs are much more critical.

Peter Olson

Posted 2011-04-26T05:31:38.857

Reputation: 7 412

2re]arded? Sounds serious. – Joe Z. – 2013-03-14T15:34:11.333

7

C - 224 characters, 2 bugs, 7 cases of undefined behavior

Edit: My assessment here is incorrect. Overflowing an unsigned integer is, in fact, well-defined in C. Moreover, comparison between signed and unsigned is well-defined too, but the compiler warns because the way it's defined may not be what you think.

m(char**a,char**b){return strcmp(*a,*b);}main(int c,char*v[]){unsigned i,j
,o;o=atoi(v[1])+19;for(i=2;i<c;i++)for(j=0;j<=strlen(v[i])-1;j++)v[i][j]=(
tolower(v[i][j])-o)%26+97;qsort(v,c,sizeof(v),m);for(i=2;i<c;puts(v[i++]));}

Usage:

$ ./a.out 5 gtdx wjbfwiji ljy Gfi hfssty
bad
boys
cannot
get
rewarded

Breakdown:

m(char**a,char**b){return strcmp(*a,*b);}
main(int c,char*v[])
{
    unsigned i,j,o;

    // Undefined behavior if o is assigned negative value.
    o=atoi(v[1])+19;

    // Comparison between signed and unsigned integer.
    for(i=2;i<c;i++)
        // * Bug: if strlen(v[i]) is zero, subtraction will overflow
        //        unsigned value, and loop will have extra iterations.
        // * strlen is implicitly declared.
        //   Undefined behavior if sizeof(void*) != sizeof(int)
        for(j=0;j<=strlen(v[i])-1;j++)
            // tolower expects either an unsigned char casted to an int, or EOF.
            // Thus, undefined behavior if v[i][j] is non-ASCII.
            v[i][j]=(tolower(v[i][j])-o)%26+97;

    // * Bug: Program name and offset are included in the sort.
    //        "./a.out" and "5" both happen to be less than lowercase strings.
    // * qsort is implicitly declared.
    //   Undefined behavior if sizeof(void*) != sizeof(int)
    qsort(v,c,sizeof(v),m);

    // Comparison between signed and unsigned integer.
    for(i=2;i<c;puts(v[i++]));
}

Joey Adams

Posted 2011-04-26T05:31:38.857

Reputation: 9 929

Good job for breaking the ice. I'll say you have a 0.0402 bug-char ratio. – Peter Olson – 2011-04-26T19:11:50.933

@Peter Does that include comments and whitespace/CRLFs? – Mateen Ulhaq – 2011-04-26T21:33:23.970

@muntoo No. It is 9/224. – Peter Olson – 2011-04-26T21:37:47.220

But shouldn't it output the unsorted text as well? – Lowjacker – 2011-04-26T23:29:59.323

5

JavaScript: 403 characters, 8 bugs, ratio = 0.0199

Although not as bad as C, JavaScript does have design flaws that can lead to bugs, at least when used by a novice (demo with all bugs unfixed).

function W(v){D.write("<input value="+v+">");return D.body.lastChild}function R(J){I=S.indexOf(C=i.value[J]);H=s.value;if(!I){o.value+=C}if(H>0){o.value+=S[I+H]}if(H<0){o.value+=S[I-26-H]}p.value=o.value.split(" ").sort().join(" ");setTimeout("R("+(J+1)+")",99)}D=document;S="ZYXWVUTSRQPONMLKJIHGFEDCBA";S+=S;S+=S.toLowerCase();i=W("input");s=W("shift");W("run type=button onclick=R(0)");o=W("");p=W("")

function W(v) {
    D.write('<input value=' + v + '>');
    return D.body.lastChild;
}

function R(J) {
    I = S.indexOf(C = i.value[J]);
    H = s.value;
    if(!I) o.value += C;
    if(H > 0) o.value += S[I + H];
    if(H < 0) o.value += S[I - 26 - H];
    p.value = o.value.split(' ').sort().join(' ');
    setTimeout('R(' + (J + 1) + ')', 99);
}

D = document;

S = 'ZYXWVUTSRQPONMLKJIHGFEDCBA';
S += S;
S += S.toLowerCase();

i = W('input');
s = W('shift');
W('run type=button onclick=R(0)');
o = W('');
p = W('');

  1. I + H is string concatenation, not addition: undefinedundefinedundefined...
  2. !I is not the correct way to check the return value of .indexOf(), which returns -1 for a non-match: boysVrewardedVV...
  3. Missing else keywords: boys Vrewarded.V Vget...
  4. Will not stop at end of input: ...cannotundefinedundefined...
  5. Won't correctly handle zero shift (e.g. just trying to use the program to sort words)
  6. Will it handle a negative shift? Not correctly.
  7. Double-clicking the button causes a second timeout to start, leading to incorrect output (demo with most of the previous bugs fixed):
    boys rebwoayrsd erde.w agredte dB.a dg ecta nBnaodt cannot.
  8. Output text boxes must be empty when the button is clicked

Also note that this will not work on older versions of IE because it uses an extension to ECMAScript 3 that was only standardized in ES5.

PleaseStand

Posted 2011-04-26T05:31:38.857

Reputation: 5 369

2Good call on not including lack of IE support in your list of bugs, I would have been quite tempted to add it, and I almost suggested it because it is a very common "bug" in web development, until I thought about whether or not lack of support for Netscape 4 (or any arbitrary browser) would really be a bug. – Peter Olson – 2011-04-27T05:19:33.493

Well, lack of support for an obsolete browser certainly hurts no one, but lack of support for a current one would. Unless you're a hip and totally rad Web 2.0 company creating stuff at the bleeding edge of technology until it bleeds out I'd say it is a bug. (Re-read the answer; lack of support for older versions is likely no bug, then. XP is long out of mainstream support and all supported Windows versions have a decent version of IE to run which even comes through automatic updates). – Joey – 2011-04-27T13:57:50.477

1

Python3 184 characters, 4 bugs. Bug ratio 0,0217

import string
d=input()
y=int(input())
print("".join(sorted(d.translate(str.maketrans("".join([chr(x)for x in range(96,123)]),"".join([chr(96+(x+y)%26)for x in range(26)]))).split())))

degolfed:

data = input() #string to convert
y = int(input()) #value to shift
print("".join(
    sorted(
        data.translate(
            str.maketrans(
                "".join([chr(x) for x in range(96,123)]),
                "".join([chr(96+(x+y)%26) for x in range(26)]))
            ).split()
        )))

Example input: gtdx wjbfwiji. ljy Gfi hfssty
Example input: -5
Example output: Gcanxrbmmnsfdsqdv`qcdc.

Known bugs:

  1. Does not convert upper-case characters
  2. Includes ` and converts to/from it.
  3. Does not convert non-ascii characters (åäö)
  4. Does not print spaces.
  5. Can handle, but ignores, punctuation -- I choose not to count this, but if I do my ratio goes to 0.027)

I'm not very good at coming up with bugs deliberately.

Robert S.

Posted 2011-04-26T05:31:38.857

Reputation: 123