Commit Diff


commit - 7fc9410615d293f182da728eb3576f2e228cbfcb
commit + fcbb06bf6925b542428b937afc9f37446058e748
blob - 8dd2c6be9a9fa4eb0d7a42d3cfbb8ba4b61a9d36
blob + d4924b840343815bf194883e5d6cf8f4354da288
--- gotd/repo_write.c
+++ gotd/repo_write.c
@@ -349,11 +349,9 @@ recv_ref_update(struct imsg *imsg)
 
 	imsg_init(&ibuf, client->fd);
 
-	refname = malloc(iref.name_len + 1);
+	refname = strndup(imsg->data + sizeof(iref), iref.name_len);
 	if (refname == NULL)
-		return got_error_from_errno("malloc");
-	memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
-	refname[iref.name_len] = '\0';
+		return got_error_from_errno("strndup");
 
 	ref_update = calloc(1, sizeof(*ref_update));
 	if (ref_update == NULL) {
blob - a1e9bda45d13cafee15c71ea7364217a9a432442
blob + 2b98e5b861a9809ce19718d8a1895fa536f61c7f
--- gotd/session.c
+++ gotd/session.c
@@ -419,11 +419,9 @@ update_ref(struct gotd_session_client *client, const c
 	memcpy(&iref, imsg->data, sizeof(iref));
 	if (datalen != sizeof(iref) + iref.name_len)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	refname = malloc(iref.name_len + 1);
+	refname = strndup(imsg->data + sizeof(iref), iref.name_len);
 	if (refname == NULL)
-		return got_error_from_errno("malloc");
-	memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
-	refname[iref.name_len] = '\0';
+		return got_error_from_errno("strndup");
 
 	log_debug("updating ref %s for uid %d", refname, client->euid);
 
@@ -667,25 +665,18 @@ recv_capability(struct gotd_session_client *client, st
 	if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	key = malloc(icapa.key_len + 1);
+	key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
 	if (key == NULL)
-		return got_error_from_errno("malloc");
+		return got_error_from_errno("strndup");
 	if (icapa.value_len > 0) {
-		value = malloc(icapa.value_len + 1);
+		value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
+		    icapa.value_len);
 		if (value == NULL) {
 			free(key);
-			return got_error_from_errno("malloc");
+			return got_error_from_errno("strndup");
 		}
 	}
 
-	memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
-	key[icapa.key_len] = '\0';
-	if (value) {
-		memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
-		    icapa.value_len);
-		value[icapa.value_len] = '\0';
-	}
-
 	capa = &client->capabilities[client->ncapabilities++];
 	capa->key = key;
 	capa->value = value;
blob - 9cee3eb3d8e8579efbc1af3b1c3a92cac95e4a9f
blob + 3874b5018ba6b20013787a2fbdc8c904ad198063
--- lib/gitconfig.c
+++ lib/gitconfig.c
@@ -239,10 +239,9 @@ conf_parse_line(char **section, struct got_gitconfig *
 			*section = NULL;
 			return NULL;
 		}
-		*section = malloc(i);
+		*section = strndup(line + 1, i - 1);
 		if (*section == NULL)
-			return got_error_from_errno("malloc");
-		strlcpy(*section, line + 1, i);
+			return got_error_from_errno("strndup");
 		return NULL;
 	}
 	while (isspace((unsigned char)*line))
