From b92963bbe2df9f5a871e2860337c5ce0222bbdd1 Mon Sep 17 00:00:00 2001 From: Alexei Starovoitov Date: Thu, 29 Dec 2022 19:12:17 -0800 Subject: [PATCH] libbpf: Restore errno after pr_warn. pr_warn calls into user-provided callback, which can clobber errno, so `errno = saved_errno` should happen after pr_warn. Fixes: 07453245620c ("libbpf: fix errno is overwritten after being closed.") Signed-off-by: Alexei Starovoitov --- src/libbpf_internal.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libbpf_internal.h b/src/libbpf_internal.h index 98333a6..e4d0566 100644 --- a/src/libbpf_internal.h +++ b/src/libbpf_internal.h @@ -544,8 +544,10 @@ static inline int ensure_good_fd(int fd) saved_errno = errno; close(old_fd); errno = saved_errno; - if (fd < 0) + if (fd < 0) { pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno); + errno = saved_errno; + } } return fd; }