Skip to content

Commit

Permalink
Change where the config dir is created.
Browse files Browse the repository at this point in the history
On Windows, if there is a `vbam.ini` where the exe is, we should not
create the configuration directory `%LOCALAPPDATA%\visualboyadvance-m`.
  • Loading branch information
denisfa authored and rkitover committed May 31, 2019
1 parent 3490620 commit bf6f2d4
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/common/ConfigManager.cpp
Expand Up @@ -659,6 +659,12 @@ const char* FindConfigFile(const char *name)
return name;
}

struct stat s;
std::string homeDirTmp = get_xdg_user_config_home() + DOT_DIR;
homeDir = (char *)homeDirTmp.c_str();
if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode))
mkdir(homeDir, 0755);

if (homeDir) {
sprintf(path, "%s%c%s", homeDir, FILE_SEP, name);
if (FileExists(path))
Expand Down Expand Up @@ -730,12 +736,6 @@ const char* FindConfigFile(const char *name)

void LoadConfigFile()
{
struct stat s;
std::string homeDirTmp = get_xdg_user_config_home() + DOT_DIR;
homeDir = (char *)homeDirTmp.c_str();
if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode))
mkdir(homeDir, 0755);

if (preferences == NULL)
{
const char* configFile = FindConfigFile("vbam.ini");
Expand All @@ -745,12 +745,6 @@ void LoadConfigFile()

void SaveConfigFile()
{
struct stat s;
std::string homeDirTmp = get_xdg_user_config_home() + DOT_DIR;
homeDir = (char *)homeDirTmp.c_str();
if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode))
mkdir(homeDir, 0755);

const char* configFile = FindConfigFile("vbam.ini");

if (configFile != NULL)
Expand Down

2 comments on commit bf6f2d4

@FredericHamel
Copy link

@FredericHamel FredericHamel commented on bf6f2d4 Jun 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global variable homeDir will contain a pointer to unallocated memory. homeDirTmp.c_str() only return a pointer to the internal buffer of the string object. When the destructor of homeDirTmp is called at the end of its scope the internal buffer is freed. Because homeDir is pointing to the internal buffer of homeDirTmp, this means that homeDir is no longer valid. The char string chould be copied using strdup or equivalent.

@rkitover
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FredericHamel thank you for catching this, I'll add a strdup.

Please sign in to comment.