blob - 5462fb412f766dfe4181df6346d61dc9b5a584fb
blob + 72df858a344baaa7631049c320a8bb04a3024eda
--- lib/privsep.c
+++ lib/privsep.c
@@ -2088,14 +2088,11 @@ got_privsep_recv_gitconfig_str(char **str, struct imsg
 	case GOT_IMSG_GITCONFIG_STR_VAL:
 		if (datalen == 0)
 			break;
-		/* datalen does not include terminating \0 */
-		*str = malloc(datalen + 1);
+		*str = strndup(imsg.data, datalen);
 		if (*str == NULL) {
-			err = got_error_from_errno("malloc");
+			err = got_error_from_errno("strndup");
 			break;
 		}
-		memcpy(*str, imsg.data, datalen);
-		(*str)[datalen] = '\0';
 		break;
 	default:
 		err = got_error(GOT_ERR_PRIVSEP_MSG);
@@ -2366,14 +2363,11 @@ got_privsep_recv_gotconfig_str(char **str, struct imsg
 	case GOT_IMSG_GOTCONFIG_STR_VAL:
 		if (datalen == 0)
 			break;
-		/* datalen does not include terminating \0 */
-		*str = malloc(datalen + 1);
+		*str = strndup(imsg.data, datalen);
 		if (*str == NULL) {
-			err = got_error_from_errno("malloc");
+			err = got_error_from_errno("strndup");
 			break;
 		}
-		memcpy(*str, imsg.data, datalen);
-		(*str)[datalen] = '\0';
 		break;
 	default:
 		err = got_error(GOT_ERR_PRIVSEP_MSG);
@@ -2828,9 +2822,9 @@ got_privsep_recv_enumerated_objects(int *found_all_obj
 			}
 			memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
 			free(path);
-			path = malloc(path_len + 1);
+			path = strndup(imsg.data + sizeof(*itree), path_len);
 			if (path == NULL) {
-				err = got_error_from_errno("malloc");
+				err = got_error_from_errno("strndup");
 				break;
 			}
 			free(canon_path);
@@ -2839,9 +2833,6 @@ got_privsep_recv_enumerated_objects(int *found_all_obj
 				err = got_error_from_errno("malloc");
 				break;
 			}
-			memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
-			    path_len);
-			path[path_len] = '\0';
 			if (!got_path_is_absolute(path)) {
 				err = got_error(GOT_ERR_BAD_PATH);
 				break;
blob - f5547423081feef64d243f4fa9151ceaec1ce8c6
blob + 684b7ef9d5dcbc8a5abab29eaa64023cc4fb365c
--- lib/serve.c
+++ lib/serve.c
@@ -339,14 +339,12 @@ announce_refs(int outfd, struct imsgbuf *ibuf, int cli
 				err = got_error(GOT_ERR_PRIVSEP_LEN);
 				goto done;
 			}
-			refname = malloc(iref.name_len + 1);
+			refname = strndup(imsg.data + sizeof(iref),
+			    iref.name_len);
 			if (refname == NULL) {
-				err = got_error_from_errno("malloc");
+				err = got_error_from_errno("strndup");
 				goto done;
 			}
-			memcpy(refname, imsg.data + sizeof(iref),
-			    iref.name_len);
-			refname[iref.name_len] = '\0';
 			err = send_ref(outfd, iref.id, refname,
 			    !sent_capabilities, client_is_reading,
 			    NULL, chattygot);
@@ -382,24 +380,20 @@ announce_refs(int outfd, struct imsgbuf *ibuf, int cli
 			    symrefname != NULL || symreftarget != NULL)
 				break;
 
-			symrefname = malloc(isymref.name_len + 1);
+			symrefname = strndup(imsg.data + sizeof(isymref),
+			    isymref.name_len);
 			if (symrefname == NULL) {
 				err = got_error_from_errno("malloc");
 				goto done;
 			}
-			memcpy(symrefname, imsg.data + sizeof(isymref),
-			    isymref.name_len);
-			symrefname[isymref.name_len] = '\0';
 
-			symreftarget = malloc(isymref.target_len + 1);
+			symreftarget = strndup(
+			    imsg.data + sizeof(isymref) + isymref.name_len,
+			    isymref.target_len);
 			if (symreftarget == NULL) {
-				err = got_error_from_errno("malloc");
+				err = got_error_from_errno("strndup");
 				goto done;
 			}
-			memcpy(symreftarget,
-			    imsg.data + sizeof(isymref) + isymref.name_len,
-			    isymref.target_len);
-			symreftarget[isymref.target_len] = '\0';
 
 			if (asprintf(&symrefstr, "%s:%s", symrefname,
 			    symreftarget) == -1) {
@@ -1219,13 +1213,11 @@ report_unpack_status(struct imsg *imsg, int outfd, int
 	if (datalen != sizeof(istatus) + istatus.reason_len)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	reason = malloc(istatus.reason_len + 1);
+	reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
 	if (reason == NULL) {
-		err = got_error_from_errno("malloc");
+		err = got_error_from_errno("strndup");
 		goto done;
 	}
-	memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
-	reason[istatus.reason_len] = '\0';
 
 	if (err == NULL)
 		len = snprintf(buf, sizeof(buf), "unpack ok\n");
@@ -1260,11 +1252,9 @@ recv_ref_update_ok(struct imsg *imsg, int outfd, int c
 
 	memcpy(&iok, imsg->data, sizeof(iok));
 
-	refname = malloc(iok.name_len + 1);
+	refname = strndup(imsg->data + sizeof(iok), iok.name_len);
 	if (refname == NULL)
-		return got_error_from_errno("malloc");
-	memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
-	refname[iok.name_len] = '\0';
+		return got_error_from_errno("strndup");
 
 	len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
 	if (len >= sizeof(buf)) {
@@ -1296,20 +1286,16 @@ recv_ref_update_ng(struct imsg *imsg, int outfd, int c
 
 	memcpy(&ing, imsg->data, sizeof(ing));
 
-	refname = malloc(ing.name_len + 1);
+	refname = strndup(imsg->data + sizeof(ing), ing.name_len);
 	if (refname == NULL)
-		return got_error_from_errno("malloc");
-	memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
-	refname[ing.name_len] = '\0';
+		return got_error_from_errno("strndup");
 
-	reason = malloc(ing.reason_len + 1);
+	reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
+	    ing.reason_len);
 	if (reason == NULL) {
-		err = got_error_from_errno("malloc");
+		err = got_error_from_errno("strndup");
 		goto done;
 	}
-	memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
-	    ing.reason_len);
-	refname[ing.reason_len] = '\0';
 
 	len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
 	if (len >= sizeof(buf)) {
blob - c2b23a762db0ab53b3f2b625b26efb74dddcd5c5
blob + dbfaae68ac376c1a772e9b1e6360ddf9decc1f3e
--- libexec/got-fetch-pack/got-fetch-pack.c
+++ libexec/got-fetch-pack/got-fetch-pack.c
@@ -866,13 +866,11 @@ main(int argc, char **argv)
 			err = got_error(GOT_ERR_PRIVSEP_LEN);
 			goto done;
 		}
-		refname = malloc(href.name_len + 1);
+		refname = strndup(imsg.data + sizeof(href), href.name_len);
 		if (refname == NULL) {
-			err = got_error_from_errno("malloc");
+			err = got_error_from_errno("strndup");
 			goto done;
 		}
-		memcpy(refname, imsg.data + sizeof(href), href.name_len);
-		refname[href.name_len] = '\0';
 
 		id = malloc(sizeof(*id));
 		if (id == NULL) {
@@ -916,13 +914,12 @@ main(int argc, char **argv)
 			err = got_error(GOT_ERR_PRIVSEP_LEN);
 			goto done;
 		}
-		refname = malloc(wbranch.name_len + 1);
+		refname = strndup(imsg.data + sizeof(wbranch),
+		    wbranch.name_len);
 		if (refname == NULL) {
-			err = got_error_from_errno("malloc");
+			err = got_error_from_errno("strndup");
 			goto done;
 		}
-		memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
-		refname[wbranch.name_len] = '\0';
 
 		err = got_pathlist_append(&wanted_branches, refname, NULL);
 		if (err) {
@@ -958,13 +955,11 @@ main(int argc, char **argv)
 			err = got_error(GOT_ERR_PRIVSEP_LEN);
 			goto done;
 		}
-		refname = malloc(wref.name_len + 1);
+		refname = strndup(imsg.data + sizeof(wref), wref.name_len);
 		if (refname == NULL) {
-			err = got_error_from_errno("malloc");
+			err = got_error_from_errno("strndup");
 			goto done;
 		}
-		memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
-		refname[wref.name_len] = '\0';
 
 		err = got_pathlist_append(&wanted_refs, refname, NULL);
 		if (err) {