parsing fix, string safety

This commit is contained in:
dborth 2011-03-20 17:19:09 +00:00
parent 6ff121a1fd
commit 22d1cfea45
6 changed files with 33 additions and 34 deletions

View File

@ -290,7 +290,7 @@ bool MakeFilePath(char filepath[], int type, char * filename, int filenum)
sprintf (temppath, "%s%s/%s", pathPrefix[GCSettings.SaveMethod], folder, file); sprintf (temppath, "%s%s/%s", pathPrefix[GCSettings.SaveMethod], folder, file);
} }
CleanupPath(temppath); // cleanup path CleanupPath(temppath); // cleanup path
strncpy(filepath, temppath, MAXPATHLEN); snprintf(filepath, MAXPATHLEN, "%s", temppath);
return true; return true;
} }
@ -348,7 +348,7 @@ void StripExt(char* returnstring, char * inputstring)
{ {
char* loc_dot; char* loc_dot;
strncpy (returnstring, inputstring, MAXJOLIET); snprintf(returnstring, MAXJOLIET, "%s", inputstring);
if(inputstring == NULL || strlen(inputstring) < 4) if(inputstring == NULL || strlen(inputstring) < 4)
return; return;
@ -516,8 +516,8 @@ int BrowserChangeFolder()
if(!UpdateDirName()) if(!UpdateDirName())
return -1; return -1;
CleanupPath(browser.dir);
HaltParseThread(); // halt parsing HaltParseThread(); // halt parsing
CleanupPath(browser.dir);
ResetBrowser(); // reset browser ResetBrowser(); // reset browser
if(browser.dir[0] != 0) if(browser.dir[0] != 0)

View File

@ -23,7 +23,7 @@
typedef struct typedef struct
{ {
char dir[MAXPATHLEN]; // directory path of browserList char dir[MAXPATHLEN + 1]; // directory path of browserList
int numEntries; // # of entries in browserList int numEntries; // # of entries in browserList
int selIndex; // currently selected index of browserList int selIndex; // currently selected index of browserList
int pageIndex; // starting index of browserList page display int pageIndex; // starting index of browserList page display

View File

@ -453,8 +453,8 @@ void CreateAppPath(char * origpath)
path[2] = 'd'; path[2] = 'd';
} }
if(ChangeInterface(&path[pos], SILENT)) if(ChangeInterface(&path[pos], SILENT))
strncpy(appPath, &path[pos], MAXPATHLEN); snprintf(appPath, MAXPATHLEN-1, "%s", &path[pos]);
appPath[MAXPATHLEN-1] = 0;
free(path); free(path);
} }
@ -481,12 +481,12 @@ bool ParseDirEntries()
char *ext; char *ext;
char path[MAXPATHLEN+1]; char path[MAXPATHLEN+1];
struct dirent *entry; struct dirent *entry = NULL;
struct stat filestat; struct stat filestat;
int i = 0; int i = 0;
while(i < 20) while(i < 20 && !parseHalt)
{ {
entry = readdir(dir); entry = readdir(dir);
@ -530,7 +530,7 @@ bool ParseDirEntries()
break; break;
} }
strncpy(browserList[browser.numEntries+i].filename, entry->d_name, MAXJOLIET); snprintf(browserList[browser.numEntries+i].filename, MAXJOLIET, "%s", entry->d_name);
browserList[browser.numEntries+i].length = filestat.st_size; browserList[browser.numEntries+i].length = filestat.st_size;
browserList[browser.numEntries+i].mtime = filestat.st_mtime; browserList[browser.numEntries+i].mtime = filestat.st_mtime;
browserList[browser.numEntries+i].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir browserList[browser.numEntries+i].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir
@ -540,7 +540,7 @@ bool ParseDirEntries()
if(strcmp(entry->d_name, "..") == 0) if(strcmp(entry->d_name, "..") == 0)
sprintf(browserList[browser.numEntries+i].displayname, "Up One Level"); sprintf(browserList[browser.numEntries+i].displayname, "Up One Level");
else else
strncpy(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename, MAXJOLIET); snprintf(browserList[browser.numEntries+i].displayname, MAXJOLIET, "%s", browserList[browser.numEntries+i].filename);
browserList[browser.numEntries+i].icon = ICON_FOLDER; browserList[browser.numEntries+i].icon = ICON_FOLDER;
} }
else else
@ -550,11 +550,14 @@ bool ParseDirEntries()
i++; i++;
} }
// Sort the file list if(!parseHalt)
if(i >= 0) {
qsort(browserList, browser.numEntries+i, sizeof(BROWSERENTRY), FileSortCallback); // Sort the file list
if(i >= 0)
qsort(browserList, browser.numEntries+i, sizeof(BROWSERENTRY), FileSortCallback);
browser.numEntries += i; browser.numEntries += i;
}
if(entry == NULL || parseHalt) if(entry == NULL || parseHalt)
{ {
@ -562,7 +565,7 @@ bool ParseDirEntries()
dir = NULL; dir = NULL;
// try to find and select the last loaded file // try to find and select the last loaded file
if(selectLoadedFile == 1 && parseHalt == 0 && loadedFile[0] != 0 && browser.dir[0] != 0) if(selectLoadedFile == 1 && !parseHalt && loadedFile[0] != 0 && browser.dir[0] != 0)
{ {
int indexFound = -1; int indexFound = -1;
@ -594,7 +597,6 @@ bool ParseDirEntries()
} }
selectLoadedFile = 2; // selecting done selectLoadedFile = 2; // selecting done
} }
return false; // no more entries return false; // no more entries
} }
return true; // more entries return true; // more entries

