Commit 0c4004e3b4aebeca63f9345737174e8fbe69abb7

Stefan Sperling 2020-10-20T21:09:00

handle non-const dirname(3) in got_path_dirname() ok naddy

diff --git a/include/got_path.h b/include/got_path.h
index 89384c4..0219d3d 100644
--- a/include/got_path.h
+++ b/include/got_path.h
@@ -101,7 +101,10 @@ const struct got_error *got_path_mkdir(const char *);
 /* Determine whether a directory has no files or directories in it. */
 int got_path_dir_is_empty(const char *);
 
-/* dirname(3) with error handling and dynamically allocated result. */
+/*
+ * dirname(3) with error handling, dynamically allocated result, and
+ * unmodified input.
+ */
 const struct got_error *got_path_dirname(char **, const char *);
 
 /*
diff --git a/lib/path.c b/lib/path.c
index 0648aeb..54f8f6f 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -358,9 +358,13 @@ got_path_dir_is_empty(const char *dir)
 const struct got_error *
 got_path_dirname(char **parent, const char *path)
 {
+	char buf[PATH_MAX];
 	char *p;
 
-	p = dirname(path);
+	if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
+		return got_error(GOT_ERR_NO_SPACE);
+
+	p = dirname(buf);
 	if (p == NULL)
 		return got_error_from_errno2("dirname", path);