Commit c9883ee9 authored by Xin Liu's avatar Xin Liu Committed by Daniel Borkmann
Browse files

libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno



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: default avatarXin Liu <liuxin350@huawei.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20221210082045.233697-1-liuxin350@huawei.com
parent 7e68dd7d
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -39,14 +39,14 @@ static const char *libbpf_strerror_table[NR_ERRNO] = {

int libbpf_strerror(int err, char *buf, size_t size)
{
	int ret;

	if (!buf || !size)
		return libbpf_err(-EINVAL);

	err = err > 0 ? err : -err;

	if (err < __LIBBPF_ERRNO__START) {
		int ret;

		ret = strerror_r(err, buf, size);
		buf[size - 1] = '\0';
		return libbpf_err_errno(ret);
@@ -56,12 +56,20 @@ int libbpf_strerror(int err, char *buf, size_t size)
		const char *msg;

		msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
		snprintf(buf, size, "%s", msg);
		ret = snprintf(buf, size, "%s", msg);
		buf[size - 1] = '\0';
		/* The length of the buf and msg is positive.
		 * A negative number may be returned only when the
		 * size exceeds INT_MAX. Not likely to appear.
		 */
		if (ret >= size)
			return libbpf_err(-ERANGE);
		return 0;
	}

	snprintf(buf, size, "Unknown libbpf error %d", err);
	ret = snprintf(buf, size, "Unknown libbpf error %d", err);
	buf[size - 1] = '\0';
	if (ret >= size)
		return libbpf_err(-ERANGE);
	return libbpf_err(-ENOENT);
}