View File

@ -455,7 +455,7 @@ int SzParse(char * filepath)
} }
// parse information about this file to the file list structure // parse information about this file to the file list structure
strncpy(browserList[SzJ].filename, SzF->Name, MAXJOLIET); snprintf(browserList[SzJ].filename, MAXJOLIET, "%s", SzF->Name);
StripExt(browserList[SzJ].displayname, browserList[SzJ].filename); StripExt(browserList[SzJ].displayname, browserList[SzJ].filename);
browserList[SzJ].length = SzF->Size; // filesize browserList[SzJ].length = SzF->Size; // filesize
browserList[SzJ].isdir = 0; // only files will be displayed (-> no flags) browserList[SzJ].isdir = 0; // only files will be displayed (-> no flags)

View File

@ -22,8 +22,7 @@ static char * GetDisplayText(char * t)
if(len < MAX_KEYBOARD_DISPLAY) if(len < MAX_KEYBOARD_DISPLAY)
return t; return t;
strncpy(tmptxt, &t[len-MAX_KEYBOARD_DISPLAY], MAX_KEYBOARD_DISPLAY); snprintf(tmptxt, MAX_KEYBOARD_DISPLAY, "%s", &t[len-MAX_KEYBOARD_DISPLAY]);
tmptxt[MAX_KEYBOARD_DISPLAY-1] = 0;
return &tmptxt[0]; return &tmptxt[0];
} }
@ -41,8 +40,7 @@ GuiKeyboard::GuiKeyboard(char * t, u32 max)
focus = 0; // allow focus focus = 0; // allow focus
alignmentHor = ALIGN_CENTRE; alignmentHor = ALIGN_CENTRE;
alignmentVert = ALIGN_MIDDLE; alignmentVert = ALIGN_MIDDLE;
strncpy(kbtextstr, t, max); snprintf(kbtextstr, 255, "%s", t);
kbtextstr[max] = 0;
kbtextmaxlen = max; kbtextmaxlen = max;
Key thekeys[4][11] = { Key thekeys[4][11] = {

View File

@ -67,8 +67,8 @@ static lwp_t updatethread = LWP_THREAD_NULL;
static bool guiHalt = true; static bool guiHalt = true;
static int showProgress = 0; static int showProgress = 0;
static char progressTitle[100]; static char progressTitle[101];
static char progressMsg[200]; static char progressMsg[201];
static int progressDone = 0; static int progressDone = 0;
static int progressTotal = 0; static int progressTotal = 0;
@ -499,7 +499,7 @@ ShowProgress (const char *msg, int done, int total)
if(showProgress != 1) if(showProgress != 1)
CancelAction(); // wait for previous progress window to finish CancelAction(); // wait for previous progress window to finish
strncpy(progressMsg, msg, 200); snprintf(progressMsg, 200, "%s", msg);
sprintf(progressTitle, "Please Wait"); sprintf(progressTitle, "Please Wait");
showProgress = 1; showProgress = 1;
progressTotal = total; progressTotal = total;
@ -522,7 +522,7 @@ ShowAction (const char *msg)
if(showProgress != 0) if(showProgress != 0)
CancelAction(); // wait for previous progress window to finish CancelAction(); // wait for previous progress window to finish
strncpy(progressMsg, msg, 200); snprintf(progressMsg, 200, "%s", msg);
sprintf(progressTitle, "Please Wait"); sprintf(progressTitle, "Please Wait");
showProgress = 2; showProgress = 2;
progressDone = 0; progressDone = 0;
@ -1496,7 +1496,7 @@ static int MenuGameSaves(int action)
char filepath[1024]; char filepath[1024];
char scrfile[1024]; char scrfile[1024];
char tmp[MAXJOLIET]; char tmp[MAXJOLIET+1];
int method = GCSettings.SaveMethod; int method = GCSettings.SaveMethod;
@ -1588,7 +1588,7 @@ static int MenuGameSaves(int action)
else else
continue; continue;
strncpy(tmp, browserList[i].filename, MAXJOLIET); strcpy(tmp, browserList[i].filename);
tmp[len2-4] = 0; tmp[len2-4] = 0;
n = FindGameSaveNum(tmp, method); n = FindGameSaveNum(tmp, method);
@ -1596,7 +1596,7 @@ static int MenuGameSaves(int action)
{ {
saves.type[j] = type; saves.type[j] = type;
saves.files[saves.type[j]][n] = 1; saves.files[saves.type[j]][n] = 1;
strncpy(saves.filename[j], browserList[i].filename, MAXJOLIET); strcpy(saves.filename[j], browserList[i].filename);
if(saves.type[j] == FILE_SNAPSHOT) if(saves.type[j] == FILE_SNAPSHOT)
{ {
@ -3486,11 +3486,10 @@ static int MenuSettingsNetwork()
if(ret >= 0 || firstRun) if(ret >= 0 || firstRun)
{ {
firstRun = false; firstRun = false;
strncpy (options.value[0], GCSettings.smbip, 25); snprintf (options.value[0], 25, "%s", GCSettings.smbip);
options.value[0][25] = 0; snprintf (options.value[1], 19, "%s", GCSettings.smbshare);
strncpy (options.value[1], GCSettings.smbshare, 19); snprintf (options.value[2], 19, "%s", GCSettings.smbuser);
strncpy (options.value[2], GCSettings.smbuser, 19); snprintf (options.value[3], 19, "%s", GCSettings.smbpwd);
strncpy (options.value[3], GCSettings.smbpwd, 19);
optionBrowser.TriggerUpdate(); optionBrowser.TriggerUpdate();
} }