Avoid fixed-PATH_MAX-size buffers

Windows still needs to use MAX_PATH in a couple places, but that macro's
guaranteed there.
This commit is contained in:
Chris Robinson
2018-01-13 04:40:20 -08:00
parent bbc4ebecab
commit c031b3cc6a
3 changed files with 78 additions and 47 deletions
+56 -34
View File
@@ -367,7 +367,7 @@ static void LoadConfigFromFile(FILE *f)
void ReadALConfig(void)
{
al_string ppath = AL_STRING_INIT_STATIC();
WCHAR buffer[PATH_MAX];
WCHAR buffer[MAX_PATH];
const WCHAR *str;
FILE *f;
@@ -420,8 +420,8 @@ void ReadALConfig(void)
#else
void ReadALConfig(void)
{
al_string ppath = AL_STRING_INIT_STATIC();
char buffer[PATH_MAX];
al_string confpaths = AL_STRING_INIT_STATIC();
al_string fname = AL_STRING_INIT_STATIC();
const char *str;
FILE *f;
@@ -437,45 +437,55 @@ void ReadALConfig(void)
if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
str = "/etc/xdg";
strncpy(buffer, str, sizeof(buffer)-1);
buffer[sizeof(buffer)-1] = 0;
alstr_copy_cstr(&confpaths, str);
/* Go through the list in reverse, since "the order of base directories
* denotes their importance; the first directory listed is the most
* important". Ergo, we need to load the settings from the later dirs
* first so that the settings in the earlier dirs override them.
*/
while(1)
while(!alstr_empty(confpaths))
{
char *next = strrchr(buffer, ':');
if(next) *(next++) = 0;
else next = buffer;
if(next[0] != '/')
WARN("Ignoring XDG config dir: %s\n", next);
char *next = strrchr(alstr_get_cstr(confpaths), ':');
if(next)
{
size_t len = next - alstr_get_cstr(confpaths);
alstr_copy_cstr(&fname, next+1);
VECTOR_RESIZE(confpaths, len, len+1);
VECTOR_ELEM(confpaths, len) = 0;
}
else
{
size_t len = strlen(next);
strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
buffer[sizeof(buffer)-1] = 0;
alstr_reset(&fname);
fname = confpaths;
AL_STRING_INIT(confpaths);
}
TRACE("Loading config %s...\n", next);
f = al_fopen(next, "r");
if(alstr_empty(fname) || VECTOR_FRONT(fname) != '/')
WARN("Ignoring XDG config dir: %s\n", alstr_get_cstr(fname));
else
{
if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf");
else alstr_append_cstr(&fname, "alsoft.conf");
TRACE("Loading config %s...\n", alstr_get_cstr(fname));
f = al_fopen(alstr_get_cstr(fname), "r");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
}
if(next == buffer)
break;
alstr_clear(&fname);
}
if((str=getenv("HOME")) != NULL && *str)
{
snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
alstr_copy_cstr(&fname, str);
if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/.alsoftrc");
else alstr_append_cstr(&fname, ".alsoftrc");
TRACE("Loading config %s...\n", buffer);
f = al_fopen(buffer, "r");
TRACE("Loading config %s...\n", alstr_get_cstr(fname));
f = al_fopen(alstr_get_cstr(fname), "r");
if(f)
{
LoadConfigFromFile(f);
@@ -484,17 +494,25 @@ void ReadALConfig(void)
}
if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
{
alstr_copy_cstr(&fname, str);
if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf");
else alstr_append_cstr(&fname, "alsoft.conf");
}
else
{
buffer[0] = 0;
alstr_clear(&fname);
if((str=getenv("HOME")) != NULL && str[0] != 0)
snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
{
alstr_copy_cstr(&fname, str);
if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/.config/alsoft.conf");
else alstr_append_cstr(&fname, ".config/alsoft.conf");
}
}
if(buffer[0] != 0)
if(!alstr_empty(fname))
{
TRACE("Loading config %s...\n", buffer);
f = al_fopen(buffer, "r");
TRACE("Loading config %s...\n", alstr_get_cstr(fname));
f = al_fopen(alstr_get_cstr(fname), "r");
if(f)
{
LoadConfigFromFile(f);
@@ -502,12 +520,15 @@ void ReadALConfig(void)
}
}
GetProcBinary(&ppath, NULL);
if(!alstr_empty(ppath))
alstr_clear(&fname);
GetProcBinary(&fname, NULL);
if(!alstr_empty(fname))
{
alstr_append_cstr(&ppath, "/alsoft.conf");
TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
f = al_fopen(alstr_get_cstr(ppath), "r");
if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf");
else alstr_append_cstr(&fname, "alsoft.conf");
TRACE("Loading config %s...\n", alstr_get_cstr(fname));
f = al_fopen(alstr_get_cstr(fname), "r");
if(f)
{
LoadConfigFromFile(f);
@@ -526,7 +547,8 @@ void ReadALConfig(void)
}
}
alstr_reset(&ppath);
alstr_reset(&fname);
alstr_reset(&confpaths);
}
#endif
+22 -5
View File
@@ -642,7 +642,7 @@ vector_al_string SearchDataFiles(const char *ext, const char *subdir)
/* Search the local and global data dirs. */
for(i = 0;i < COUNTOF(ids);i++)
{
WCHAR buffer[PATH_MAX];
WCHAR buffer[MAX_PATH];
if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
{
alstr_copy_wcstr(&path, buffer);
@@ -907,15 +907,32 @@ vector_al_string SearchDataFiles(const char *ext, const char *subdir)
{
al_string path = AL_STRING_INIT_STATIC();
const char *str, *next;
char cwdbuf[PATH_MAX];
/* Search the app-local directory. */
if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
DirectorySearch(str, ext, &results);
else if(getcwd(cwdbuf, sizeof(cwdbuf)))
DirectorySearch(cwdbuf, ext, &results);
else
DirectorySearch(".", ext, &results);
{
size_t cwdlen = 256;
char *cwdbuf = malloc(cwdlen);
while(!getcwd(cwdbuf, cwdlen))
{
free(cwdbuf);
cwdbuf = NULL;
if(errno != ERANGE)
break;
cwdlen <<= 1;
cwdbuf = malloc(cwdlen);
}
if(!cwdbuf)
DirectorySearch(".", ext, &results);
else
{
DirectorySearch(cwdbuf, ext, &results);
free(cwdbuf);
cwdbuf = NULL;
}
}
// Search local data dir
if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
-8
View File
@@ -236,14 +236,6 @@ typedef ALuint64SOFT ALuint64;
#define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
#endif
#ifndef PATH_MAX
#ifdef MAX_PATH
#define PATH_MAX MAX_PATH
#else
#define PATH_MAX 4096
#endif
#endif
static const union {
ALuint u;