CodeSOD: Capitalizing on Memories

This post was originally published on this site

The Daily WTF

Gavin inherited some “very old” C code, originally developed for old Windows systems. The code is loaded with a number of reinvented wheels.

For example, they needed to do a case insensitive string comparison. Now, instead of using stricmp, the case insensitive string comparison, they wrote this:

int uppercmp( char *sStr1, char *sStr2 ) { char *sUStr1; char *sUStr2; int iRet; if ( ( sStr1 == NULL ) || ( sStr2 == NULL ) ) return -1; __try { sUStr1 = strupr(strdup( sStr1 )); sUStr2 = strupr(strdup( sStr2 )); iRet = strcmp( sUStr1, sUStr2 ); return iRet; } __finally { FreeIfNotNULL(sUStr1); FreeIfNotNULL(sUStr2); } return }

This uses Microsoft’s structured exception handling extensions to C, hence the __try. SEH is its own weird beast, with huge amounts of caveats and gotchas and weirdness. It’s honestly more interesting than this function, which isn’t itself a WTF- it’s just bad. They duplicate

To read the full article click on the 'post' link at the top.