Skip to content

Commit 2877505

Browse files
mhaggergitster
authored andcommitted
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>
1 parent 1f58a03 commit 2877505

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

refs.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -546,25 +546,25 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
546546
return NULL;
547547
len = read_in_full(fd, buffer, sizeof(buffer)-1);
548548
close(fd);
549+
if (len < 0)
550+
return NULL;
551+
while (len && isspace(buffer[len-1]))
552+
len--;
553+
buffer[len] = '\0';
549554

550555
/*
551556
* Is it a symbolic ref?
552557
*/
553-
if (len < 4 || memcmp("ref:", buffer, 4))
558+
if (prefixcmp(buffer, "ref:"))
554559
break;
555560
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);
564564
if (flag)
565565
*flag |= REF_ISSYMREF;
566566
}
567-
if (len < 40 || get_sha1_hex(buffer, sha1))
567+
if (get_sha1_hex(buffer, sha1))
568568
return NULL;
569569
return ref;
570570
}

0 commit comments

Comments
 (0)