Wrong-but-not-incorrect code

Discussion in 'C Programming' started by Dave Vandervies, Mar 17, 2005.

  1. Seen in a chunk of code I was looking at recently (not mine!):
    --------
    char* imgfilename[100];
    sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    ReadImage((char*)imgfilename);
    --------
    (ReadImage is another part of the program's code that does exactly what
    the reasonable reader would expect.)

    For the CLC readers:
    Can you, by artifical construction or actual experience, come up with
    something that's more Wrong and yet still manages to be correct code
    that performs the intended task?


    dave

    --
    Dave Vandervies
    It is true that their choices are not always consonant with the choices _I_
    would make, but that's because they're cats and I'm not.
    --Mike Andrews in the scary devil monastery
     
    Dave Vandervies, Mar 17, 2005
    #1
    1. Advertising

  2. Dave Vandervies

    Eric Sosman Guest

    Dave Vandervies wrote:
    > Seen in a chunk of code I was looking at recently (not mine!):
    > --------
    > char* imgfilename[100];
    > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > ReadImage((char*)imgfilename);
    > --------
    > (ReadImage is another part of the program's code that does exactly what
    > the reasonable reader would expect.)
    >
    > For the CLC readers:
    > Can you, by artifical construction or actual experience, come up with
    > something that's more Wrong and yet still manages to be correct code
    > that performs the intended task?


    My personal favorite is this one-liner a colleague
    found many years ago:

    #define HASHSIZE 51 /* a small prime */

    --
     
    Eric Sosman, Mar 17, 2005
    #2
    1. Advertising

  3. Dave Vandervies

    Allan Bruce Guest

    >
    > My personal favorite is this one-liner a colleague
    > found many years ago:
    >
    > #define HASHSIZE 51 /* a small prime */
    >
    > --
    >


    He obiously doesnt know his 3 time table above 3x10 ;-)
    Allan
    >
     
    Allan Bruce, Mar 17, 2005
    #3
  4. Dave Vandervies wrote:

    > Seen in a chunk of code I was looking at recently (not mine!):
    > --------
    > char* imgfilename[100];
    > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > ReadImage((char*)imgfilename);
    > --------
    > (ReadImage is another part of the program's code that does exactly what
    > the reasonable reader would expect.)
    >
    > For the CLC readers:
    > Can you, by artifical construction or actual experience, come up with
    > something that's more Wrong and yet still manages to be correct code
    > that performs the intended task?
    > ...


    signed char direction; /* either -1 or +1 */
    unsigned offset; /* absolute distance */
    T* ptr;

    ...
    ptr += direction * offset;
    /* Moving the pointer in direction specified by 'direction' */

    In general case the code does not do what it was intended to do, since
    the right hand side will be promoted and evaluated within the bounds of
    'unsigned' type. However, on a 32 bit platform the code "worked" - the
    pointer value wrapped around and ended up exactly where it would be if
    code worked as intended. On a 64 bit platform (32-bit 'unsigned' and
    64-bit pointers) the code started to fail for obvious reasons.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 17, 2005
    #4
  5. Dave Vandervies wrote:
    > Seen in a chunk of code I was looking at recently (not mine!):
    > --------
    > char* imgfilename[100];
    > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > ReadImage((char*)imgfilename);
    > --------
    > (ReadImage is another part of the program's code that does exactly what
    > the reasonable reader would expect.)
    >
    > For the CLC readers:
    > Can you, by artifical construction or actual experience, come up with
    > something that's more Wrong and yet still manages to be correct code
    > that performs the intended task?


    Your example itself could easily be made Worse. That sprintf call, for
    example, is missing some casts, innit? And both that and the ReadImage
    call could used a (void), innit?
     
    Martin Ambuhl, Mar 17, 2005
    #5
  6. Dave Vandervies

    Chris Torek Guest

    In article <d1cv00$huf$>
    Dave Vandervies <> wrote:
    >Can you, by artifical construction or actual experience, come up with
    >something that's more Wrong and yet still manages to be correct code
    >that performs the intended task?


    I have no idea how to rate the "wrongness" on a scale that
    affords comparisons, but I once put a typo in my code so
    that instead of:

    if (expr && var)

    I had:

    if (expr &*& var)

    The code still worked because the expression (whatever it was)
    produced either 0 or 1 as its result -- it was perhaps something
    like "a == b" -- and "var" was also logically boolean, holding
    only either 0 or 1. Hence:

    if (a == b &*& c)

    was:

    if ((a == b) & (*&c))

    which was:

    if ((a == b) & c)

    which meant the same thing as the intended line.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Mar 17, 2005
    #6
  7. Dave Vandervies

    Kenneth Bull Guest

    Dave Vandervies wrote:
    > Seen in a chunk of code I was looking at recently (not mine!):
    > --------
    > char* imgfilename[100];
    > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > ReadImage((char*)imgfilename);
    > --------
    > (ReadImage is another part of the program's code that does exactly

    what
    > the reasonable reader would expect.)
    >
    > For the CLC readers:
    > Can you, by artifical construction or actual experience, come up with
    > something that's more Wrong and yet still manages to be correct code
    > that performs the intended task?
    >
    >


    This isn't technically wrong but I thought I'd mention it. I knew this
    chap who really loved GOTO statements, but upon entering a job he
    realized the company's coding practices didn't allow them. So instead
    he dropped a bunch of JUMPahead and JUMPbehind statemetns in his code
    as a replacement. In a header file he hid the following:

    jumpcommands.h
    ===============

    #define JUMPahead goto
    #define JUMPbehind goto

    -------------------------

    Makes you wonder why I.Q. tests are given to job applicants anymore.
    BTW, this chap had a lot of fancy -qualifications-. If you ask me, the
    onyl qualification that matters is the one between someone's ears.
     
    Kenneth Bull, Mar 18, 2005
    #7
  8. Dave Vandervies

    CBFalconer Guest

    Eric Sosman wrote:
    >

    .... snip ...
    >
    > My personal favorite is this one-liner a colleague
    > found many years ago:
    >
    > #define HASHSIZE 51 /* a small prime */


    A few years ago, in this very newsgroup, 91 enjoyed similar fame.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Mar 18, 2005
    #8
  9. Eric Sosman wrote:
    >
    > Dave Vandervies wrote:
    > > Seen in a chunk of code I was looking at recently (not mine!):
    > > --------
    > > char* imgfilename[100];
    > > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > > ReadImage((char*)imgfilename);
    > > --------
    > > (ReadImage is another part of the program's code that does exactly what
    > > the reasonable reader would expect.)
    > >
    > > For the CLC readers:
    > > Can you, by artifical construction or actual experience, come up with
    > > something that's more Wrong and yet still manages to be correct code
    > > that performs the intended task?

    >
    > My personal favorite is this one-liner a colleague
    > found many years ago:
    >
    > #define HASHSIZE 51 /* a small prime */
    >

    "If 91 were prime, it would be a counterexample to your conjecture."
    -- Bruce Wheeler

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond It is moral cowardice to leave |
    | undone what one perceives right |
    | richmond at plano dot net to do. -- Confucius |
    +----------------------------------------------------------------+
     
    Charles Richmond, Mar 18, 2005
    #9
  10. Dave Vandervies

    pete Guest

    Allan Bruce wrote:
    >
    > >
    > > My personal favorite is this one-liner a colleague
    > > found many years ago:
    > >
    > > #define HASHSIZE 51 /* a small prime */
    > >
    > > --
    > >

    >
    > He obiously doesnt know his 3 time table above 3x10 ;-)
    > Allan


    I add up the digits to see if a number is divisible by 3.
    (5 + 1) % 3 == 0

    --
    pete
     
    pete, Mar 18, 2005
    #10
  11. In article <>,
    Chris Torek <> wrote:
    >In article <d1cv00$huf$>
    >Dave Vandervies <> wrote:
    >>Can you, by artifical construction or actual experience, come up with
    >>something that's more Wrong and yet still manages to be correct code
    >>that performs the intended task?

    >
    >I have no idea how to rate the "wrongness" on a scale that
    >affords comparisons,


    I was thinking along the lines of "invokes a `That can't POSSIBLY be
    what it's supposed to say!' response, optionally accompanied by sever
    aesthetic revulsion". Not well-defined or objective enough to make a
    competition out of it, but comparing according to how well it matches
    that is useful enough for war stories or language lawyering.


    > but I once put a typo in my code so
    >that instead of:
    >
    > if (expr && var)
    >
    >I had:
    >
    > if (expr &*& var)
    >
    >The code still worked because the expression (whatever it was)
    >produced either 0 or 1 as its result -- it was perhaps something
    >like "a == b" -- and "var" was also logically boolean, holding
    >only either 0 or 1.


    Impressive. (But not nearly as impressive as if you'd had a plausible
    reason for writing it that way intentionally.)


    dave

    --
    Dave Vandervies
    If the committee members can't agree over it, what chance have I got of
    defending /either/ position?
    --Richard Heathfield in comp.lang.c
     
    Dave Vandervies, Mar 18, 2005
    #11
  12. On Fri, 18 Mar 2005 02:44:41 GMT, CBFalconer
    <> wrote:

    > Eric Sosman wrote:
    >>

    > ... snip ...
    >>
    >> My personal favorite is this one-liner a colleague
    >> found many years ago:
    >>
    >> #define HASHSIZE 51 /* a small prime */

    >
    > A few years ago, in this very newsgroup, 91 enjoyed similar fame.


    With a bit more reasonableness, since 13*7 is harder to recognise as
    non-prime than 3*17...

    Chris C
     
    Chris Croughton, Mar 18, 2005
    #12
  13. Dave Vandervies

    Chris Dollin Guest

    Eric Sosman wrote:

    >
    >
    > Dave Vandervies wrote:
    >> Seen in a chunk of code I was looking at recently (not mine!):
    >> --------
    >> char* imgfilename[100];
    >> sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    >> ReadImage((char*)imgfilename);
    >> --------
    >> (ReadImage is another part of the program's code that does exactly what
    >> the reasonable reader would expect.)
    >>
    >> For the CLC readers:
    >> Can you, by artifical construction or actual experience, come up with
    >> something that's more Wrong and yet still manages to be correct code
    >> that performs the intended task?

    >
    > My personal favorite is this one-liner a colleague
    > found many years ago:
    >
    > #define HASHSIZE 51 /* a small prime */


    The writer clearly didn't play enough card games.

    --
    Chris "electric hedgehog" Dollin
     
    Chris Dollin, Mar 18, 2005
    #13
  14. Dave Vandervies

    Eric Sosman Guest

    Allan Bruce wrote:

    >> My personal favorite is this one-liner a colleague
    >>found many years ago:
    >>
    >>#define HASHSIZE 51 /* a small prime */
    >>--
    >>

    >
    > He obiously doesnt know his 3 time table above 3x10 ;-)
    > Allan


    Or perhaps he was writing in decimal but thinking
    in octal. "3x10 -- that's two dozen, right?"

    (Exculpatory note: The colleague I mention was the
    finder of the bug, not its author.)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 18, 2005
    #14
  15. Dave Vandervies

    Ben Pfaff Guest

    Chris Dollin <> writes:

    > Eric Sosman wrote:
    >> My personal favorite is this one-liner a colleague
    >> found many years ago:
    >>
    >> #define HASHSIZE 51 /* a small prime */

    >
    > The writer clearly didn't play enough card games.


    Are there many card games that call for a deck one card short?
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Mar 18, 2005
    #15
  16. Dave Vandervies

    Rob Thorpe Guest

    (Dave Vandervies) wrote in message news:<d1cv00$huf$>...
    > Seen in a chunk of code I was looking at recently (not mine!):
    > --------
    > char* imgfilename[100];
    > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > ReadImage((char*)imgfilename);
    > --------
    > (ReadImage is another part of the program's code that does exactly what
    > the reasonable reader would expect.)
    >
    > For the CLC readers:
    > Can you, by artifical construction or actual experience, come up with
    > something that's more Wrong and yet still manages to be correct code
    > that performs the intended task?
    >
    >
    > dave


    There is some particularly contorted code in the message cracker
    header windowsx.h in MS Windows that works. For instance:-

    #define HANDLE_WM_INITDIALOG(hwnd, wParam, lParam, fn) \
    (LRESULT)(DWORD)(UINT)(BOOL)(fn)((hwnd), (HWND)(wParam), lParam)

    Not that this is totally wrongheaded unlike the code you mentioned
    above. It's just mildly derranged.

    It's difficult looking at pieces of code like that you posted.
    Sometimes pieces work, but are bad style, others work but are
    undefined behaviour, still others are bugs but just happen to work.
    When code is contorted it's very hard to tell which is which.
     
    Rob Thorpe, Mar 18, 2005
    #16
  17. In article <>,
    Ben Pfaff <> wrote:

    >>> #define HASHSIZE 51 /* a small prime */


    >> The writer clearly didn't play enough card games.


    >Are there many card games that call for a deck one card short?


    When you deal a pack of 52 cards between three players, you have one
    left over.

    -- Richard
     
    Richard Tobin, Mar 18, 2005
    #17
  18. Dave Vandervies

    Chris Dollin Guest

    Ben Pfaff wrote:

    > Chris Dollin <> writes:
    >
    >> Eric Sosman wrote:
    >>> My personal favorite is this one-liner a colleague
    >>> found many years ago:
    >>>
    >>> #define HASHSIZE 51 /* a small prime */

    >>
    >> The writer clearly didn't play enough card games.

    >
    > Are there many card games that call for a deck one card short?


    I don't know about "many", but there are several three-handed games
    where all-but-one of the cards are dealt out, leaving the players
    with 17 each. The left-over card may be left concealed, or it may
    be exposed, or may be exchanged; it may determine the trump suit.

    The two that immediately come to mind are Black Maria aka Hearts,
    and Nullos. (Stares into space.) And Ninety-Nine.

    --
    Chris "electric hedgehog" Dollin
     
    Chris Dollin, Mar 18, 2005
    #18
  19. Dave Vandervies

    Tor Rustad Guest

    "Dave Vandervies" <> wrote in message
    news:d1cv00$huf$...
    > Seen in a chunk of code I was looking at recently (not mine!):
    > --------
    > char* imgfilename[100];
    > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
    > ReadImage((char*)imgfilename);
    > --------
    > (ReadImage is another part of the program's code that does exactly

    what
    > the reasonable reader would expect.)
    >
    > For the CLC readers:
    > Can you, by artifical construction or actual experience, come up with
    > something that's more Wrong and yet still manages to be correct code
    > that performs the intended task?


    Did a code audit once, of a program that had passed the integration
    testing phase. It was a 3000 liner in a single source file. Well, that
    happens... after scanning through +200 lines of globals.. I finally came
    to the main.. puuuh!

    The main() function was a +1500 liner, full of duplicate and
    complicated rollback code....

    Oh no... this wasn't any student code, the programmer had a CS
    master degree and multiple years of C/C++ programming
    experience. He had spent 3 months on this module. <g>

    --
    Tor <torust AT online DOT no>
     
    Tor Rustad, Mar 18, 2005
    #19
  20. On Fri, 18 Mar 2005 16:26:33 +0000, Chris Dollin
    <> wrote:

    > Ben Pfaff wrote:
    >
    >> Chris Dollin <> writes:
    >>
    >>> Eric Sosman wrote:
    >>>> My personal favorite is this one-liner a colleague
    >>>> found many years ago:
    >>>>
    >>>> #define HASHSIZE 51 /* a small prime */
    >>>
    >>> The writer clearly didn't play enough card games.

    >>
    >> Are there many card games that call for a deck one card short?

    >
    > I don't know about "many", but there are several three-handed games
    > where all-but-one of the cards are dealt out, leaving the players
    > with 17 each. The left-over card may be left concealed, or it may
    > be exposed, or may be exchanged; it may determine the trump suit.
    >
    > The two that immediately come to mind are Black Maria aka Hearts,


    One of a number of games by both names, some are four-hand (the 'Hearts'
    program distributed with Windows is one of the latter, but I first
    played the same game four-handed some 30 years ago using the name "Black
    Maria").

    > and Nullos. (Stares into space.) And Ninety-Nine.


    There's also at least a couple where 16 cards are dealt to each player,
    with 4 being left to exchange.

    I've also played a three-hand version of Whist with the remaining card
    being used to determine which suit was trumps. That was 30-odd years
    ago as well (it was in a card games book which was at least 20 years
    older than that)...

    Chris C
     
    Chris Croughton, Mar 18, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl Gilbert
    Replies:
    14
    Views:
    579
    Tom Blantern
    May 21, 2004
  2. Sullivan WxPyQtKinter
    Replies:
    1
    Views:
    418
    Tim Roberts
    Apr 7, 2006
  3. David Sudolcan
    Replies:
    7
    Views:
    2,174
    Eric Sosman
    Apr 4, 2011
  4. techy techno
    Replies:
    3
    Views:
    190
    Chris Barber
    Feb 24, 2004
  5. Carl Gilbert
    Replies:
    14
    Views:
    234
    Tom Blantern
    May 21, 2004
Loading...

Share This Page