send-pack: prefer prefixcmp over memcmp in receive_status

This code predates prefixcmp, so it used memcmp along with static sizes. Replacing these memcmps with prefixcmp makes the code much more readable, and the lack of static sizes will make refactoring it in future patches simpler. Note that we used to be unnecessarily liberal in parsing the "unpack" status line, and would accept "unpack ok\njunk". No version of git has ever produced that, and it violates the BNF in Documentation/technical/pack-protocol.txt. Let's take this opportunity to tighten the check by converting the prefix comparison into a strcmp. While we're in the area, let's also fix a vague error message that does not follow our usual conventions (it writes directly to stderr and does not use the "error:" prefix). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 20, 2013 at 15:00 UTC 8f9e3e498c113e987c2b8662485db5461cad1c36
1 file changed +4 -5
send-pack.c
+4 -5
@@ -109,9 +109,9 @@ static int receive_status(int in, struct ref *refs)
109 char line[1000];
110 int ret = 0;
111 int len = packet_read_line(in, line, sizeof(line));
112 - if (len < 10 || memcmp(line, "unpack ", 7))
112 + if (prefixcmp(line, "unpack "))
113 return error("did not receive remote status");
114 - if (memcmp(line, "unpack ok\n", 10)) {
114 + if (strcmp(line, "unpack ok\n")) {
115 char *p = line + strlen(line) - 1;
116 if (*p == '\n')
117 *p = '\0';
@@ -125,9 +125,8 @@ static int receive_status(int in, struct ref *refs)
125 len = packet_read_line(in, line, sizeof(line));
126 if (!len)
127 break;
128 - if (len < 3 ||
129 - (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
130 - fprintf(stderr, "protocol error: %s\n", line);
128 + if (prefixcmp(line, "ok ") && prefixcmp(line, "ng ")) {
129 + error("invalid ref status from remote: %s", line);
130 ret = -1;
131 break;
132 }