CodeSOD: Concrapenate Strings

This post was originally published on this site

The Daily WTF

As oft discussed, null-terminated C-style strings are an endless source of problems. But there’s no problem so bad that it can’t be made worse by a sufficiently motivated developer.

Today’s rather old code comes from Mike, who inherited an old, MFC application. This code is responsible for opening a file dialog, and the key goal of the code is to configure the file filter in that dialog. In MFC, this is done by passing a delimited string containing a caption and a glob for filtering. E.g., “Text Files (.txt) | *.txt” would open a dialog for finding text files.

char szFileName[MAX_FILE_NAME]; char szDlgTitle[] = “Save File As”; char szDefExt[] = “log”; char* szPos; WORD nFileOffset; WORD nFileExtension; char szFileFilter[MAX_FILE_NAME]; char szBuffer[128]; std::ifstream inFile; memset(szFileFilter, NULL, MAX_FILTER_SIZE); memset(szFileName, NULL, MAX_FILE_NAME); strcpy_s(szFileFilter, sizeof(szFileFilter), “*.log – Debug Log File|”); szPos = szFileFilter + strlen(“*.log – Debug Log File|”) + 1; strcpy_s(szPos, sizeof( szPos),

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