Skip to content

Commit bb3afad

Browse files
committed
Merge branch 'jk/validate-headref-fix'
Code clean-up. * jk/validate-headref-fix: validate_headref: use get_oid_hex for detached HEADs validate_headref: use skip_prefix for symref parsing validate_headref: NUL-terminate HEAD buffer
2 parents cb1083c + 0bca165 commit bb3afad

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

path.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,9 @@ void strbuf_git_common_path(struct strbuf *sb,
637637
int validate_headref(const char *path)
638638
{
639639
struct stat st;
640-
char *buf, buffer[256];
641-
unsigned char sha1[20];
640+
char buffer[256];
641+
const char *refname;
642+
struct object_id oid;
642643
int fd;
643644
ssize_t len;
644645

@@ -662,24 +663,24 @@ int validate_headref(const char *path)
662663
len = read_in_full(fd, buffer, sizeof(buffer)-1);
663664
close(fd);
664665

666+
if (len < 0)
667+
return -1;
668+
buffer[len] = '\0';
669+
665670
/*
666671
* Is it a symbolic ref?
667672
*/
668-
if (len < 4)
669-
return -1;
670-
if (!memcmp("ref:", buffer, 4)) {
671-
buf = buffer + 4;
672-
len -= 4;
673-
while (len && isspace(*buf))
674-
buf++, len--;
675-
if (len >= 5 && !memcmp("refs/", buf, 5))
673+
if (skip_prefix(buffer, "ref:", &refname)) {
674+
while (isspace(*refname))
675+
refname++;
676+
if (starts_with(refname, "refs/"))
676677
return 0;
677678
}
678679

679680
/*
680681
* Is this a detached HEAD?
681682
*/
682-
if (!get_sha1_hex(buffer, sha1))
683+
if (!get_oid_hex(buffer, &oid))
683684
return 0;
684685

685686
return -1;

0 commit comments

Comments
 (0)