On 07.03.2024 11:38, Henry Wang wrote:
> 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
> 
> Fix the build by using strerror() to replace strerror_r(). Since
> strerror() is thread-unsafe, use a separate local mutex to protect
> the action. 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.wa...@amd.com>

Reviewed-by: Jan Beulich <jbeul...@suse.com>
albeit there are a number of cosmetic aspects I'd have done differently
(see bottom of mail). The one thing I'd really like to ask for it a
comment ...

> --- a/tools/9pfsd/io.c
> +++ b/tools/9pfsd/io.c
> @@ -680,8 +680,18 @@ static bool name_ok(const char *str)
>  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 *strerror_str;
> +    RING_IDX strerror_len = 0, copy_len = 0;
> +

... here explaining why strerror_r() isn't used. Unless other comments
arise and a v3 would be needed, I'd add

    /*
     * 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.
     */

while committing. Anyway it'll need a maintainer ack first.

> +    pthread_mutex_lock(&mutex);
> +    strerror_str = strerror(err);
> +    strerror_len = strlen(strerror_str) + 1;
> +    copy_len = min(strerror_len, ring->ring_size);
> +    memcpy(ring->buffer, strerror_str, copy_len);
> +    ((char *)(ring->buffer))[copy_len - 1] = '\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",

    pthread_mutex_lock(&mutex);
    str = strerror(err);
    len = min(strlen(str), ring->ring_size - 1);
    memcpy(ring->buffer, str, len);
    ((char *)ring->buffer)[len] = '\0';
    pthread_mutex_unlock(&mutex);

Jan

Reply via email to