What’s wrong with this code?
I ran into a subtle an interesting error last week. See if you can spot the error below — and submit your guesses!
I ripped this out of a complex system, and had to add in quite a bit of scaffolding to put the relevant code snippet into some context. The error is in the while loop — not the choice of string functions, use of naked pointers, return types, or variable names.
class CrtWrappers
{
public:
static int strcmp(const char *str1, const char *str2, const char *strMessage = "")
{
if( !str1 || !str2 )
{
throw std::exception(strMessage);
}
return ::strcmp(str1, str2);
}
};
struct thing
{
char *m_cszJobName;
};
static std::set<thing%gt; collection; // assume this is populated somewhere
const char * FindThisThing(const char *cpszJobName)
{
const char * pszReturn=0;
std::set<thing>::const_iterator citer = collection.begin();
std::set<thing>::const_iterator citerEnd = collection.end();
while(citer != citerEnd)
{
if(0 == CrtWrappers::strcmp(cpszJobName,(*citer).m_cszJobName), L"cpszJobName")
{
pszReturn = (*citer).m_cszJobName;
}
++citer;
}
return pszReturn;
}
|