error-handling: protocol, pkt
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
diff --git a/src/pkt.c b/src/pkt.c
index 51da55d..f8af7e2 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -31,13 +31,12 @@ static int flush_pkt(git_pkt **out)
git_pkt *pkt;
pkt = git__malloc(sizeof(git_pkt));
- if (pkt == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_FLUSH;
*out = pkt;
- return GIT_SUCCESS;
+ return 0;
}
/* the rest of the line will be useful for multi_ack */
@@ -48,13 +47,12 @@ static int ack_pkt(git_pkt **out, const char *line, size_t len)
GIT_UNUSED(len);
pkt = git__malloc(sizeof(git_pkt));
- if (pkt == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_ACK;
*out = pkt;
- return GIT_SUCCESS;
+ return 0;
}
static int nak_pkt(git_pkt **out)
@@ -62,13 +60,12 @@ static int nak_pkt(git_pkt **out)
git_pkt *pkt;
pkt = git__malloc(sizeof(git_pkt));
- if (pkt == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_NAK;
*out = pkt;
- return GIT_SUCCESS;
+ return 0;
}
static int pack_pkt(git_pkt **out)
@@ -76,13 +73,12 @@ static int pack_pkt(git_pkt **out)
git_pkt *pkt;
pkt = git__malloc(sizeof(git_pkt));
- if (pkt == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_PACK;
*out = pkt;
- return GIT_SUCCESS;
+ return 0;
}
static int comment_pkt(git_pkt **out, const char *line, size_t len)
@@ -90,8 +86,7 @@ static int comment_pkt(git_pkt **out, const char *line, size_t len)
git_pkt_comment *pkt;
pkt = git__malloc(sizeof(git_pkt_comment) + len + 1);
- if (pkt == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_COMMENT;
memcpy(pkt->comment, line, len);
@@ -99,7 +94,7 @@ static int comment_pkt(git_pkt **out, const char *line, size_t len)
*out = (git_pkt *) pkt;
- return GIT_SUCCESS;
+ return 0;
}
/*
@@ -108,24 +103,21 @@ static int comment_pkt(git_pkt **out, const char *line, size_t len)
static int ref_pkt(git_pkt **out, const char *line, size_t len)
{
git_pkt_ref *pkt;
- int error;
pkt = git__malloc(sizeof(git_pkt_ref));
- if (pkt == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(pkt);
memset(pkt, 0x0, sizeof(git_pkt_ref));
pkt->type = GIT_PKT_REF;
- error = git_oid_fromstr(&pkt->head.oid, line);
- if (error < GIT_SUCCESS) {
- error = git__throw(error, "Failed to parse reference ID");
- goto out;
+ if (git_oid_fromstr(&pkt->head.oid, line) < 0) {
+ giterr_set(GITERR_NET, "Error parsing pkt-line");
+ goto error_out;
}
/* Check for a bit of consistency */
if (line[GIT_OID_HEXSZ] != ' ') {
- error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse ref. No SP");
- goto out;
+ giterr_set(GITERR_NET, "Error parsing pkt-line");
+ goto error_out;
}
/* Jump from the name */
@@ -136,10 +128,8 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len)
--len;
pkt->head.name = git__malloc(len + 1);
- if (pkt->head.name == NULL) {
- error = GIT_ENOMEM;
- goto out;
- }
+ GITERR_CHECK_ALLOC(pkt->head.name);
+
memcpy(pkt->head.name, line, len);
pkt->head.name[len] = '\0';
@@ -147,20 +137,19 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len)
pkt->capabilities = strchr(pkt->head.name, '\0') + 1;
}
-out:
- if (error < GIT_SUCCESS)
- git__free(pkt);
- else
- *out = (git_pkt *)pkt;
+ *out = (git_pkt *)pkt;
- return error;
+ return 0;
+
+error_out:
+ git__free(pkt);
+ return -1;
}
-static ssize_t parse_len(const char *line)
+static int parse_len(const char *line)
{
char num[PKT_LEN_SIZE + 1];
- int i, error;
- int len;
+ int i, len;
const char *num_end;
memcpy(num, line, PKT_LEN_SIZE);
@@ -171,12 +160,10 @@ static ssize_t parse_len(const char *line)
return GIT_ENOTNUM;
}
- error = git__strtol32(&len, num, &num_end, 16);
- if (error < GIT_SUCCESS) {
- return error;
- }
+ if (git__strtol32(&len, num, &num_end, 16) < 0)
+ return -1;
- return (unsigned int) len;
+ return len;
}
/*
@@ -194,15 +181,14 @@ static ssize_t parse_len(const char *line)
int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t bufflen)
{
- int error = GIT_SUCCESS;
+ int ret = 0;
size_t len;
/* Not even enough for the length */
if (bufflen > 0 && bufflen < PKT_LEN_SIZE)
return GIT_ESHORTBUFFER;
- error = parse_len(line);
- if (error < GIT_SUCCESS) {
+ if ((ret = parse_len(line)) < 0) {
/*
* If we fail to parse the length, it might be because the
* server is trying to send us the packfile already.
@@ -212,10 +198,11 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
return pack_pkt(head);
}
- return git__throw(error, "Failed to parse pkt length");
+ giterr_set(GITERR_NET, "Error parsing pkt-line");
+ return -1;
}
- len = error;
+ len = ret;
/*
* If we were given a buffer length, then make sure there is
@@ -231,7 +218,7 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
*/
if (len == PKT_LEN_SIZE) {
*out = line;
- return GIT_SUCCESS;
+ return 0;
}
if (len == 0) { /* Flush pkt */
@@ -243,17 +230,17 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
/* Assming the minimal size is actually 4 */
if (!git__prefixcmp(line, "ACK"))
- error = ack_pkt(head, line, len);
+ ret = ack_pkt(head, line, len);
else if (!git__prefixcmp(line, "NAK"))
- error = nak_pkt(head);
+ ret = nak_pkt(head);
else if (*line == '#')
- error = comment_pkt(head, line, len);
+ ret = comment_pkt(head, line, len);
else
- error = ref_pkt(head, line, len);
+ ret = ref_pkt(head, line, len);
*out = line + len;
- return error;
+ return ret;
}
void git_pkt_free(git_pkt *pkt)
@@ -298,9 +285,8 @@ static int send_want_with_caps(git_remote_head *head, git_transport_caps *caps,
git_buf buf = GIT_BUF_INIT;
int error;
- error = buffer_want_with_caps(head, caps, &buf);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to buffer want with caps");
+ if (buffer_want_with_caps(head, caps, &buf) < 0)
+ return -1;
error = gitno_send(fd, buf.ptr, buf.size, 0);
git_buf_free(&buf);
@@ -316,7 +302,6 @@ static int send_want_with_caps(git_remote_head *head, git_transport_caps *caps,
int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_buf *buf)
{
unsigned int i = 0;
- int error;
git_remote_head *head;
if (caps->common) {
@@ -326,9 +311,8 @@ int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_b
break;
}
- error = buffer_want_with_caps(refs->contents[i], caps, buf);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to buffer want with caps");
+ if (buffer_want_with_caps(refs->contents[i], caps, buf) < 0)
+ return -1;
i++;
}
@@ -344,6 +328,8 @@ int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_b
git_buf_put(buf, pkt_want_prefix, strlen(pkt_want_prefix));
git_buf_put(buf, oid, GIT_OID_HEXSZ);
git_buf_putc(buf, '\n');
+ if (git_buf_oom(buf))
+ return -1;
}
return git_pkt_buffer_flush(buf);
@@ -352,7 +338,6 @@ int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_b
int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, int fd)
{
unsigned int i = 0;
- int error = GIT_SUCCESS;
char buf[sizeof(pkt_want_prefix) + GIT_OID_HEXSZ + 1];
git_remote_head *head;
@@ -370,9 +355,8 @@ int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, int fd)
break;
}
- error = send_want_with_caps(refs->contents[i], caps, fd);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to send want pkt with caps");
+ if (send_want_with_caps(refs->contents[i], caps, fd) < 0)
+ return -1;
/* Increase it here so it's correct whether we run this or not */
i++;
}
@@ -384,9 +368,8 @@ int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, int fd)
continue;
git_oid_fmt(buf + strlen(pkt_want_prefix), &head->oid);
- error = gitno_send(fd, buf, strlen(buf), 0);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to send want pkt");
+ if (gitno_send(fd, buf, strlen(buf), 0) < 0)
+ return -1;
}
return git_pkt_send_flush(fd);
diff --git a/src/protocol.c b/src/protocol.c
index dd93623..4c4a7f7 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -17,10 +17,12 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
const char *line_end, *ptr;
if (len == 0) { /* EOF */
- if (buf->size != 0)
- return p->error = git__throw(GIT_ERROR, "EOF and unprocessed data");
- else
+ if (buf->size != 0) {
+ giterr_set(GITERR_NET, "Unexpected EOF");
+ return p->error = -1;
+ } else {
return 0;
+ }
}
git_buf_put(buf, data, len);
@@ -34,17 +36,16 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->size);
if (error == GIT_ESHORTBUFFER)
return 0; /* Ask for more */
- if (error < GIT_SUCCESS)
- return p->error = git__rethrow(error, "Failed to parse pkt-line");
+ if (error < 0)
+ return p->error = -1;
git_buf_consume(buf, line_end);
- error = git_vector_insert(refs, pkt);
- if (error < GIT_SUCCESS)
- return p->error = git__rethrow(error, "Failed to add pkt to list");
+ if (git_vector_insert(refs, pkt) < 0)
+ return p->error = -1;
if (pkt->type == GIT_PKT_FLUSH)
p->flush = 1;
}
- return error;
+ return 0;
}