diff options
| author | Russell Belfer <arrbee@arrbee.com> | 2011-11-30 11:27:15 -0800 |
|---|---|---|
| committer | Russell Belfer <arrbee@arrbee.com> | 2011-12-07 23:08:15 -0800 |
| commit | 97769280ba9938ae27f6e06cbd0d5e8a768a86b9 (patch) | |
| tree | 4fe43e99acb55f904f6b586bd7c5158610f9512f /src/config.c | |
| parent | a22b14d32dd8d5f06f121aa154d45bac3b10a305 (diff) | |
| download | libgit2-97769280ba9938ae27f6e06cbd0d5e8a768a86b9.tar.gz | |
Use git_buf for path storage instead of stack-based buffers
This converts virtually all of the places that allocate GIT_PATH_MAX
buffers on the stack for manipulating paths to use git_buf objects
instead. The patch is pretty careful not to touch the public API
for libgit2, so there are a few places that still use GIT_PATH_MAX.
This extends and changes some details of the git_buf implementation
to add a couple of extra functions and to make error handling easier.
This includes serious alterations to all the path.c functions, and
several of the fileops.c ones, too. Also, there are a number of new
functions that parallel existing ones except that use a git_buf
instead of a stack-based buffer (such as git_config_find_global_r
that exists alongsize git_config_find_global).
This also modifies the win32 version of p_realpath to allocate whatever
buffer size is needed to accommodate the realpath instead of hardcoding
a GIT_PATH_MAX limit, but that change needs to be tested still.
Diffstat (limited to 'src/config.c')
| -rw-r--r-- | src/config.c | 64 |
1 files changed, 48 insertions, 16 deletions
diff --git a/src/config.c b/src/config.c index a8e15405b..2e341d256 100644 --- a/src/config.c +++ b/src/config.c @@ -330,9 +330,25 @@ int git_config_get_string(git_config *cfg, const char *name, const char **out) int git_config_find_global(char *global_config_path) { - const char *home; + git_buf path = GIT_BUF_INIT; + int error = git_config_find_global_r(&path); + + if (error == GIT_SUCCESS) { + if (path.size > GIT_PATH_MAX) + error = git__throw(GIT_ESHORTBUFFER, "Path is too long"); + else + git_buf_copy_cstr(global_config_path, GIT_PATH_MAX, &path); + } + + git_buf_free(&path); - home = getenv("HOME"); + return error; +} + +int git_config_find_global_r(git_buf *path) +{ + int error; + const char *home = getenv("HOME"); #ifdef GIT_WIN32 if (home == NULL) @@ -342,10 +358,13 @@ int git_config_find_global(char *global_config_path) if (home == NULL) return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot locate the user's home directory"); - git_path_join(global_config_path, home, GIT_CONFIG_FILENAME); + if ((error = git_buf_joinpath(path, home, GIT_CONFIG_FILENAME)) < GIT_SUCCESS) + return error; - if (git_futils_exists(global_config_path) < GIT_SUCCESS) + if (git_futils_exists(path->ptr) < GIT_SUCCESS) { + git_buf_clear(path); return git__throw(GIT_EOSERR, "Failed to open global config file. The file does not exist"); + } return GIT_SUCCESS; } @@ -353,7 +372,7 @@ int git_config_find_global(char *global_config_path) #if GIT_WIN32 -static int win32_find_system(char *system_config_path) +static int win32_find_system(git_buf *system_config_path) { const wchar_t *query = L"%PROGRAMFILES%\\Git\\etc\\gitconfig"; wchar_t *apphome_utf16; @@ -378,25 +397,21 @@ static int win32_find_system(char *system_config_path) apphome_utf8 = gitwin_from_utf16(apphome_utf16); git__free(apphome_utf16); - if (strlen(apphome_utf8) >= GIT_PATH_MAX) { - git__free(apphome_utf8); - return git__throw(GIT_ESHORTBUFFER, "Path is too long"); - } + git_buf_attach(system_config_path, apphome_utf8, 0); - strcpy(system_config_path, apphome_utf8); - git__free(apphome_utf8); return GIT_SUCCESS; } #endif -int git_config_find_system(char *system_config_path) +int git_config_find_system_r(git_buf *system_config_path) { - const char *etc = "/etc/gitconfig"; + if (git_buf_sets(system_config_path, "/etc/gitconfig") < GIT_SUCCESS) + return git_buf_lasterror(system_config_path); - if (git_futils_exists(etc) == GIT_SUCCESS) { - memcpy(system_config_path, etc, strlen(etc) + 1); + if (git_futils_exists(system_config_path->ptr) == GIT_SUCCESS) return GIT_SUCCESS; - } + + git_buf_clear(system_config_path); #if GIT_WIN32 return win32_find_system(system_config_path); @@ -405,6 +420,23 @@ int git_config_find_system(char *system_config_path) #endif } +int git_config_find_system(char *system_config_path) +{ + git_buf path = GIT_BUF_INIT; + int error = git_config_find_system_r(&path); + + if (error == GIT_SUCCESS) { + if (path.size > GIT_PATH_MAX) + error = git__throw(GIT_ESHORTBUFFER, "Path is too long"); + else + git_buf_copy_cstr(system_config_path, GIT_PATH_MAX, &path); + } + + git_buf_free(&path); + + return error; +} + int git_config_open_global(git_config **out) { int error; |
