Commit cc483380c54d31f8ddf61787337cefe1ccf3581b

Stefan Sperling 2019-09-01T13:08:13

fix NULL deref in got_error_from_errno via got_error_uuid (found by jasper)

diff --git a/include/got_error.h b/include/got_error.h
index b156fbf..97f0473 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -323,7 +323,7 @@ const struct got_error *got_error_no_obj(struct got_object_id *);
 const struct got_error *got_error_not_ref(const char *);
 
 /* Return an error based on a uuid(3) status code. */
-const struct got_error *got_error_uuid(uint32_t);
+const struct got_error *got_error_uuid(uint32_t, const char *);
 
 /* Return an error with a path prefixed to the error message. */
 const struct got_error *got_error_path(const char *, int);
diff --git a/lib/error.c b/lib/error.c
index 1c3c04a..92ce7a5 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -157,7 +157,7 @@ got_error_not_ref(const char *refname)
 }
 
 const struct got_error *
-got_error_uuid(uint32_t uuid_status)
+got_error_uuid(uint32_t uuid_status, const char *prefix)
 {
 	switch (uuid_status) {
 	case uuid_s_ok:
@@ -167,7 +167,7 @@ got_error_uuid(uint32_t uuid_status)
 	case uuid_s_invalid_string_uuid:
 		return got_error(GOT_ERR_UUID_INVALID);
 	case uuid_s_no_memory:
-		return got_error_set_errno(ENOMEM, NULL);
+		return got_error_set_errno(ENOMEM, prefix);
 	default:
 		return got_error(GOT_ERR_UUID);
 	}
diff --git a/lib/worktree.c b/lib/worktree.c
index 3991862..71909fd 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -284,12 +284,12 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 	/* Generate UUID. */
 	uuid_create(&uuid, &uuid_status);
 	if (uuid_status != uuid_s_ok) {
-		err = got_error_uuid(uuid_status);
+		err = got_error_uuid(uuid_status, "uuid_create");
 		goto done;
 	}
 	uuid_to_string(&uuid, &uuidstr, &uuid_status);
 	if (uuid_status != uuid_s_ok) {
-		err = got_error_uuid(uuid_status);
+		err = got_error_uuid(uuid_status, "uuid_to_string");
 		goto done;
 	}
 	err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
@@ -397,7 +397,7 @@ open_worktree(struct got_worktree **worktree, const char *path)
 		goto done;
 	uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
 	if (uuid_status != uuid_s_ok) {
-		err = got_error_uuid(uuid_status);
+		err = got_error_uuid(uuid_status, "uuid_from_string");
 		goto done;
 	}
 
@@ -1455,7 +1455,7 @@ get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
 
 	uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
 	if (uuid_status != uuid_s_ok)
-		return got_error_uuid(uuid_status);
+		return got_error_uuid(uuid_status, "uuid_to_string");
 
 	if (asprintf(refname, "%s-%s", prefix, uuidstr)
 	    == -1) {