use strndup instead of malloc+memcpy Simplifies the reading of the author/committer string and makes also the code shorter. ok stsp@
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
diff --git a/lib/privsep.c b/lib/privsep.c
index ebf6a99..438b7c7 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -1301,40 +1301,18 @@ get_commit_from_imsg(struct got_commit_object **commit,
(*commit)->committer_time = icommit->committer_time;
(*commit)->committer_gmtoff = icommit->committer_gmtoff;
- if (icommit->author_len == 0) {
- (*commit)->author = strdup("");
- if ((*commit)->author == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- } else {
- (*commit)->author = malloc(icommit->author_len + 1);
- if ((*commit)->author == NULL) {
- err = got_error_from_errno("malloc");
- goto done;
- }
- memcpy((*commit)->author, imsg->data + len,
- icommit->author_len);
- (*commit)->author[icommit->author_len] = '\0';
+ (*commit)->author = strndup(imsg->data + len, icommit->author_len);
+ if ((*commit)->author == NULL) {
+ err = got_error_from_errno("strndup");
+ goto done;
}
len += icommit->author_len;
- if (icommit->committer_len == 0) {
- (*commit)->committer = strdup("");
- if ((*commit)->committer == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- } else {
- (*commit)->committer =
- malloc(icommit->committer_len + 1);
- if ((*commit)->committer == NULL) {
- err = got_error_from_errno("malloc");
- goto done;
- }
- memcpy((*commit)->committer, imsg->data + len,
- icommit->committer_len);
- (*commit)->committer[icommit->committer_len] = '\0';
+ (*commit)->committer = strndup(imsg->data + len,
+ icommit->committer_len);
+ if ((*commit)->committer == NULL) {
+ err = got_error_from_errno("strndup");
+ goto done;
}
len += icommit->committer_len;
@@ -1901,21 +1879,10 @@ got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
- if (itag->tag_len == 0) {
- (*tag)->tag = strdup("");
- if ((*tag)->tag == NULL) {
- err = got_error_from_errno("strdup");
- break;
- }
- } else {
- (*tag)->tag = malloc(itag->tag_len + 1);
- if ((*tag)->tag == NULL) {
- err = got_error_from_errno("malloc");
- break;
- }
- memcpy((*tag)->tag, imsg.data + len,
- itag->tag_len);
- (*tag)->tag[itag->tag_len] = '\0';
+ (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
+ if ((*tag)->tag == NULL) {
+ err = got_error_from_errno("strndup");
+ break;
}
len += itag->tag_len;
@@ -1923,21 +1890,10 @@ got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
(*tag)->tagger_time = itag->tagger_time;
(*tag)->tagger_gmtoff = itag->tagger_gmtoff;
- if (itag->tagger_len == 0) {
- (*tag)->tagger = strdup("");
- if ((*tag)->tagger == NULL) {
- err = got_error_from_errno("strdup");
- break;
- }
- } else {
- (*tag)->tagger = malloc(itag->tagger_len + 1);
- if ((*tag)->tagger == NULL) {
- err = got_error_from_errno("malloc");
- break;
- }
- memcpy((*tag)->tagger, imsg.data + len,
- itag->tagger_len);
- (*tag)->tagger[itag->tagger_len] = '\0';
+ (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
+ if ((*tag)->tagger == NULL) {
+ err = got_error_from_errno("strndup");
+ break;
}
len += itag->tagger_len;