Skip to content

Commit 76d70dc

Browse files
rsahlberggitster
authored andcommitted
refs.c: make resolve_ref_unsafe set errno to something meaningful on error
Making errno when returning from resolve_ref_unsafe() meaningful, which should fix * a bug in lock_ref_sha1_basic, where it assumes EISDIR means it failed due to a directory being in the way Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Michael Haggerty <mhagger@alum.mit.edu>
1 parent d3f6655 commit 76d70dc

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ extern int read_ref(const char *refname, unsigned char *sha1);
979979
* NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
980980
* give up and return NULL.
981981
*
982-
* errno is sometimes set on errors, but not always.
982+
* errno is set to something meaningful on error.
983983
*/
984984
extern const char *resolve_ref_unsafe(const char *ref, unsigned char *sha1, int reading, int *flag);
985985
extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag);

refs.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ static const char *handle_missing_loose_ref(const char *refname,
13341334
}
13351335
}
13361336

1337+
/* This function needs to return a meaningful errno on failure */
13371338
const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
13381339
{
13391340
int depth = MAXDEPTH;
@@ -1344,17 +1345,21 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
13441345
if (flag)
13451346
*flag = 0;
13461347

1347-
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
1348+
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1349+
errno = EINVAL;
13481350
return NULL;
1351+
}
13491352

13501353
for (;;) {
13511354
char path[PATH_MAX];
13521355
struct stat st;
13531356
char *buf;
13541357
int fd;
13551358

1356-
if (--depth < 0)
1359+
if (--depth < 0) {
1360+
errno = ELOOP;
13571361
return NULL;
1362+
}
13581363

13591364
git_snpath(path, sizeof(path), "%s", refname);
13601365

@@ -1416,9 +1421,13 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
14161421
return NULL;
14171422
}
14181423
len = read_in_full(fd, buffer, sizeof(buffer)-1);
1419-
close(fd);
1420-
if (len < 0)
1424+
if (len < 0) {
1425+
int save_errno = errno;
1426+
close(fd);
1427+
errno = save_errno;
14211428
return NULL;
1429+
}
1430+
close(fd);
14221431
while (len && isspace(buffer[len-1]))
14231432
len--;
14241433
buffer[len] = '\0';
@@ -1435,6 +1444,7 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
14351444
(buffer[40] != '\0' && !isspace(buffer[40]))) {
14361445
if (flag)
14371446
*flag |= REF_ISBROKEN;
1447+
errno = EINVAL;
14381448
return NULL;
14391449
}
14401450
return refname;
@@ -1447,6 +1457,7 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
14471457
if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
14481458
if (flag)
14491459
*flag |= REF_ISBROKEN;
1460+
errno = EINVAL;
14501461
return NULL;
14511462
}
14521463
refname = strcpy(refname_buffer, buf);

0 commit comments

Comments
 (0)