Refactor Qt GUI function getDirFromFile to use QFileInfo to get absolute directory path. Also, added a input buffer size argument to prevent string copy buffer overflows. Fixes issue #598.

This commit is contained in:
harry 2023-01-07 00:43:35 -05:00
parent 77b894df0e
commit b3717c008b
15 changed files with 63 additions and 52 deletions

View File

@ -768,7 +768,7 @@ void GuiCheatsDialog_t::openCheatFile(void)
g_config->getOption("SDL.LastOpenFile", &last);
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));

View File

@ -505,7 +505,7 @@ void CodeDataLoggerDialog_t::loadCdlFile(void)
if (romFile)
{
getDirFromFile(romFile, dir);
getDirFromFile(romFile, dir, sizeof(dir));
dialog.setDirectory(tr(dir));
}

View File

@ -25,6 +25,7 @@
#include <QWindow>
#include <QScreen>
#include <QToolTip>
#include <QFileInfo>
#include <QApplication>
#if WIN32
@ -47,31 +48,28 @@
#include "Qt/ConsoleUtilities.h"
//---------------------------------------------------------------------------
int getDirFromFile( const char *path, char *dir )
int getDirFromFile( const char *path, char *dir, size_t bufSize )
{
int i, lastSlash = -1, lastPeriod = -1;
dir[0] = 0;
i=0;
while ( path[i] != 0 )
if (path[0] != 0)
{
if ( path[i] == '/' )
{
lastSlash = i;
}
else if ( path[i] == '.' )
{
lastPeriod = i;
}
dir[i] = path[i]; i++;
}
dir[i] = 0;
QFileInfo fi;
if ( lastPeriod >= 0 )
{
if ( lastPeriod > lastSlash )
fi.setFile( QString(path) );
if (fi.exists())
{
dir[lastSlash] = 0;
strncpy( dir, fi.canonicalPath().toStdString().c_str(), bufSize );
}
else
{
strncpy( dir, fi.absolutePath().toStdString().c_str(), bufSize );
}
dir[bufSize-1] = 0;
//printf("Dir: '%s'\n", dir);
}
return 0;
@ -231,7 +229,7 @@ int parseFilepath( const char *filepath, char *dir, char *base, char *suffix )
}
//---------------------------------------------------------------------------
// Returns the path of fceux.exe as a string.
int fceuExecutablePath( char *outputPath, int outputSize )
int fceuExecutablePath( char *outputPath, size_t outputSize )
{
if ( (outputPath == NULL) || (outputSize <= 0) )
{

View File

@ -9,7 +9,7 @@
#include <QHelpEvent>
#include <QCheckBox>
int getDirFromFile( const char *path, char *dir );
int getDirFromFile( const char *path, char *dir, size_t bufSize );
const char *getRomFile( void );
@ -17,7 +17,7 @@ int getFileBaseName( const char *filepath, char *base, char *suffix = NULL );
int parseFilepath( const char *filepath, char *dir, char *base, char *suffix = NULL );
int fceuExecutablePath( char *outputPath, int outputSize );
int fceuExecutablePath( char *outputPath, size_t outputSize );
int fceuLoadConfigColor( const char *confName, QColor *color );

View File

@ -2358,7 +2358,7 @@ void consoleWin_t::openROMFile(void)
g_config->getOption ("SDL.LastOpenFile", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -2451,7 +2451,7 @@ void consoleWin_t::loadNSF(void)
g_config->getOption ("SDL.LastOpenNSF", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -2535,7 +2535,7 @@ void consoleWin_t::loadStateFrom(void)
g_config->getOption ("SDL.LastLoadStateFrom", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -2626,7 +2626,7 @@ void consoleWin_t::saveStateAs(void)
last = std::string(base) + "/sav";
}
}
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -3395,7 +3395,7 @@ void consoleWin_t::loadGameGenieROM(void)
g_config->getOption ("SDL.LastOpenFile", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -3490,7 +3490,7 @@ void consoleWin_t::fdsLoadBiosFile(void)
g_config->getOption ("SDL.LastOpenFile", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );

View File

@ -419,7 +419,7 @@ void GuiConfDialog_t::openQss(void)
last.assign(iniPath);
}
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));
@ -523,7 +523,7 @@ void GuiConfDialog_t::openQPal(void)
last.assign(iniPath);
}
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));
@ -1228,7 +1228,7 @@ void GuiPaletteEditDialog_t::paletteSaveAs(void)
last.assign(iniPath);
}
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));

