Skip to content

Commit

Permalink
refs: explicitly propagate errno from refs_read_raw_ref
Browse files Browse the repository at this point in the history
The function refs_resolve_ref_unsafe_with_errno should produce an errno output.
Rather than taking the value from the errno (which might contain garbage
beforehand), explicitly propagate the failure_errno coming out of
refs_read_raw_ref().

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
  • Loading branch information
hanwen committed Apr 29, 2021
1 parent 768e8b5 commit 18dbbe3
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1685,12 +1685,11 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
type, failure_errno);
}

/* This function needs to return a meaningful errno on failure */
static const char *refs_resolve_ref_unsafe_errno(struct ref_store *refs,
const char *refname,
int resolve_flags,
struct object_id *oid,
int *flags)
const char *refs_resolve_ref_unsafe_with_errno(struct ref_store *refs,
const char *refname,
int resolve_flags,
struct object_id *oid,
int *flags, int *failure_errno)
{
static struct strbuf sb_refname = STRBUF_INIT;
struct object_id unused_oid;
Expand All @@ -1703,6 +1702,7 @@ static const char *refs_resolve_ref_unsafe_errno(struct ref_store *refs,
flags = &unused_flags;

*flags = 0;
*failure_errno = 0;

if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
Expand All @@ -1729,6 +1729,8 @@ static const char *refs_resolve_ref_unsafe_errno(struct ref_store *refs,
&read_flags, &read_failure)) {
*flags |= read_flags;

*failure_errno = read_failure;

/* In reading mode, refs must eventually resolve */
if (resolve_flags & RESOLVE_REF_READING)
return NULL;
Expand Down Expand Up @@ -1780,22 +1782,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, const char *refname,
int resolve_flags, struct object_id *oid,
int *flags)
{
const char *result = refs_resolve_ref_unsafe_errno(
refs, refname, resolve_flags, oid, flags);
errno = 0;
return result;
}

const char *refs_resolve_ref_unsafe_with_errno(struct ref_store *refs,
const char *refname,
int resolve_flags,
struct object_id *oid,
int *flags, int *failure_errno)
{
const char *result = refs_resolve_ref_unsafe_errno(
refs, refname, resolve_flags, oid, flags);
*failure_errno = errno;
return result;
int ignore;
return refs_resolve_ref_unsafe_with_errno(refs, refname, resolve_flags,
oid, flags, &ignore);
}

/* backend functions */
Expand Down

0 comments on commit 18dbbe3

Please sign in to comment.