Commit aaefbdeea205fbea2005113a9743c81721a42b36

Ben Straub 2013-08-08T08:48:57

Discriminate path-specific and general UTF-X conversions

diff --git a/src/fileops.c b/src/fileops.c
index 25323d0..d62158d 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -60,7 +60,7 @@ int git_futils_creat_locked(const char *path, const mode_t mode)
 #ifdef GIT_WIN32
 	git_win32_path_utf16 buf;
 
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC |
 		O_EXCL | O_BINARY | O_CLOEXEC, mode);
 #else
diff --git a/src/path.c b/src/path.c
index 72831c7..d0cecc0 100644
--- a/src/path.c
+++ b/src/path.c
@@ -493,7 +493,7 @@ bool git_path_is_empty_dir(const char *path)
 	if (!git_path_isdir(path)) return false;
 
 	git_buf_printf(&pathbuf, "%s\\*", path);
-	git__utf8_to_16(wbuf, git_buf_cstr(&pathbuf));
+	git__win32_path_utf8_to_16(wbuf, git_buf_cstr(&pathbuf));
 
 	hFind = FindFirstFileW(wbuf, &ffd);
 	if (INVALID_HANDLE_VALUE == hFind) {
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 359ab5f..5c55c10 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -264,7 +264,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
 		if (git_buf_printf(&buf, "Content-Type: application/x-git-%s-request", s->service) < 0)
 			goto on_error;
 
-		git__utf8_to_16(ct, git_buf_cstr(&buf));
+		git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf));
 
 		if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
 			giterr_set(GITERR_OS, "Failed to add a header to the request");
@@ -593,7 +593,7 @@ replay:
 		else
 			snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
 
