Commit cadddaedc3dc6ab8e76ae4ff4a7a59ea3b3abf75

Edward Thomson 2019-05-20T06:20:18

w32: safely cast to int during charset conversion

diff --git a/src/win32/w32_buffer.c b/src/win32/w32_buffer.c
index 22ea9a1..b78a7e6 100644
--- a/src/win32/w32_buffer.c
+++ b/src/win32/w32_buffer.c
@@ -25,13 +25,17 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
 	int utf8_len, utf8_write_len;
 	size_t new_size;
 
-	if (!len_w)
+	if (!len_w) {
 		return 0;
+	} else if (len_w > INT_MAX) {
+		git_error_set_oom();
+		return -1;
+	}
 
 	assert(string_w);
 
 	/* Measure the string necessary for conversion */
-	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, NULL, 0, NULL, NULL)) == 0)
+	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, NULL, 0, NULL, NULL)) == 0)
 		return 0;
 
 	assert(utf8_len > 0);
@@ -43,7 +47,7 @@ int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
 		return -1;
 
 	if ((utf8_write_len = WideCharToMultiByte(
-			CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0)
+			CP_UTF8, WC_ERR_INVALID_CHARS, string_w, (int)len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0)
 		return handle_wc_error();
 
 	assert(utf8_write_len == utf8_len);