From patchwork Sat Dec 3 09:37:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Liu X-Patchwork-Id: 13063488 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1A6DC4332F for ; Sat, 3 Dec 2022 09:37:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229498AbiLCJho (ORCPT ); Sat, 3 Dec 2022 04:37:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229532AbiLCJho (ORCPT ); Sat, 3 Dec 2022 04:37:44 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AC5310063; Sat, 3 Dec 2022 01:37:42 -0800 (PST) Received: from dggpeml500010.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4NPPpd32B9z15Lxj; Sat, 3 Dec 2022 17:36:57 +0800 (CST) Received: from huawei.com (10.175.101.6) by dggpeml500010.china.huawei.com (7.185.36.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 3 Dec 2022 17:37:40 +0800 From: Xin Liu To: , , , , , , , , , , CC: , , , , , , , Subject: [PATCH bpf-next] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Date: Sat, 3 Dec 2022 17:37:40 +0800 Message-ID: <20221203093740.218935-1-liuxin350@huawei.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500010.china.huawei.com (7.185.36.155) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- 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; }