summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-bus
diff options
context:
space:
mode:
authorLuca Boccassi <bluca@debian.org>2022-10-11 21:47:38 +0200
committerGitHub <noreply@github.com>2022-10-11 21:47:38 +0200
commitda6018275903434ced990eb956423bc70e61796b (patch)
tree3682ede24865553e1e58f2e149327c71439d6a87 /src/libsystemd/sd-bus
parent2c03055e990abdb36d81a48122e31f2062a75cea (diff)
parent0b6a47957bd4f1dd4801ee6d7eb337e09dcf7099 (diff)
downloadsystemd-da6018275903434ced990eb956423bc70e61796b.tar.gz
Merge pull request #24933 from keszybz/erradicate-strerror
Erradicate strerror
Diffstat (limited to 'src/libsystemd/sd-bus')
-rw-r--r--src/libsystemd/sd-bus/bus-error.c23
-rw-r--r--src/libsystemd/sd-bus/bus-error.h8
-rw-r--r--src/libsystemd/sd-bus/test-bus-chat.c2
-rw-r--r--src/libsystemd/sd-bus/test-bus-error.c2
4 files changed, 19 insertions, 16 deletions
diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c
index 218677eac4..4d687cfac9 100644
--- a/src/libsystemd/sd-bus/bus-error.c
+++ b/src/libsystemd/sd-bus/bus-error.c
@@ -494,7 +494,7 @@ _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
*e = BUS_ERROR_FAILED;
}
- /* Now, fill in the message from strerror() if we can */
+ /* Now, fill in the message from strerror_r() if we can */
bus_error_strerror(e, error);
return -error;
}
@@ -555,7 +555,7 @@ _public_ int sd_bus_error_set_errnofv(sd_bus_error *e, int error, const char *fo
}
fail:
- /* If that didn't work, use strerror() for the message */
+ /* If that didn't work, use strerror_r() for the message */
bus_error_strerror(e, error);
return -error;
}
@@ -586,19 +586,16 @@ _public_ int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *for
return sd_bus_error_set_errno(e, error);
}
-const char *bus_error_message(const sd_bus_error *e, int error) {
+const char* _bus_error_message(const sd_bus_error *e, int error, char buf[static ERRNO_BUF_LEN]) {
+ /* Sometimes, the D-Bus server is a little bit too verbose with
+ * its error messages, so let's override them here */
+ if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
+ return "Access denied";
- if (e) {
- /* Sometimes, the D-Bus server is a little bit too verbose with
- * its error messages, so let's override them here */
- if (sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED))
- return "Access denied";
-
- if (e->message)
- return e->message;
- }
+ if (e && e->message)
+ return e->message;
- return strerror_safe(error);
+ return strerror_r(abs(error), buf, ERRNO_BUF_LEN);
}
static bool map_ok(const sd_bus_error_map *map) {
diff --git a/src/libsystemd/sd-bus/bus-error.h b/src/libsystemd/sd-bus/bus-error.h
index 518493762c..c8768c9a7b 100644
--- a/src/libsystemd/sd-bus/bus-error.h
+++ b/src/libsystemd/sd-bus/bus-error.h
@@ -5,11 +5,17 @@
#include "sd-bus.h"
+#include "errno-util.h"
#include "macro.h"
bool bus_error_is_dirty(sd_bus_error *e);
-const char *bus_error_message(const sd_bus_error *e, int error);
+const char* _bus_error_message(const sd_bus_error *e, int error, char buf[static ERRNO_BUF_LEN]);
+
+/* Note: the lifetime of the compound literal is the immediately surrounding block,
+ * see C11 ยง6.5.2.5, and
+ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
+#define bus_error_message(e, error) _bus_error_message(e, error, (char[ERRNO_BUF_LEN]){})
#define BUS_ERROR_OOM SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_MEMORY, "Out of memory")
#define BUS_ERROR_FAILED SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FAILED, "Operation failed")
diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c
index df6dd62151..93e8ebfb1b 100644
--- a/src/libsystemd/sd-bus/test-bus-chat.c
+++ b/src/libsystemd/sd-bus/test-bus-chat.c
@@ -308,7 +308,7 @@ static void* client1(void *p) {
errno = 0;
if (read(pp[0], &x, 1) <= 0) {
- log_error("Failed to read from pipe: %s", errno != 0 ? strerror_safe(errno) : "early read");
+ log_error("Failed to read from pipe: %s", STRERROR_OR_EOF(errno));
goto finish;
}
diff --git a/src/libsystemd/sd-bus/test-bus-error.c b/src/libsystemd/sd-bus/test-bus-error.c
index cc59de12b4..a55f3f9856 100644
--- a/src/libsystemd/sd-bus/test-bus-error.c
+++ b/src/libsystemd/sd-bus/test-bus-error.c
@@ -99,7 +99,7 @@ TEST(error) {
assert_se(!sd_bus_error_is_set(&error));
assert_se(sd_bus_error_set_errno(&error, EBUSY) == -EBUSY);
assert_se(streq(error.name, "System.Error.EBUSY"));
- assert_se(streq(error.message, strerror_safe(EBUSY)));
+ assert_se(streq(error.message, STRERROR(EBUSY)));
assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY"));
assert_se(sd_bus_error_get_errno(&error) == EBUSY);
assert_se(sd_bus_error_is_set(&error));