resolve_ref(): turn buffer into a proper string as soon as possible

Immediately strip off trailing spaces and null-terminate the string holding the contents of the reference file; this allows the use of string functions and avoids the need to keep separate track of the string's length. (get_sha1_hex() fails automatically if the string is too short.) Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 15, 2011 at 23:10 UTC 287750507d3859ff65a7b0baac5ba1fdf30e9604
1 file changed +10 -10
refs.c
+10 -10
@@ -546,25 +546,25 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
546 return NULL;
547 len = read_in_full(fd, buffer, sizeof(buffer)-1);
548 close(fd);
549 + if (len < 0)
550 + return NULL;
551 + while (len && isspace(buffer[len-1]))
552 + len--;
553 + buffer[len] = '\0';
554
555 /*
556 * Is it a symbolic ref?
557 */
553 - if (len < 4 || memcmp("ref:", buffer, 4))
558 + if (prefixcmp(buffer, "ref:"))
559 break;
560 buf = buffer + 4;
556 - len -= 4;
557 - while (len && isspace(*buf))
558 - buf++, len--;
559 - while (len && isspace(buf[len-1]))
560 - len--;
561 - buf[len] = 0;
562 - memcpy(ref_buffer, buf, len + 1);
563 - ref = ref_buffer;
561 + while (isspace(*buf))
562 + buf++;
563 + ref = strcpy(ref_buffer, buf);
564 if (flag)
565 *flag |= REF_ISSYMREF;
566 }
567 - if (len < 40 || get_sha1_hex(buffer, sha1))
567 + if (get_sha1_hex(buffer, sha1))
568 return NULL;
569 return ref;
570 }