summaryrefslogtreecommitdiff
path: root/src/win32
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2011-11-30 11:27:15 -0800
committerRussell Belfer <arrbee@arrbee.com>2011-12-07 23:08:15 -0800
commit97769280ba9938ae27f6e06cbd0d5e8a768a86b9 (patch)
tree4fe43e99acb55f904f6b586bd7c5158610f9512f /src/win32
parenta22b14d32dd8d5f06f121aa154d45bac3b10a305 (diff)
downloadlibgit2-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/win32')
-rw-r--r--src/win32/posix_w32.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 6f722581e..e406a8f6c 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -304,32 +304,37 @@ int p_hide_directory__w32(const char *path)
char *p_realpath(const char *orig_path, char *buffer)
{
- int ret, alloc = 0;
+ int ret;
wchar_t* orig_path_w = gitwin_to_utf16(orig_path);
wchar_t* buffer_w = (wchar_t*)git__malloc(GIT_PATH_MAX * sizeof(wchar_t));
- if (buffer == NULL) {
- buffer = (char *)git__malloc(GIT_PATH_MAX);
- alloc = 1;
- }
-
ret = GetFullPathNameW(orig_path_w, GIT_PATH_MAX, buffer_w, NULL);
git__free(orig_path_w);
if (!ret || ret > GIT_PATH_MAX) {
- git__free(buffer_w);
- if (alloc) git__free(buffer);
-
- return NULL;
+ buffer = NULL;
+ goto done;
}
- if (!WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, buffer, GIT_PATH_MAX, NULL, NULL)) {
- git__free(buffer_w);
- if (alloc) git__free(buffer);
+ if (buffer == NULL) {
+ int buffer_sz = WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, NULL, 0, NULL, NULL);
+
+ if (!buffer_sz ||
+ !(buffer = (char *)git__malloc(buffer_sz)) ||
+ !WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, buffer, buffer_sz, NULL, NULL))
+ {
+ git__free(buffer);
+ buffer = NULL;
+ }
+ } else {
+ if (!WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, buffer, GIT_PATH_MAX, NULL, NULL))
+ buffer = NULL;
}
-
+
+done:
git__free(buffer_w);
- git_path_mkposix(buffer);
+ if (buffer)
+ git_path_mkposix(buffer);
return buffer;
}