Commit 5e1c9f2326a6e3d33c6e0ff547f59e1ef91d62da

Stefan Sperling 2019-03-11T20:13:26

fix dirname() usage errors around got_path_mkdir()

diff --git a/lib/path.c b/lib/path.c
index 569469b..0e002c4 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -273,22 +273,29 @@ static const struct got_error *
 make_parent_dirs(const char *abspath)
 {
 	const struct got_error *err = NULL;
+	char *p, *parent;
 
-	char *parent = dirname(abspath);
+	p = dirname(abspath);
+	if (p == NULL)
+		return got_error_from_errno();
+	parent = strdup(p);
 	if (parent == NULL)
-		return NULL;
+		return got_error_from_errno();
 
 	if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
 		if (errno == ENOENT) {
 			err = make_parent_dirs(parent);
 			if (err)
-				return err;
-			if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
-				return got_error_from_errno();
+				goto done;
+			if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
+				err = got_error_from_errno();
+				goto done;
+			}
 		} else
 			err = got_error_from_errno();
 	}
-
+done:
+	free(parent);
 	return err;
 }
 
diff --git a/lib/reference.c b/lib/reference.c
index 8071dfb..66e4588 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -742,15 +742,21 @@ got_ref_write(struct got_reference *ref, struct got_repository *repo)
 
 	err = got_opentemp_named(&tmppath, &f, path);
 	if (err) {
-		char *parent;
+		char *p, *parent;
 		if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
 			goto done;
-		parent = dirname(path);
+		p = dirname(path);
+		if (p == NULL) {
+			err = got_error_from_errno();
+			goto done;
+		}
+		parent = strdup(p);
 		if (parent == NULL) {
 			err = got_error_from_errno();
 			goto done;
 		}
 		err = got_path_mkdir(parent);
+		free(parent);
 		if (err)
 			goto done;
 		err = got_opentemp_named(&tmppath, &f, path);