From 294ed97e646a41b7749cd67e240138fcab5bdc5a Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Fri, 26 Apr 2019 22:40:18 +0200 Subject: [PATCH] log: remove "%m" from format strings by using strerror(errno) The printf() format specifier "%m" is a glibc extension to print the string returned by strerror(errno). While supported by other libraries (e.g. uClibc and musl), it is not widely portable. In Wayland code the format string is often passed to a logging function that calls other syscalls before the conversion of "%m" takes place. If one of such syscall modifies the value in errno, the conversion of "%m" will incorrectly report the error string corresponding to the new value of errno. Remove all the occurrences of the specifier "%m" in Wayland code by using directly the string returned by strerror(errno). Signed-off-by: Antonio Borneo --- src/event-loop.c | 4 ++-- src/scanner.c | 2 +- src/wayland-server.c | 6 +++--- src/wayland-shm.c | 4 +++- tests/test-runner.c | 3 ++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/event-loop.c b/src/event-loop.c index eb2dce6..54c8474 100644 --- a/src/event-loop.c +++ b/src/event-loop.c @@ -231,7 +231,7 @@ wl_event_source_timer_dispatch(struct wl_event_source *source, len = read(source->fd, &expires, sizeof expires); if (!(len == -1 && errno == EAGAIN) && len != sizeof expires) /* Is there anything we can do here? Will this ever happen? */ - wl_log("timerfd read error: %m\n"); + wl_log("timerfd read error: %s\n", strerror(errno)); return timer_source->func(timer_source->base.data); } @@ -325,7 +325,7 @@ wl_event_source_signal_dispatch(struct wl_event_source *source, len = read(source->fd, &signal_info, sizeof signal_info); if (!(len == -1 && errno == EAGAIN) && len != sizeof signal_info) /* Is there anything we can do here? Will this ever happen? */ - wl_log("signalfd read error: %m\n"); + wl_log("signalfd read error: %s\n", strerror(errno)); return signal_source->func(signal_source->signal_number, signal_source->base.data); diff --git a/src/scanner.c b/src/scanner.c index 86d90c0..8cffea5 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -2015,7 +2015,7 @@ int main(int argc, char *argv[]) buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE); len = fread(buf, 1, XML_BUFFER_SIZE, input); if (len < 0) { - fprintf(stderr, "fread: %m\n"); + fprintf(stderr, "fread: %s\n", strerror(errno)); fclose(input); exit(EXIT_FAILURE); } diff --git a/src/wayland-server.c b/src/wayland-server.c index 19f6a76..884a5a3 100644 --- a/src/wayland-server.c +++ b/src/wayland-server.c @@ -1362,7 +1362,7 @@ socket_data(int fd, uint32_t mask, void *data) client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name, &length); if (client_fd < 0) - wl_log("failed to accept: %m\n"); + wl_log("failed to accept: %s\n", strerror(errno)); else if (!wl_client_create(display, client_fd)) close(client_fd); @@ -1467,12 +1467,12 @@ _wl_display_add_socket(struct wl_display *display, struct wl_socket *s) size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path); if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) { - wl_log("bind() failed with error: %m\n"); + wl_log("bind() failed with error: %s\n", strerror(errno)); return -1; } if (listen(s->fd, 128) < 0) { - wl_log("listen() failed with error: %m\n"); + wl_log("listen() failed with error: %s\n", strerror(errno)); return -1; } diff --git a/src/wayland-shm.c b/src/wayland-shm.c index 4191231..967b875 100644 --- a/src/wayland-shm.c +++ b/src/wayland-shm.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "wayland-util.h" #include "wayland-private.h" @@ -281,7 +282,8 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource, if (pool->data == MAP_FAILED) { wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, - "failed mmap fd %d: %m", fd); + "failed mmap fd %d: %s", fd, + strerror(errno)); goto err_free; } close(fd); diff --git a/tests/test-runner.c b/tests/test-runner.c index 1487dc4..8f08445 100644 --- a/tests/test-runner.c +++ b/tests/test-runner.c @@ -338,7 +338,8 @@ int main(int argc, char *argv[]) if (waitid(P_PID, pid, &info, WEXITED)) { stderr_set_color(RED); - fprintf(stderr, "waitid failed: %m\n"); + fprintf(stderr, "waitid failed: %s\n", + strerror(errno)); stderr_reset_color(); abort(); -- 2.7.4