Skip to content

Commit 41052b1

Browse files
committed
Merge branch 'jk/validate-headref-fix' into maint
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 7f607f6 + 0bca165 commit 41052b1

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
@@ -635,8 +635,9 @@ void strbuf_git_common_path(struct strbuf *sb,
635635
int validate_headref(const char *path)
636636
{
637637
struct stat st;
638-
char *buf, buffer[256];
639-
unsigned char sha1[20];
638+
char buffer[256];
639+
const char *refname;
640+
struct object_id oid;
640641
int fd;
641642
ssize_t len;
642643

@@ -660,24 +661,24 @@ int validate_headref(const char *path)
660661
len = read_in_full(fd, buffer, sizeof(buffer)-1);
661662
close(fd);
662663

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

677678
/*
678679
* Is this a detached HEAD?
679680
*/
680-
if (!get_sha1_hex(buffer, sha1))
681+
if (!get_oid_hex(buffer, &oid))
681682
return 0;
682683

683684
return -1;

0 commit comments

Comments
 (0)