If we scale our buffer to be wide enough for the format string, we
should expect that the calculation was correct.
char_array_0() invocations are removed, since snprintf nul-terminates
the output in any case.
A similar wrapper is used for strftime calls, but only in timedatectl.c.
23 files changed:
static void automount_enter_waiting(Automount *a) {
_cleanup_close_ int ioctl_fd = -1;
int p[2] = { -1, -1 };
static void automount_enter_waiting(Automount *a) {
_cleanup_close_ int ioctl_fd = -1;
int p[2] = { -1, -1 };
- char name[32], options[128];
+ char name[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t) + 1];
+ char options[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
+ + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t) + 1];
bool mounted = false;
int r, dev_autofs_fd;
struct stat st;
bool mounted = false;
int r, dev_autofs_fd;
struct stat st;
- snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
- char_array_0(options);
-
- snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
- char_array_0(name);
-
+ xsprintf(options, "fd=%i,pgrp="PID_FMT",minproto=5,maxproto=5,direct", p[1], getpgrp());
+ xsprintf(name, "systemd-"PID_FMT, getpid());
if (mount(name, a->where, "autofs", 0, options) < 0) {
r = -errno;
goto fail;
if (mount(name, a->where, "autofs", 0, options) < 0) {
r = -errno;
goto fail;
DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buf, sizeof(buf), format, unit_description(u));
DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buf, sizeof(buf), format, unit_description(u));
REENABLE_WARNING;
if (t == JOB_START) {
REENABLE_WARNING;
if (t == JOB_START) {
args = newa(const char*, args_size);
if (!switch_root_init) {
args = newa(const char*, args_size);
if (!switch_root_init) {
+ char sfd[DECIMAL_STR_MAX(int) + 1];
/* First try to spawn ourselves with the right
* path, and with full serialization. We do
/* First try to spawn ourselves with the right
* path, and with full serialization. We do
assert(arg_serialization);
assert(fds);
assert(arg_serialization);
assert(fds);
- snprintf(sfd, sizeof(sfd), "%i", fileno(arg_serialization));
- char_array_0(sfd);
+ xsprintf(sfd, "%i", fileno(arg_serialization));
i = 0;
args[i++] = SYSTEMD_BINARY_PATH;
i = 0;
args[i++] = SYSTEMD_BINARY_PATH;
assert(command_line[pos] == NULL);
env_block = strv_copy(environ);
assert(command_line[pos] == NULL);
env_block = strv_copy(environ);
- snprintf(log_level, sizeof(log_level), "%d", log_get_max_level());
+ xsprintf(log_level, "%d", log_get_max_level());
switch (log_get_target()) {
case LOG_TARGET_KMSG:
switch (log_get_target()) {
case LOG_TARGET_KMSG:
const struct audit_info *audit = auditdata;
uid_t uid = 0, login_uid = 0;
gid_t gid = 0;
const struct audit_info *audit = auditdata;
uid_t uid = 0, login_uid = 0;
gid_t gid = 0;
- char login_uid_buf[DECIMAL_STR_MAX(uid_t)] = "n/a";
- char uid_buf[DECIMAL_STR_MAX(uid_t)] = "n/a";
- char gid_buf[DECIMAL_STR_MAX(gid_t)] = "n/a";
+ char login_uid_buf[DECIMAL_STR_MAX(uid_t) + 1] = "n/a";
+ char uid_buf[DECIMAL_STR_MAX(uid_t) + 1] = "n/a";
+ char gid_buf[DECIMAL_STR_MAX(gid_t) + 1] = "n/a";
if (sd_bus_creds_get_audit_login_uid(audit->creds, &login_uid) >= 0)
if (sd_bus_creds_get_audit_login_uid(audit->creds, &login_uid) >= 0)
- snprintf(login_uid_buf, sizeof(login_uid_buf), UID_FMT, login_uid);
+ xsprintf(login_uid_buf, UID_FMT, login_uid);
if (sd_bus_creds_get_euid(audit->creds, &uid) >= 0)
if (sd_bus_creds_get_euid(audit->creds, &uid) >= 0)
- snprintf(uid_buf, sizeof(uid_buf), UID_FMT, uid);
+ xsprintf(uid_buf, UID_FMT, uid);
if (sd_bus_creds_get_egid(audit->creds, &gid) >= 0)
if (sd_bus_creds_get_egid(audit->creds, &gid) >= 0)
- snprintf(gid_buf, sizeof(gid_buf), GID_FMT, gid);
+ xsprintf(gid_buf, GID_FMT, gid);
snprintf(msgbuf, msgbufsize,
"auid=%s uid=%s gid=%s%s%s%s%s%s%s",
snprintf(msgbuf, msgbufsize,
"auid=%s uid=%s gid=%s%s%s%s%s%s%s",
audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "",
audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : "");
audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "",
audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : "");
- msgbuf[msgbufsize-1] = 0;
-
DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buf, sizeof(buf), format, unit_description(u));
DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buf, sizeof(buf), format, unit_description(u));
REENABLE_WARNING;
mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING :
REENABLE_WARNING;
mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING :
const char *device, *type;
bool root_directory;
int progress_pipe[2] = { -1, -1 };
const char *device, *type;
bool root_directory;
int progress_pipe[2] = { -1, -1 };
+ char dash_c[sizeof("-C")-1 + DECIMAL_STR_MAX(int) + 1];
struct stat st;
if (argc > 2) {
struct stat st;
if (argc > 2) {
cmdline[i++] = "-f";
if (progress_pipe[1] >= 0) {
cmdline[i++] = "-f";
if (progress_pipe[1] >= 0) {
- snprintf(dash_c, sizeof(dash_c), "-C%i", progress_pipe[1]);
- char_array_0(dash_c);
+ xsprintf(dash_c, "-C%i", progress_pipe[1]);
static int add_raw_socket(RemoteServer *s, int fd) {
int r;
_cleanup_close_ int fd_ = fd;
static int add_raw_socket(RemoteServer *s, int fd) {
int r;
_cleanup_close_ int fd_ = fd;
- char name[strlen("raw-socket-") + DECIMAL_STR_MAX(int)];
+ char name[sizeof("raw-socket-")-1 + DECIMAL_STR_MAX(int) + 1];
- snprintf(name, sizeof(name), "raw-socket-%d", fd);
+ xsprintf(name, "raw-socket-%d", fd);
r = sd_event_source_set_description(s->listen_event, name);
if (r < 0)
r = sd_event_source_set_description(s->listen_event, name);
if (r < 0)
r = snprintf(buf + pos, size - pos,
"_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
r = snprintf(buf + pos, size - pos,
"_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
/* not enough space */
return pos;
/* not enough space */
return pos;
/* FIXME: Instead of limiting things to LINE_MAX we could do a
C99 variable-length array on the stack here in a loop. */
/* FIXME: Instead of limiting things to LINE_MAX we could do a
C99 variable-length array on the stack here in a loop. */
- char buffer[8 + LINE_MAX], p[11]; struct iovec iov[2];
+ char buffer[8 + LINE_MAX], p[sizeof("PRIORITY=")-1 + DECIMAL_STR_MAX(int) + 1];
+ struct iovec iov[2];
assert_return(priority >= 0, -EINVAL);
assert_return(priority <= 7, -EINVAL);
assert_return(format, -EINVAL);
assert_return(priority >= 0, -EINVAL);
assert_return(priority <= 7, -EINVAL);
assert_return(format, -EINVAL);
- snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
- char_array_0(p);
+ xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
memcpy(buffer, "MESSAGE=", 8);
vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
memcpy(buffer, "MESSAGE=", 8);
vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
zero(iov);
IOVEC_SET_STRING(iov[0], buffer);
zero(iov);
IOVEC_SET_STRING(iov[0], buffer);
errno = 0;
j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
if (errno == 0) {
errno = 0;
j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
if (errno == 0) {
- char error[6 + 10 + 1]; /* for a 32bit value */
+ char error[sizeof("ERRNO=")-1 + DECIMAL_STR_MAX(int) + 1];
if (j != buffer + 8 + k)
memmove(buffer + 8 + k, j, strlen(j)+1);
if (j != buffer + 8 + k)
memmove(buffer + 8 + k, j, strlen(j)+1);
memcpy(buffer + 8 + k - 2, ": ", 2);
}
memcpy(buffer + 8 + k - 2, ": ", 2);
}
- snprintf(error, sizeof(error), "ERRNO=%i", _saved_errno_);
- char_array_0(error);
+ xsprintf(error, "ERRNO=%i", _saved_errno_);
IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
IOVEC_SET_STRING(iov[skip+1], buffer);
IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
IOVEC_SET_STRING(iov[skip+1], buffer);
}
_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
}
_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
- char buffer[8 + LINE_MAX], p[11];
+ char buffer[8 + LINE_MAX], p[sizeof("PRIORITY=")-1 + DECIMAL_STR_MAX(int) + 1];
struct iovec iov[5];
char *f;
struct iovec iov[5];
char *f;
assert_return(priority <= 7, -EINVAL);
assert_return(format, -EINVAL);
assert_return(priority <= 7, -EINVAL);
assert_return(format, -EINVAL);
- snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
- char_array_0(p);
+ xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
memcpy(buffer, "MESSAGE=", 8);
vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
memcpy(buffer, "MESSAGE=", 8);
vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
/* func is initialized from __func__ which is not a macro, but
* a static const char[], hence cannot easily be prefixed with
/* func is initialized from __func__ which is not a macro, but
* a static const char[], hence cannot easily be prefixed with
const struct ucred *ucred) {
struct iovec iovec[5];
const struct ucred *ucred) {
struct iovec iovec[5];
- char tbuf[4 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
+ char tbuf[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
+ char header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t)];
int n = 0, fd;
_cleanup_free_ char *ident_buf = NULL;
const char *tty;
int n = 0, fd;
_cleanup_free_ char *ident_buf = NULL;
const char *tty;
/* First: timestamp */
if (prefix_timestamp()) {
assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
/* First: timestamp */
if (prefix_timestamp()) {
assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
- snprintf(tbuf, sizeof(tbuf), "[%5"PRI_TIME".%06ld] ",
+ xsprintf(tbuf, "[%5"PRI_TIME".%06ld] ",
ts.tv_sec,
ts.tv_nsec / 1000);
IOVEC_SET_STRING(iovec[n++], tbuf);
ts.tv_sec,
ts.tv_nsec / 1000);
IOVEC_SET_STRING(iovec[n++], tbuf);
identifier = ident_buf;
}
identifier = ident_buf;
}
- snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", ucred->pid);
- char_array_0(header_pid);
+ xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
if (identifier)
IOVEC_SET_STRING(iovec[n++], identifier);
if (identifier)
IOVEC_SET_STRING(iovec[n++], identifier);
const struct ucred *ucred) {
struct iovec iovec[5];
const struct ucred *ucred) {
struct iovec iovec[5];
- char header_priority[6], header_pid[16];
+ char header_priority[4],
+ header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t) + 1];
int n = 0;
char *ident_buf = NULL;
int n = 0;
char *ident_buf = NULL;
priority = syslog_fixup_facility(priority);
/* First: priority field */
priority = syslog_fixup_facility(priority);
/* First: priority field */
- snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
- char_array_0(header_priority);
+ xsprintf(header_priority, "<%i>", priority);
IOVEC_SET_STRING(iovec[n++], header_priority);
/* Second: identifier and PID */
IOVEC_SET_STRING(iovec[n++], header_priority);
/* Second: identifier and PID */
identifier = ident_buf;
}
identifier = ident_buf;
}
- snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", ucred->pid);
- char_array_0(header_pid);
+ xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
if (identifier)
IOVEC_SET_STRING(iovec[n++], identifier);
if (identifier)
IOVEC_SET_STRING(iovec[n++], identifier);
va_start(ap, format);
vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
va_end(ap);
va_start(ap, format);
vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
va_end(ap);
IOVEC_SET_STRING(iovec[n++], buffer);
if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
snprintf(mid, sizeof(mid), LOG_MESSAGE_ID(message_id));
IOVEC_SET_STRING(iovec[n++], buffer);
if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
snprintf(mid, sizeof(mid), LOG_MESSAGE_ID(message_id));
IOVEC_SET_STRING(iovec[n++], mid);
}
IOVEC_SET_STRING(iovec[n++], mid);
}
struct iovec iovec[N_IOVEC_META_FIELDS + 5];
int priority;
char syslog_priority[] = "PRIORITY=\0";
struct iovec iovec[N_IOVEC_META_FIELDS + 5];
int priority;
char syslog_priority[] = "PRIORITY=\0";
- char syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(priority)];
+ char syslog_facility[sizeof("SYSLOG_FACILITY=")-1 + DECIMAL_STR_MAX(int) + 1];
_cleanup_free_ char *message = NULL, *syslog_identifier = NULL;
unsigned n = 0;
char *label = NULL;
_cleanup_free_ char *message = NULL, *syslog_identifier = NULL;
unsigned n = 0;
char *label = NULL;
IOVEC_SET_STRING(iovec[n++], syslog_priority);
if (priority & LOG_FACMASK) {
IOVEC_SET_STRING(iovec[n++], syslog_priority);
if (priority & LOG_FACMASK) {
- snprintf(syslog_facility, sizeof(syslog_facility), "SYSLOG_FACILITY=%i", LOG_FAC(priority));
+ xsprintf(syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority));
IOVEC_SET_STRING(iovec[n++], syslog_facility);
}
IOVEC_SET_STRING(iovec[n++], syslog_facility);
}
void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, const struct ucred *ucred, const struct timeval *tv) {
struct iovec iovec[5];
void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, const struct ucred *ucred, const struct timeval *tv) {
struct iovec iovec[5];
- char header_priority[6], header_time[64], header_pid[16];
+ char header_priority[4], header_time[64],
+ header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t) + 1];
int n = 0;
time_t t;
struct tm *tm;
int n = 0;
time_t t;
struct tm *tm;
return;
/* First: priority field */
return;
/* First: priority field */
- snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
- char_array_0(header_priority);
+ xsprintf(header_priority, "<%i>", priority);
IOVEC_SET_STRING(iovec[n++], header_priority);
/* Second: timestamp */
IOVEC_SET_STRING(iovec[n++], header_priority);
/* Second: timestamp */
identifier = ident_buf;
}
identifier = ident_buf;
}
- snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", ucred->pid);
- char_array_0(header_pid);
+ xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
if (identifier)
IOVEC_SET_STRING(iovec[n++], identifier);
if (identifier)
IOVEC_SET_STRING(iovec[n++], identifier);
if (flags & SD_JOURNAL_CURRENT_USER) {
char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
if (flags & SD_JOURNAL_CURRENT_USER) {
char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
- assert_se(snprintf(prefix, sizeof(prefix), "user-"UID_FMT, getuid())
- < (int) sizeof(prefix));
+ xsprintf(prefix, "user-"UID_FMT, getuid());
if (file_has_type_prefix(prefix, filename))
return true;
if (file_has_type_prefix(prefix, filename))
return true;
_printf_(3,0)
static int output_vnprintf(Output *o, size_t max, const char *format, va_list args) {
_printf_(3,0)
static int output_vnprintf(Output *o, size_t max, const char *format, va_list args) {
int r;
assert_return(o, -EINVAL);
assert_return(format, -EINVAL);
int r;
assert_return(o, -EINVAL);
assert_return(format, -EINVAL);
- assert_return(max <= sizeof(buf), -EINVAL);
+ assert_return(max <= 4096, -EINVAL);
- r = vsnprintf(buf, max, format, args);
- if (r > (ssize_t)max)
- r = max;
+ r = MIN(vsnprintf(buf, max, format, args), (int) max);
return output_write(o, buf, r);
}
return output_write(o, buf, r);
}
if (c->type - BUS_MATCH_ARG < 3)
name_change_arg[c->type - BUS_MATCH_ARG] = c->value_str;
if (c->type - BUS_MATCH_ARG < 3)
name_change_arg[c->type - BUS_MATCH_ARG] = c->value_str;
- snprintf(buf, sizeof(buf), "arg%i", c->type - BUS_MATCH_ARG);
+ xsprintf(buf, "arg%i", c->type - BUS_MATCH_ARG);
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST: {
char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST: {
char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
- snprintf(buf, sizeof(buf), "arg%i-slash-prefix", c->type - BUS_MATCH_ARG_PATH);
+ xsprintf(buf, "arg%i-slash-prefix", c->type - BUS_MATCH_ARG_PATH);
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
- snprintf(buf, sizeof(buf), "arg%i-dot-prefix", c->type - BUS_MATCH_ARG_NAMESPACE);
+ xsprintf(buf, "arg%i-dot-prefix", c->type - BUS_MATCH_ARG_NAMESPACE);
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
using_bloom = true;
break;
- snprintf(t, sizeof(t), "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id));
- char_array_0(t);
+ xsprintf(t, "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id));
return bus_socket_auth_write(b, t);
}
return bus_socket_auth_write(b, t);
}
l = 9;
b->auth_buffer = hexmem("anonymous", l);
} else {
l = 9;
b->auth_buffer = hexmem("anonymous", l);
} else {
- char text[20 + 1]; /* enough space for a 64bit integer plus NUL */
+ char text[DECIMAL_STR_MAX(uid_t) + 1];
auth_prefix = "\0AUTH EXTERNAL ";
auth_prefix = "\0AUTH EXTERNAL ";
- snprintf(text, sizeof(text), UID_FMT, geteuid());
- char_array_0(text);
+ xsprintf(text, UID_FMT, geteuid());
l = strlen(text);
b->auth_buffer = hexmem(text, l);
l = strlen(text);
b->auth_buffer = hexmem(text, l);
if (show_location) {
snprintf(location, sizeof(location), "(%s:%i) ", file, line);
if (show_location) {
snprintf(location, sizeof(location), "(%s:%i) ", file, line);
- char_array_0(location);
IOVEC_SET_STRING(iovec[n++], location);
}
IOVEC_SET_STRING(iovec[n++], location);
}
const char *object,
const char *buffer) {
const char *object,
const char *buffer) {
- char header_priority[1 + DECIMAL_STR_MAX(int) + 2], header_time[64], header_pid[1 + DECIMAL_STR_MAX(pid_t) + 4];
+ char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
+ header_time[64],
+ header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
struct iovec iovec[5] = {};
struct msghdr msghdr = {
.msg_iov = iovec,
struct iovec iovec[5] = {};
struct msghdr msghdr = {
.msg_iov = iovec,
if (syslog_fd < 0)
return 0;
if (syslog_fd < 0)
return 0;
- snprintf(header_priority, sizeof(header_priority), "<%i>", level);
- char_array_0(header_priority);
+ xsprintf(header_priority, "<%i>", level);
t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
tm = localtime(&t);
t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
tm = localtime(&t);
if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
return -EINVAL;
if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
return -EINVAL;
- snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
- char_array_0(header_pid);
+ xsprintf(header_pid, "["PID_FMT"]: ", getpid());
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], header_time);
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], header_time);
const char *object,
const char *buffer) {
const char *object,
const char *buffer) {
- char header_priority[1 + DECIMAL_STR_MAX(int) + 2], header_pid[1 + DECIMAL_STR_MAX(pid_t) + 4];
+ char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
+ header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
struct iovec iovec[5] = {};
if (kmsg_fd < 0)
return 0;
struct iovec iovec[5] = {};
if (kmsg_fd < 0)
return 0;
- snprintf(header_priority, sizeof(header_priority), "<%i>", level);
- char_array_0(header_priority);
-
- snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
- char_array_0(header_pid);
+ xsprintf(header_priority, "<%i>", level);
+ xsprintf(header_pid, "["PID_FMT"]: ", getpid());
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
isempty(object) ? "" : object,
isempty(object) ? "" : "\n",
program_invocation_short_name);
isempty(object) ? "" : object,
isempty(object) ? "" : "\n",
program_invocation_short_name);
- header[size - 1] = '\0';
errno = error;
vsnprintf(buffer, sizeof(buffer), format, ap);
errno = error;
vsnprintf(buffer, sizeof(buffer), format, ap);
return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
}
return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
}
errno = error;
vsnprintf(buffer, sizeof(buffer), format, ap);
errno = error;
vsnprintf(buffer, sizeof(buffer), format, ap);
return log_dispatch(level, error, file, line, func, object_field, object, buffer);
}
return log_dispatch(level, error, file, line, func, object_field, object, buffer);
}
snprintf(buffer, sizeof(buffer), format, text, file, line, func);
REENABLE_WARNING;
snprintf(buffer, sizeof(buffer), format, text, file, line, func);
REENABLE_WARNING;
log_abort_msg = buffer;
log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
log_abort_msg = buffer;
log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
va_copy(aq, ap);
vsnprintf(buf, sizeof(buf), format, aq);
va_end(aq);
va_copy(aq, ap);
vsnprintf(buf, sizeof(buf), format, aq);
va_end(aq);
if (startswith(buf, "MESSAGE=")) {
found = true;
if (startswith(buf, "MESSAGE=")) {
found = true;
int polkit_agent_open(void) {
int r;
int pipe_fd[2];
int polkit_agent_open(void) {
int r;
int pipe_fd[2];
- char notify_fd[10 + 1];
+ char notify_fd[DECIMAL_STR_MAX(int) + 1];
if (agent_pid > 0)
return 0;
if (agent_pid > 0)
return 0;
if (pipe2(pipe_fd, 0) < 0)
return -errno;
if (pipe2(pipe_fd, 0) < 0)
return -errno;
- snprintf(notify_fd, sizeof(notify_fd), "%i", pipe_fd[1]);
- char_array_0(notify_fd);
+ xsprintf(notify_fd, "%i", pipe_fd[1]);
r = fork_agent(&agent_pid,
&pipe_fd[1], 1,
r = fork_agent(&agent_pid,
&pipe_fd[1], 1,
struct stat sb;
snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i);
struct stat sb;
snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i);
- char_array_0(new_mount);
mkdir_p_label(new_mount, 0755);
mkdir_p_label(new_mount, 0755);
int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
+#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
+
int fd_columns(int fd);
unsigned columns(void);
int fd_lines(int fd);
int fd_columns(int fd);
unsigned columns(void);
int fd_lines(int fd);
#include "pager.h"
#include "time-dst.h"
#include "pager.h"
#include "time-dst.h"
+#define xstrftime(buf, fmt, tm) assert_se(strftime(buf, sizeof(buf), fmt, tm) > 0)
+
static bool arg_no_pager = false;
static bool arg_ask_password = true;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
static bool arg_no_pager = false;
static bool arg_ask_password = true;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
if (have_time) {
fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
if (have_time) {
- zero(tm);
- assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0);
- char_array_0(a);
- printf(" Local time: %s\n", a);
-
- zero(tm);
- assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0);
- char_array_0(a);
- printf(" Universal time: %s\n", a);
+ xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
+ printf(" Local time: %.*s\n", (int) sizeof(a), a);
+
+ xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
+ printf(" Universal time: %.*s\n", (int) sizeof(a), a);
} else {
printf(" Local time: %s\n", "n/a");
printf(" Universal time: %s\n", "n/a");
} else {
printf(" Local time: %s\n", "n/a");
printf(" Universal time: %s\n", "n/a");
time_t rtc_sec;
rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
time_t rtc_sec;
rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
- zero(tm);
- assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)) > 0);
- char_array_0(a);
- printf(" RTC time: %s\n", a);
+ xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
+ printf(" RTC time: %.*s\n", (int) sizeof(a), a);
} else
printf(" RTC time: %s\n", "n/a");
} else
printf(" RTC time: %s\n", "n/a");
- if (have_time) {
- zero(tm);
- assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0);
- char_array_0(a);
- }
+ if (have_time)
+ xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
- printf(" Time zone: %s (%s)\n"
+ printf(" Time zone: %s (%.*s)\n"
" NTP enabled: %s\n"
"NTP synchronized: %s\n"
" RTC in local TZ: %s\n",
" NTP enabled: %s\n"
"NTP synchronized: %s\n"
" RTC in local TZ: %s\n",
- strna(i->timezone), have_time ? a : "n/a",
+ strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
yes_no(i->ntp_synced),
yes_no(i->rtc_local));
i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
yes_no(i->ntp_synced),
yes_no(i->rtc_local));
printf(" DST active: %s\n", yes_no(is_dstc));
t = tc - 1;
printf(" DST active: %s\n", yes_no(is_dstc));
t = tc - 1;
- zero(tm);
- assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
- char_array_0(a);
+ xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
- zero(tm);
- assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm)) > 0);
- char_array_0(b);
+ xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm));
printf(" Last DST change: DST %s at\n"
printf(" Last DST change: DST %s at\n"
- " %s\n"
- " %s\n",
- is_dstc ? "began" : "ended", a, b);
+ " %.*s\n"
+ " %.*s\n",
+ is_dstc ? "began" : "ended",
+ (int) sizeof(a), a,
+ (int) sizeof(b), b);
- zero(tm);
- assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0);
- char_array_0(a);
-
- zero(tm);
- assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm)) > 0);
- char_array_0(b);
+ xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
+ xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm));
printf(" Next DST change: DST %s (the clock jumps %s) at\n"
printf(" Next DST change: DST %s (the clock jumps %s) at\n"
- " %s\n"
- " %s\n",
- is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b);
+ " %.*s\n"
+ " %.*s\n",
+ is_dstn ? "begins" : "ends",
+ jump_str(dn, s, sizeof(s)),
+ (int) sizeof(a), a,
+ (int) sizeof(b), b);
}
} else
printf(" DST active: %s\n", yes_no(is_dstc));
}
} else
printf(" DST active: %s\n", yes_no(is_dstc));