path: safely cast path calculation
diff --git a/src/path.c b/src/path.c
index af18edc..b1a92ae 100644
--- a/src/path.c
+++ b/src/path.c
@@ -160,7 +160,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
while (endp > path && *endp == '/')
endp--;
- if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
+ if (endp - path + 1 > INT_MAX) {
+ git_error_set(GIT_ERROR_INVALID, "path too long");
+ len = -1;
+ goto Exit;
+ }
+
+ if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
is_prefix = 1;
goto Exit;
}
@@ -180,7 +186,13 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
endp--;
} while (endp > path && *endp == '/');
- if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
+ if (endp - path + 1 > INT_MAX) {
+ git_error_set(GIT_ERROR_INVALID, "path too long");
+ len = -1;
+ goto Exit;
+ }
+
+ if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
is_prefix = 1;
goto Exit;
}