[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3] tools/9pfsd: Fix build error caused by strerror_r


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Henry Wang <xin.wang2@xxxxxxx>
  • Date: Thu, 7 Mar 2024 21:56:16 +0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=kwMVbPg4+WKCy5F7bQxVc2/fbCEkF426EOJxgKoiuOA=; b=XdWRjWtrSkRAA9T5zbTLZl6+HFlLAEnp7mcoCpS7oDYGnc34aI5SLaRp1rwiZzDM3p/04ZA7WR+rWhob31lAJSLO+pEkyc+HLZaw3wwB1CG6vdmrOqNinbB7IWOTONYl6EFV58AyJZMzgt9SAMQKXSs16CL/CCBLJjly6HkLQ7mZBrLYXwNaUksjF4b+n4eMlOM9v7ymqw37wctmsmgFyz8hvdRPvN88ZhsCI2xftwyJ5DvW6g1pYzS0XavKgX0LX6BgFZ287uQaM3qz2rFauA4pgk66hiaCUpI6FOtL7yTMt/odCiTm1PQLxdqQ5l7ZsYMzntMpbXVjlro3V8eKEA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XyQ4q3jfixL9gdKLG6NBP4HiurWtUXx2Bl5GbNvqXMcA0ktHPLL+CHfFGMSPzTHvuVpFrxPU/WO6FCt0KfbdFuDV5yJ44KWZBqC697SCxkRUN1gABQHJULSA9f5hbh/jSb2q66KcOxpr60XnhRo1wd23pAPw0tXAbS+7+K02mjkmdXMTK5Fu+ngussSyCqaZ5SaKiO9wg1sWWmAXkzJosY0SDBCyGVG6Z+IAJYzTodAPlXMr+WVmwaTv4ORXG3xHeOBV8REwnYaLQScER96E+2XathxgAyX6fwGyUR5FsEQcSY8EtJDaP3d2HUF43VRrjohk6k2BqluimXoM+LkJTA==
  • Cc: Henry Wang <xin.wang2@xxxxxxx>, Wei Liu <wl@xxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Juergen Gross <jgross@xxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>
  • Delivery-date: Thu, 07 Mar 2024 13:56:35 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Below error can be seen when doing Yocto build of the toolstack:

| io.c: In function 'p9_error':
| io.c:684:5: error: ignoring return value of 'strerror_r' declared
  with attribute 'warn_unused_result' [-Werror=unused-result]
|   684 |     strerror_r(err, ring->buffer, ring->ring_size);
|       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| cc1: all warnings being treated as errors

Using strerror_r() without special casing different build environments
is impossible due to the different return types (int vs char *)
depending on the environment. As p9_error() is not on a performance
critical path, using strerror() with a mutex ought to be fine. So,
fix the build by using strerror() to replace strerror_r(). The steps
would then become: Acquire the mutex first, invoke strerror(), copy
the string from strerror() to the designated buffer and then drop the
mutex.

Fixes: f4900d6d69b5 ("9pfsd: allow building with old glibc")
Signed-off-by: Henry Wang <xin.wang2@xxxxxxx>
Reviewed-by: Jan Beulich <jbeulich@xxxxxxxx>
---
v3:
- Add in-code comment and commit msg to explain why use strerror_r().
- Cosmetic and style changes about the logic.
- Add Jan's Reviewed-by tag.
- Add a "Fixes" tag.
- Use a macro-defined value for the maximum error string length.
---
 tools/9pfsd/io.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/tools/9pfsd/io.c b/tools/9pfsd/io.c
index adb887c7d9..df1be3df7d 100644
--- a/tools/9pfsd/io.c
+++ b/tools/9pfsd/io.c
@@ -677,11 +677,28 @@ static bool name_ok(const char *str)
     return true;
 }
 
+/* Including the '\0' */
+#define MAX_ERRSTR_LEN 80
 static void p9_error(struct ring *ring, uint16_t tag, uint32_t err)
 {
     unsigned int erroff;
+    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+    char *str;
+    size_t len = 0;
+
+    /*
+     * While strerror_r() exists, it comes in a POSIX and a GNU flavor.
+     * Let's try to avoid trying to be clever with determining which
+     * one it is that the underlying C library offers, when really we
+     * don't expect this function to be called very often.
+     */
+    pthread_mutex_lock(&mutex);
+    str = strerror(err);
+    len = min(strlen(str), (size_t)(MAX_ERRSTR_LEN - 1));
+    memcpy(ring->buffer, str, len);
+    ((char *)ring->buffer)[len] = '\0';
+    pthread_mutex_unlock(&mutex);
 
-    strerror_r(err, ring->buffer, ring->ring_size);
     erroff = add_string(ring, ring->buffer, strlen(ring->buffer));
     fill_buffer(ring, P9_CMD_ERROR, tag, "SU",
                 erroff != ~0 ? ring->str + erroff : "cannot allocate memory",
-- 
2.34.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.