View File

@ -175,7 +175,7 @@ std::string consoleWin_t::findHelpFile(void)
if ( last.size() > 0 )
{
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
}

View File

@ -379,7 +379,7 @@ void LuaControlDialog_t::openLuaScriptFile(void)
#endif
}
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));

View File

@ -443,7 +443,7 @@ void MoviePlayDialog_t::doScan(void)
g_config->getOption("SDL.LastOpenMovie", &last);
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
scanDirectory(dir, md5);
}
@ -509,7 +509,7 @@ void MoviePlayDialog_t::openMovie(void)
g_config->getOption("SDL.LastOpenMovie", &last);
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));

View File

@ -222,7 +222,7 @@ void MovieRecordDialog_t::setLoadState(void)
g_config->getOption ("SDL.LastLoadStateFrom", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -330,7 +330,7 @@ void MovieRecordDialog_t::browseFiles(void)
g_config->getOption ("SDL.LastOpenMovie", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );

View File

@ -575,7 +575,7 @@ void PaletteConfDialog_t::openPaletteFile(void)
last.assign(iniPath);
}
getDirFromFile(last.c_str(), dir);
getDirFromFile(last.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));

View File

@ -401,7 +401,7 @@ void PaletteEditorDialog_t::openPaletteFileDialog(void)
{
last.assign( iniPath );
}
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );

View File

@ -578,14 +578,14 @@ void TraceLoggerDialog_t::openLogFile(void)
if (romFile != NULL)
{
char dir[1024];
getDirFromFile(romFile, dir);
getDirFromFile(romFile, dir, sizeof(dir));
dialog.setDirectory(tr(dir));
}
if ( logFilePath.size() != 0 )
{
char dir[1024];
getDirFromFile(logFilePath.c_str(), dir);
getDirFromFile(logFilePath.c_str(), dir, sizeof(dir));
dialog.setDirectory(tr(dir));
}

View File

@ -974,16 +974,29 @@ int fceuWrapperInit( int argc, char *argv[] )
if (romIndex >= 0)
{
// load the specified game
error = LoadGame(argv[romIndex]);
if (error != 1)
QFileInfo fi( argv[romIndex] );
// Resolve absolute path to file
if ( fi.exists() )
{
DriverKill();
SDL_Quit();
std::string fullpath = fi.canonicalFilePath().toStdString().c_str();
error = LoadGame( fullpath.c_str() );
if (error != 1)
{
DriverKill();
SDL_Quit();
return -1;
}
g_config->setOption("SDL.LastOpenFile", fullpath.c_str() );
g_config->save();
}
else
{
// File was not found
return -1;
}
g_config->setOption("SDL.LastOpenFile", argv[romIndex]);
g_config->save();
}
aviRecordInit();

View File

@ -795,7 +795,7 @@ bool iNesHeaderEditor_t::openFile(void)
g_config->getOption ("SDL.LastOpenFile", &last );
getDirFromFile( last.c_str(), dir );
getDirFromFile( last.c_str(), dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );
@ -848,7 +848,7 @@ void iNesHeaderEditor_t::saveFileAs(void)
dialog.setLabelText( QFileDialog::Accept, tr("Save") );
dialog.setDefaultSuffix( tr(".nes") );
getDirFromFile( LoadedRomFName, dir );
getDirFromFile( LoadedRomFName, dir, sizeof(dir) );
dialog.setDirectory( tr(dir) );