From: Han-Wen Nienhuys <hanwen@xxxxxxxxxx> 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@xxxxxxxxxx> Reviewed-by: Jonathan Tan <jonathantanmy@xxxxxxxxxx> --- refs.c | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/refs.c b/refs.c index 191cbf5a330f..92c4796078bb 100644 --- a/refs.c +++ b/refs.c @@ -1686,11 +1686,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_implicit_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; @@ -1703,11 +1703,12 @@ refs_resolve_ref_unsafe_implicit_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) || !refname_is_safe(refname)) { - errno = EINVAL; + *failure_errno = EINVAL; return NULL; } @@ -1730,6 +1731,8 @@ refs_resolve_ref_unsafe_implicit_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; @@ -1767,7 +1770,7 @@ refs_resolve_ref_unsafe_implicit_errno(struct ref_store *refs, if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || !refname_is_safe(refname)) { - errno = EINVAL; + *failure_errno = EINVAL; return NULL; } @@ -1775,7 +1778,7 @@ refs_resolve_ref_unsafe_implicit_errno(struct ref_store *refs, } } - errno = ELOOP; + *failure_errno = ELOOP; return NULL; } @@ -1783,22 +1786,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_implicit_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_implicit_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 */ -- gitgitgadget