Commit d488c02c958ce23947dbd8ec541661192c8be221

Edward Thomson 2019-05-20T06:31:42

win32: safely cast path sizes for win api

diff --git a/src/win32/path_w32.c b/src/win32/path_w32.c
index b955b02..eda85ab 100644
--- a/src/win32/path_w32.c
+++ b/src/win32/path_w32.c
@@ -140,14 +140,24 @@ int git_win32_path_canonicalize(git_win32_path path)
 
 	*to = L'\0';
 
-	return (to - path);
+	if ((to - path) > INT_MAX) {
+		SetLastError(ERROR_FILENAME_EXCED_RANGE);
+		return -1;
+	}
+
+	return (int)(to - path);
 }
 
 int git_win32_path__cwd(wchar_t *out, size_t len)
 {
 	int cwd_len;
 
-	if ((cwd_len = path__cwd(out, len)) < 0)
+	if (len > INT_MAX) {
+		errno = ENAMETOOLONG;
+		return -1;
+	}
+
+	if ((cwd_len = path__cwd(out, (int)len)) < 0)
 		return -1;
 
 	/* UNC paths */