Thread: Proof that professional programmers can be braindead

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    Proof that professional programmers can be braindead

    Well, not necessarily professional programmers, but you can't argue that OpenSSL or procmail aren't or haven't been heavily used programs.

    Whilst reading reddit, I stumbled upon this link: cortesi - Reading Code: In praise of superficial beauty

    It discusses the importance of superficially beautiful code, and also shows several examples of major products whose code is so bad that it might, at least in the case of OpenSSL, make you worried about your privacy.

    If you read further on that link, you might see this link:

    procmail.c

    It is, hands down, the ugliest piece of code I have ever seen. I actually tried tracing through the code, but I went nowhere.

    However, the first place of ........ups still goes to OpenSLL, for the if statement with 0 as a condition, and a goto label inside it, to guard the code inside it from ever getting executed unless you jump into it.

    I guess there is one thing that is certain, though. No sane man would be able to actually derive any information from the OpenSSL code that could potentially expose vulnerabilities. If they're there, you'll have to fuzz them out. Code analysis won't do you good. It might send you to a mental health institution.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The first time I heard of a "fencepost error" it involved a piece of historical SSL code that had been exploited and subsequently corrected.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    Quote Originally Posted by IceDane View Post
    However, the first place of ........ups still goes to OpenSLL, for the if statement with 0 as a condition, and a goto label inside it, to guard the code inside it from ever getting executed unless you jump into it.
    What's wrong with it? It's a well known technique considering C has no exception handling. It's not just some cheap horror trick.

    Sometimes you see things like this in C:

    Code:
    int func()
    {
      ...
      if(ptr_a == NULL) goto oops; // oops, go to exception handler
      ...
      return 0; // normal return
    oops:
      // this handles an 'exception'  
      ...
    }
    This is essentially a cheap form of exception handling. It's an easy way to keep the flow and logic behind what can go wrong clear to the programmer (goto's are probably most used for things of this nature today.)

    There's a problem however, how do you do this in nested scope?

    Code:
    int func2()
    {
      if(ptr_a == NULL) {
       /// ????
      }
      ...
    }
    The problem is that at the point of ?????, if an 'exception' happens, you need to continue by eventually moving out of the scope of the if and returning to the normal program flow. But you can't just put a return at ??? followed by a block with your 'exception handler': the only way to get out of scope is to fall off the end, so where can you put your exception handler, without executing it, while getting out of the scope?

    You model try, catch and finally:

    Code:
    int func3()
    {
      if(ptr_a != NULL) {
        ...
        // try
        if(ptr_b == NULL) goto oops;  
    
        if(0) {
          oops:
          // 'catch' block
        }
        // finally
        ...
      }
    
    }
    The if(0) perfectly solves this problem of dealing with this problem inside nested scope (or out of it, for that matter,) and it lets you continue the normal program flow after the 'exception handling' has happened. It may seem awkward, but it's a decent representation of exception handlers in C when you need it. You could use macros to make it look nicer if you want:

    Code:
    #define THROW(x) goto x
    #define CATCH if(0)
    
    int func4()
    {
      if(a == 2) {
    
        if(screwed) THROW(got_screwed);
        ...
        CATCH { got_screwed:
          ...
        }
        ...
      }
      ..
    }
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It is kinda ugly the way they cram so many statements onto one line, however I'd hate to see how long and stringy the code would be if it was spaced out nicely. At least the way it is it's easy to visualize the blocks the code has been broken into.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's like esbo actually wrote procmail....

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Huh, I thought you're not allowed to jump into control blocks ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Mad_guy View Post
    This is essentially a cheap form of exception handling. It's an easy way to keep the flow and logic behind what can go wrong clear to the programmer (goto's are probably most used for things of this nature today.)
    I think Linus himself does that too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  2. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  3. Absolute proof of M$ programmers idiocy
    By Stoned_Coder in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-10-2003, 01:22 PM
  4. professional programmers, do you spend more time writing or modifying code
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 11-25-2002, 10:54 PM
  5. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM