diff options
author | Chris Robinson <[email protected]> | 2018-01-13 04:40:20 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-01-13 04:40:20 -0800 |
commit | c031b3cc6a8bb1525b59a850d3d8c6e4541256ce (patch) | |
tree | 59855fe52ead07739f0bddc67da0d2ff8439c268 /Alc | |
parent | bbc4ebecabc9417f92fbc9a185edf7360f1a8b6e (diff) |
Avoid fixed-PATH_MAX-size buffers
Windows still needs to use MAX_PATH in a couple places, but that macro's
guaranteed there.
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/alconfig.c | 88 | ||||
-rw-r--r-- | Alc/helpers.c | 27 |
2 files changed, 77 insertions, 38 deletions
diff --git a/Alc/alconfig.c b/Alc/alconfig.c index b1df7d14..5dbf59d2 100644 --- a/Alc/alconfig.c +++ b/Alc/alconfig.c @@ -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; + 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 + { + alstr_reset(&fname); + fname = confpaths; + AL_STRING_INIT(confpaths); + } - if(next[0] != '/') - WARN("Ignoring XDG config dir: %s\n", next); + if(alstr_empty(fname) || VECTOR_FRONT(fname) != '/') + WARN("Ignoring XDG config dir: %s\n", alstr_get_cstr(fname)); else { - size_t len = strlen(next); - strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len); - buffer[sizeof(buffer)-1] = 0; + if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf"); + else alstr_append_cstr(&fname, "alsoft.conf"); - TRACE("Loading config %s...\n", next); - f = al_fopen(next, "r"); + 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 diff --git a/Alc/helpers.c b/Alc/helpers.c index 790484e9..96f9b681 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -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') |