[PATCH bpf-next] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno
From: Xin Liu
Date: Sat Dec 03 2022 - 04:37:50 EST
This is a small improvement in libbpf_strerror. When libbpf_strerror
is used to obtain the system error description, if the length of the
buf is insufficient, libbpf_sterror returns ERANGE and sets errno to
ERANGE.
However, this processing is not performed when the error code
customized by libbpf is obtained. Make some minor improvements here,
return -ERANGE and set errno to ERANGE when buf is not enough for
custom description.
Signed-off-by: Xin Liu <liuxin350@xxxxxxxxxx>
---
tools/lib/bpf/libbpf_errno.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c
index 96f67a772a1b..48ce7d5b5bf9 100644
--- a/tools/lib/bpf/libbpf_errno.c
+++ b/tools/lib/bpf/libbpf_errno.c
@@ -54,10 +54,16 @@ int libbpf_strerror(int err, char *buf, size_t size)
if (err < __LIBBPF_ERRNO__END) {
const char *msg;
+ size_t msg_size;
msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
snprintf(buf, size, "%s", msg);
buf[size - 1] = '\0';
+
+ msg_size = strlen(msg);
+ if (msg_size >= size)
+ return libbpf_err(-ERANGE);
+
return 0;
}
--
2.33.0