Ticket #65 (closed defect)

Opened 8 years ago

Last modified 8 years ago

[wx, Win2000, 2.2.1, VC 6] wxFindFileInPath

Reported by: mebarker Owned by: vadz
Priority: normal Milestone:
Component: wxMSW Version:
Keywords: Cc: mebarker, vadz
Blocked By: Patch: no
Blocking:

Description

I was playing with the internat sample today and ran across what I think is a bug in either wxFindFileInPath or wxStrtok. When I compile the sample in debug configuration (VC6 on Win2000), the sample fails to find any of the catalogs. In release configuration, it works fine. I traced this failure down to wxFindFileInPath searching for the catalogs. The wxFindFileInPath always returns false in debug mode. So I further traced it to the faliure of wxStrtok to work properly if the wxChar **save_ptr is valid and points to NULL when it is called first.

Here's some code cut from wxFindFileInPath :

// copy the path (strtok will modify it)
wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1];
wxStrcpy(szPath, pszPath);


wxString strFile;
wxChar *pc, *save_ptr;
for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr);

pc != NULL;
pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) )

{

// search for the file in this directory
strFile = pc;
if ( !wxEndsWithPathSeparator(pc) )

strFile += wxFILE_SEP_PATH;

strFile += pszFile;


if ( FileExists(strFile) ) {

*pStr = strFile;
break;

}

}


When in debug, the VC compiler is nice and initializes all your pointer to NULL for you if you don't. So in the debug case, save_ptr == NULL.
When in release, save_ptr != NULL.

Here's wxStrtok :

WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
{

if (!(save_ptr && *save_ptr)) {

return (wxChar *) NULL;

}


if (!psz) psz = *save_ptr;
psz += wxStrspn(psz, delim);
if (!*psz) {

*save_ptr = (wxChar *)NULL;
return (wxChar *)NULL;

}
wxChar *ret = psz;
psz = wxStrpbrk(psz, delim);
if (!psz) *save_ptr = (wxChar*)NULL;
else {

*psz = wxT('\0');
*save_ptr = psz + 1;

}
return ret;

}

As you can, see if save_ptr points to a NULL wxChar, then wxStrtok returns NULL right off the bat. Therefore, wxFindFileInPath fails in debug mode. How should this be fixed? Is this a problem with wxStrtok? In the case of wxFindFileInPath, you could just make sure save_ptr != NULL before you call wxStrtok the first time, but usualy I initialize pointer to NULL in my code. Any suggestions?

Change History

Changed 8 years ago by vadz

wxStrtok was fixed

Note: See TracTickets for help on using tickets.