Skip to content

Commit 4bbccb0

Browse files
committed
tree-wide: introduce strerror_safe()
1 parent 7fa6223 commit 4bbccb0

File tree

20 files changed

+55
-38
lines changed

20 files changed

+55
-38
lines changed

src/basic/errno-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22
#pragma once
33

4+
#include <stdlib.h>
5+
#include <string.h>
6+
47
#include "macro.h"
58

69
static inline void _reset_errno_(int *saved_errno) {
@@ -28,6 +31,11 @@ static inline int negative_errno(void) {
2831
return -errno;
2932
}
3033

34+
static inline char *strerror_safe(int error) {
35+
/* 'safe' here does NOT mean thread safety. */
36+
return strerror(abs(error));
37+
}
38+
3139
/* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5.
3240
*
3341
* Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases. See the

src/core/execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4891,7 +4891,7 @@ static void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
48914891
cmd = exec_command_line(c->argv);
48924892
fprintf(f,
48934893
"%sCommand Line: %s\n",
4894-
prefix, cmd ? cmd : strerror(ENOMEM));
4894+
prefix, cmd ? cmd : strerror_safe(ENOMEM));
48954895

48964896
exec_status_dump(&c->exec_status, f, prefix2);
48974897
}

src/core/path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler) {
8989
break;
9090
}
9191

92-
r = log_warning_errno(errno, "Failed to add watch on %s: %s", s->path, errno == ENOSPC ? "too many watches" : strerror(-r));
92+
r = log_warning_errno(errno, "Failed to add watch on %s: %s", s->path, errno == ENOSPC ? "too many watches" : strerror_safe(r));
9393
if (cut)
9494
*cut = tmp;
9595
goto fail;

src/core/selinux-access.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "alloc-util.h"
1818
#include "audit-fd.h"
1919
#include "bus-util.h"
20+
#include "errno-util.h"
2021
#include "format-util.h"
2122
#include "log.h"
2223
#include "path-util.h"
@@ -158,7 +159,7 @@ static int access_init(sd_bus_error *error) {
158159
/* Return an access denied error, if we couldn't load
159160
* the AVC but enforcing mode was on, or we couldn't
160161
* determine whether it is one. */
161-
return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %s", strerror(saved_errno));
162+
return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %s", strerror_safe(saved_errno));
162163
}
163164

164165
selinux_set_callback(SELINUX_CB_AUDIT, (union selinux_callback) audit_callback);

src/core/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
782782

783783
r = socket_address_print(&p->address, &k);
784784
if (r < 0)
785-
t = strerror(-r);
785+
t = strerror_safe(r);
786786
else
787787
t = k;
788788

src/core/unit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
13261326
"%s\tMerged into: %s\n",
13271327
prefix, u->merged_into->id);
13281328
else if (u->load_state == UNIT_ERROR)
1329-
fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->load_error));
1329+
fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror_safe(u->load_error));
13301330

13311331
for (n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track))
13321332
fprintf(f, "%s\tBus Ref: %s\n", prefix, n);

src/journal-remote/journal-gatewayd.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "alloc-util.h"
1717
#include "bus-util.h"
18+
#include "errno-util.h"
1819
#include "fd-util.h"
1920
#include "fileio.h"
2021
#include "hostname-util.h"
@@ -252,7 +253,7 @@ static ssize_t request_reader_entries(
252253
errno = 0;
253254
k = fread(buf, 1, n, m->tmp);
254255
if (k != n) {
255-
log_error("Failed to read from file: %s", errno ? strerror(errno) : "Premature EOF");
256+
log_error("Failed to read from file: %s", errno != 0 ? strerror_safe(errno) : "Premature EOF");
256257
return MHD_CONTENT_READER_END_WITH_ERROR;
257258
}
258259

@@ -605,7 +606,7 @@ static ssize_t request_reader_fields(
605606
errno = 0;
606607
k = fread(buf, 1, n, m->tmp);
607608
if (k != n) {
608-
log_error("Failed to read from file: %s", errno ? strerror(errno) : "Premature EOF");
609+
log_error("Failed to read from file: %s", errno != 0 ? strerror_safe(errno) : "Premature EOF");
609610
return MHD_CONTENT_READER_END_WITH_ERROR;
610611
}
611612

src/journal/journalctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ static int add_boot(sd_journal *j) {
14251425
r = get_boots(j, NULL, &boot_id, arg_boot_offset);
14261426
assert(r <= 1);
14271427
if (r <= 0) {
1428-
const char *reason = (r == 0) ? "No such boot ID in journal" : strerror(-r);
1428+
const char *reason = (r == 0) ? "No such boot ID in journal" : strerror_safe(r);
14291429

14301430
if (sd_id128_is_null(arg_boot_id))
14311431
log_error("Data from the specified boot (%+i) is not available: %s",

src/journal/journald-server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ void server_driver_message(Server *s, pid_t object_pid, const char *message_id,
10611061
/* We failed to format the message. Emit a warning instead. */
10621062
char buf[LINE_MAX];
10631063

1064-
xsprintf(buf, "MESSAGE=Entry printing failed: %s", strerror(-r));
1064+
xsprintf(buf, "MESSAGE=Entry printing failed: %s", strerror_safe(r));
10651065

10661066
n = 3;
10671067
iovec[n++] = IOVEC_MAKE_STRING("PRIORITY=4");

src/libsystemd/sd-bus/bus-error.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ const char *bus_error_message(const sd_bus_error *e, int error) {
577577
if (error < 0)
578578
error = -error;
579579

580-
return strerror(error);
580+
return strerror_safe(error);
581581
}
582582

583583
static bool map_ok(const sd_bus_error_map *map) {

0 commit comments

Comments
 (0)