fix dirname() usage errors around got_path_mkdir()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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);