-		git__utf8_to_16(expected_content_type, expected_content_type_8);
+		git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8);
 		content_type_length = sizeof(content_type);
 
 		if (!WinHttpQueryHeaders(s->request,
@@ -920,7 +920,7 @@ static int winhttp_connect(
 		return -1;
 
 	/* Prepare host */
-	git__utf8_to_16(host, t->host);
+	git__win32_path_utf8_to_16(host, t->host);
 
 	/* Establish session */
 	t->session = WinHttpOpen(
diff --git a/src/win32/dir.c b/src/win32/dir.c
index b1cb47c..85f793d 100644
--- a/src/win32/dir.c
+++ b/src/win32/dir.c
@@ -40,7 +40,7 @@ git__DIR *git__opendir(const char *dir)
 	if (!new->dir)
 		goto fail;
 
-	git__utf8_to_16(filter_w, filter);
+	git__win32_path_utf8_to_16(filter_w, filter);
 	new->h = FindFirstFileW(filter_w, &new->f);
 
 	if (new->h == INVALID_HANDLE_VALUE) {
@@ -80,7 +80,7 @@ int git__readdir_ext(
 	if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
 		return -1;
 
-	git__utf16_to_8(entry->d_name, d->f.cFileName);
+	git__win32_path_utf16_to_8(entry->d_name, d->f.cFileName);
 	entry->d_ino = 0;
 
 	*result = entry;
@@ -116,7 +116,7 @@ void git__rewinddir(git__DIR *d)
 	if (!init_filter(filter, sizeof(filter), d->dir))
 		return;
 
-	git__utf8_to_16(filter_w, filter);
+	git__win32_path_utf8_to_16(filter_w, filter);
 	d->h = FindFirstFileW(filter_w, &d->f);
 
 	if (d->h == INVALID_HANDLE_VALUE)
diff --git a/src/win32/findfile.c b/src/win32/findfile.c
index 2481735..64b9bb5 100644
--- a/src/win32/findfile.c
+++ b/src/win32/findfile.c
@@ -27,7 +27,7 @@ static int win32_path_utf16_to_8(git_buf *path_utf8, const wchar_t *path_utf16)
 {
 	char temp_utf8[GIT_PATH_MAX];
 
-	git__utf16_to_8(temp_utf8, path_utf16);
+	git__utf16_to_8(temp_utf8, GIT_PATH_MAX, path_utf16);
 	git_path_mkposix(temp_utf8);
 
 	return git_buf_sets(path_utf8, temp_utf8);
@@ -53,7 +53,7 @@ int git_win32__find_file(
 	if (*filename == '/' || *filename == '\\')
 		filename++;
 
-	git__utf8_to_16(file_utf16 + root->len - 1, filename);
+	git__utf8_to_16(file_utf16 + root->len - 1, alloc_len - root->len, filename);
 
 	/* check access */
 	if (_waccess(file_utf16, F_OK) < 0) {
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 47f6dde..7803c9c 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -22,7 +22,7 @@ GIT_INLINE(int) p_mkdir(const char *path, mode_t mode)
 {
 	git_win32_path_utf16 buf;
 	GIT_UNUSED(mode);
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	return _wmkdir(buf);
 }
 
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index a7df424..d9a68f2 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -17,7 +17,7 @@
 int p_unlink(const char *path)
 {
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	_wchmod(buf, 0666);
 	return _wunlink(buf);
 }
@@ -63,7 +63,7 @@ static int do_lstat(
 	wchar_t lastch;
 	int flen;
 
-	flen = git__utf8_to_16(fbuf, file_name);
+	flen = git__win32_path_utf8_to_16(fbuf, file_name);
 
 	/* truncate trailing slashes */
 	for (; flen > 0; --flen) {
@@ -189,7 +189,7 @@ int p_readlink(const char *link, char *target, size_t target_len)
 		}
 	}
 
-	git__utf8_to_16(link_w, link);
+	git__win32_path_utf8_to_16(link_w, link);
 
 	hFile = CreateFileW(link_w,			// file to open
 			GENERIC_READ,			// open for reading
@@ -258,7 +258,7 @@ int p_open(const char *path, int flags, ...)
 	git_win32_path_utf16 buf;
 	mode_t mode = 0;
 
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 
 	if (flags & O_CREAT) {
 		va_list arg_list;
@@ -274,7 +274,7 @@ int p_open(const char *path, int flags, ...)
 int p_creat(const char *path, mode_t mode)
 {
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	return _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode);
 }
 
@@ -317,14 +317,14 @@ int p_stat(const char* path, struct stat* buf)
 int p_chdir(const char* path)
 {
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	return _wchdir(buf);
 }
 
 int p_chmod(const char* path, mode_t mode)
 {
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	return _wchmod(buf, mode);
 }
 
@@ -332,7 +332,7 @@ int p_rmdir(const char* path)
 {
 	int error;
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 
 	error = _wrmdir(buf);
 
@@ -349,7 +349,7 @@ int p_rmdir(const char* path)
 int p_hide_directory__w32(const char *path)
 {
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	return (SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0) ? 0 : -1;
 }
 
@@ -359,7 +359,7 @@ char *p_realpath(const char *orig_path, char *buffer)
 	git_win32_path_utf16 orig_path_w;
 	git_win32_path_utf16 buffer_w;
 
-	git__utf8_to_16(orig_path_w, orig_path);
+	git__win32_path_utf8_to_16(orig_path_w, orig_path);
 
 	/* Implicitly use GetCurrentDirectory which can be a threading issue */
 	ret = GetFullPathNameW(orig_path_w, GIT_WIN_PATH_UTF16, buffer_w, NULL);
@@ -450,7 +450,7 @@ int p_setenv(const char* name, const char* value, int overwrite)
 int p_access(const char* path, mode_t mode)
 {
 	git_win32_path_utf16 buf;
-	git__utf8_to_16(buf, path);
+	git__win32_path_utf8_to_16(buf, path);
 	return _waccess(buf, mode);
 }
 
@@ -459,8 +459,8 @@ int p_rename(const char *from, const char *to)
 	git_win32_path_utf16 wfrom;
 	git_win32_path_utf16 wto;
 
-	git__utf8_to_16(wfrom, from);
-	git__utf8_to_16(wto, to);
+	git__win32_path_utf8_to_16(wfrom, from);
+	git__win32_path_utf8_to_16(wto, to);
 	return MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) ? 0 : -1;
 }
 
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index bb63b0f..623b051 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -70,12 +70,12 @@ void git__utf8_to_16(wchar_t *dest, size_t length, const char *src)
 }
 #endif
 
-int git__utf8_to_16(git_win32_path_utf16 dest, const git_win32_path_utf8 src)
+int git__utf8_to_16(wchar_t * dest, size_t dest_size, const char *src)
 {
-	return MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, GIT_WIN_PATH_UTF16);
+	return MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, dest_size);
 }
 
-int git__utf16_to_8(git_win32_path_utf8 dest, const git_win32_path_utf16 src)
+int git__utf16_to_8(char *dest, size_t dest_size, const wchar_t *src)
 {
-	return WideCharToMultiByte(CP_UTF8, 0, src, -1, dest, GIT_WIN_PATH_UTF8, NULL, NULL);
+	return WideCharToMultiByte(CP_UTF8, 0, src, -1, dest, dest_size, NULL, NULL);
 }
diff --git a/src/win32/utf-conv.h b/src/win32/utf-conv.h
index 4e60229..7290f1d 100644
--- a/src/win32/utf-conv.h
+++ b/src/win32/utf-conv.h
@@ -16,8 +16,19 @@
 typedef wchar_t git_win32_path_utf16[GIT_WIN_PATH_UTF16];
 typedef char git_win32_path_utf8[GIT_WIN_PATH_UTF8];
 
-int git__utf8_to_16(git_win32_path_utf16 dest, const git_win32_path_utf8 src);
-int git__utf16_to_8(git_win32_path_utf8 dest, const git_win32_path_utf16 src);
+// dest_size is the size of dest in wchar_t's
+int git__utf8_to_16(wchar_t * dest, size_t dest_size, const char *src);
+// dest_size is the size of dest in char's
+int git__utf16_to_8(char *dest, size_t dest_size, const wchar_t *src);
 
-#endif
+GIT_INLINE(int) git__win32_path_utf8_to_16(git_win32_path_utf16 dest, const git_win32_path_utf8 src)
+{
+	return git__utf8_to_16(dest, GIT_WIN_PATH_UTF16, src);
+}
+
+GIT_INLINE(int) git__win32_path_utf16_to_8(git_win32_path_utf8 dest, const git_win32_path_utf16 src)
+{
+	return git__utf16_to_8(dest, GIT_WIN_PATH_UTF8, src);
+}
 
+#endif
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index ebd034a..6c741ac 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -61,7 +61,7 @@ char *cl_getenv(const char *name)
 	wchar_t *value_utf16;
 	char *value_utf8;
 
-	git__utf8_to_16(name_utf16, name);
+	git__win32_path_utf8_to_16(name_utf16, name);
 	alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0);
 	if (alloc_len <= 0)
 		return NULL;
@@ -72,7 +72,7 @@ char *cl_getenv(const char *name)
 	GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len);
 
 	cl_assert(value_utf8 = git__malloc(alloc_len));
-	git__utf16_to_8(value_utf8, value_utf16);
+	git__win32_path_utf16_to_8(value_utf8, value_utf16);
 
 	git__free(value_utf16);
 
@@ -84,10 +84,10 @@ int cl_setenv(const char *name, const char *value)
 	git_win32_path_utf16 name_utf16;
 	git_win32_path_utf16 value_utf16;
 
-	git__utf8_to_16(name_utf16, name);
+	git__win32_path_utf8_to_16(name_utf16, name);
 
 	if (value) {
-		git__utf8_to_16(value_utf16, value);
+		git__win32_path_utf8_to_16(value_utf16, value);
 		cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16));
 	} else {
 		/* Windows XP returns 0 (failed) when passing NULL for lpValue when
@@ -111,8 +111,8 @@ int cl_rename(const char *source, const char *dest)
 	git_win32_path_utf16 dest_utf16;
 	unsigned retries = 1;
 
-	git__utf8_to_16(source_utf16, source);
-	git__utf8_to_16(dest_utf16, dest);
+	git__win32_path_utf8_to_16(source_utf16, source);
+	git__win32_path_utf8_to_16(dest_utf16, dest);
 
 	while (!MoveFileW(source_utf16, dest_utf16)) {
 		/* Only retry if the error is ERROR_ACCESS_DENIED;