Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Oct 2012

Real Security

by in CodeSOD on

Emmett had been contracted to maintain the curb appeal of The Website of Judy S. Kirkland (#1 Realtor in Townhouse Resales in the Upper Eastern Greater Metro Area, February 2009).

"It's a charming homepage," he was told by Judy S. Kirkland (Bay Street Diamond Award recipient, thrice). "I got in on the ground floor with this The Internet thing way back when. After all, one only becomes as recognizably successful as myself by staying ahead of the trends."


Blame Peter

by in CodeSOD on

We’ve all had the moment when we search for an answer to a technical question and our search engine suggests a trip to ExpertSexchange.com. Er, ExpertsExchange.com. And when we realize that they expect us to pay to see the answer, our instincts take over- we scrub our way through the page source hoping to see a way around the paywall.

If your question was, “Who do I blame?”, that’s one answer ExpertsExchange will give you for free.


Not Good Enough for Paul

by in CodeSOD on

"If I told you that I work for a primarily Microsoft coding shop that hires on non-Microsoft developers, you'd think that it would be an unending, seething mass of WTF code," writes Hank, "but really, it isn't."

"The new hires that the company brings on are usually eager to learn and their diverse backgrounds and experiences make our team more well-rounded overall."


Space: The Final NOP

by in CodeSOD on

Some code demands a little breathing room. Phil found this block of C#, which makes sure it has plenty of space to do its work.

public string AddSpace(string value)
       {
            string space = " ";
           if (value.Equals(space))
           {
               return space;
           }

           return value;
       }

The White Flag

by in CodeSOD on

Handling dates is hard. Kevin sends us this attempt at building it from scratch, found in a third-party library. It was shaped with a Tab key that wishes nightly for death.

Protected Shared Function dateTimeFromJavaXml(ByVal dt As String) As DateTime  
   Dim datetime1 As DateTime = DateTime.get_Now()  
   If (dt.EndsWith(" GMT-12:00")) Then
       dt = dt.Replace(" GMT-12:00", "-12:00")  
   ElseIf (dt.EndsWith(" GMT-11:00")) Then
       dt = dt.Replace(" GMT-11:00", "-11:00")
       GoTo label1
       If (dt.EndsWith(" WSST")) Then
           dt = dt.Replace(" WSST", "-11:00")
       ElseIf (dt.EndsWith(" SDT")) Then
           dt = dt.Replace(" SDT", "-11:00")
           GoTo label2
           If (dt.EndsWith(" NUST")) Then
               dt = dt.Replace(" NUST", "-11:00")
           ElseIf (dt.EndsWith(" GMT-10:00")) Then
               dt = dt.Replace(" GMT-10:00", "-10:00")
               GoTo label3  
'SNIP: Let's skip ahead, retaining the indenting…      scroll that way ->                                                  ->                       keep going                                               ->                                                                        and going                                                oh, there it is!
                                                                                                                                                                                                                                                                                                                                                               If (Else.EndsWith(" TOST")) Then
                                                                                                                                                                                                                                                                                                                                                                   dt = dt.Replace(" TOST", "+13:00")
                                                                                                                                                                                                                                                                                                                                                               ElseIf (dt.EndsWith(" GMT+14:00")) Then
                                                                                                                                                                                                                                                                                                                                                                   dt = dt.Replace(" GMT+14:00", "+14:00")
                                                                                                                                                                                                                                                                                                                                                                   GoTo label88
                                                                                                                                                                                                                                                                                                                                                                   If (Else.EndsWith(" LINST")) Then
                                                                                                                                                                                                                                                                                                                                                                       dt = dt.Replace(" LINST", "+14:00")
                                                                                                                                                                                                                                                                                                                                                                   Else
                                                                                                                                                                                                                                                                                                                                                                       dt = dt.Substring(0, (dt.LastIndexOf(" ") - 1))
                                                                                                                                                                                                                                                                                                                                                                   End If
                                                                                                                                                                                                                                                                                                                                                               End If  

'SNIP: we'll just skip past the sea of End Ifs and hope to find shore  
End If  
   Try
       label1:
       label2:
       label3:
       label4:
       label5:
       label6:
       label7:
       label8:
       label9:  
'SNIP  
       label88:
         datetime1 = DateTime.Parse(dt)  
   Catch exception As Exception
       exception  
   End Try  
   Return datetime1  
End Function

Ask the Index

by in CodeSOD on

Different tasks call for different conventions. At least, that’s the excuse some people use for switching between 0-based and 1-based array indexes. That still doesn’t explain why Phillip’s co-worker did this.

var panelIndexes = {  
   "1" : 0,  
   "2": 1,  
   "3": 3,  
   "4": 4,  
   "5": 5,  
   "6": 6,  
   "7": 7,  
   "8": 8,  
   "10": 9,  
   "11" : 2,  
   "12" : 10 //Don't ask  
};
/* snip */  
var activePanel = panels[panelIndexes[i]];

The GNDN Protocol

by in CodeSOD on

The sort of software that’s used for research is the sort of software designed by engineers, not developers. With tight deadlines, corners get cut. This creates software that supports more shell injections, sql injections, and venous injections than useful functions. That’s bad, but all too common. This block that Koen found, on the other hand, is special.


            

Known Bad Code

by in CodeSOD on

Juho was skimming around code, searching for a bug, when saw the following PHP code. In my opinion, its purpose was probably as a workaround for a bug in MySQL 5.0. I mean, I hope it was.

At least the original author knew it was bad code.


Determined

by in CodeSOD on

There are a large number of programming problems that involve the use of matrices and linear algebra. And when you have a matrix, there may be times where you need to know its determinant. For calculating the determinant of a 2x2, or a 3x3 matrix, there’s a fairly straightforward formula. On the other hand, if you need a generic solution for a matrix of any size, you have to get a little more complicated.

One of Jarrod’s co-workers had their own “optimized” solution for this. It’s optimized in the sense that it doesn’t always return the right answer. This particular sample is for 3x3 matrices, but don’t worry: it’s been copy/pasted into all the spots that need to work with 4x4 matrices too.