summaryrefslogtreecommitdiff
path: root/src/journal
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-01-27 08:00:11 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-02-01 17:21:39 -0500
commit5ffa8c818120e35c89becd938d160235c069dd12 (patch)
treee4a1ce20a003e23618bd54f49cb4acf68aed70cd /src/journal
parent294929f8916ca37d89ccb1757868d22f8068c56b (diff)
Add a snprinf wrapper which checks that the buffer was big enough
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.
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/journal-send.c18
-rw-r--r--src/journal/journald-console.c9
-rw-r--r--src/journal/journald-kmsg.c9
-rw-r--r--src/journal/journald-server.c2
-rw-r--r--src/journal/journald-stream.c4
-rw-r--r--src/journal/journald-syslog.c9
-rw-r--r--src/journal/sd-journal.c3
7 files changed, 22 insertions, 32 deletions
diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c
index 65fefd25ac..1e3a463504 100644
--- a/src/journal/journal-send.c
+++ b/src/journal/journal-send.c
@@ -90,18 +90,17 @@ _public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
/* 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);
- 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);
- char_array_0(buffer);
zero(iov);
IOVEC_SET_STRING(iov[0], buffer);
@@ -372,7 +371,7 @@ static int fill_iovec_perror_and_send(const char *message, int skip, struct iove
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);
@@ -384,8 +383,7 @@ static int fill_iovec_perror_and_send(const char *message, int skip, struct iove
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);
@@ -474,7 +472,7 @@ _public_ int sd_journal_print_with_location(int priority, const char *file, cons
}
_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;
@@ -482,12 +480,10 @@ _public_ int sd_journal_printv_with_location(int priority, const char *file, con
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);
- char_array_0(buffer);
/* func is initialized from __func__ which is not a macro, but
* a static const char[], hence cannot easily be prefixed with
diff --git a/src/journal/journald-console.c b/src/journal/journald-console.c
index 4afa6ef9c3..5363aaa4ff 100644
--- a/src/journal/journald-console.c
+++ b/src/journal/journald-console.c
@@ -51,9 +51,9 @@ void server_forward_console(
const struct ucred *ucred) {
struct iovec iovec[5];
- char header_pid[16];
struct timespec ts;
- 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;
@@ -67,7 +67,7 @@ void server_forward_console(
/* 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);
@@ -80,8 +80,7 @@ void server_forward_console(
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);
diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c
index aca4571ece..0b02eff6ef 100644
--- a/src/journal/journald-kmsg.c
+++ b/src/journal/journald-kmsg.c
@@ -40,7 +40,8 @@ void server_forward_kmsg(
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;
@@ -60,8 +61,7 @@ void server_forward_kmsg(
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 */
@@ -71,8 +71,7 @@ void server_forward_kmsg(
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);
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 87b459b3ca..aecf8596dc 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -837,12 +837,10 @@ void server_driver_message(Server *s, sd_id128_t message_id, const char *format,
va_start(ap, format);
vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
va_end(ap);
- char_array_0(buffer);
IOVEC_SET_STRING(iovec[n++], buffer);
if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
snprintf(mid, sizeof(mid), LOG_MESSAGE_ID(message_id));
- char_array_0(mid);
IOVEC_SET_STRING(iovec[n++], mid);
}
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
index eabe019bfc..942a857803 100644
--- a/src/journal/journald-stream.c
+++ b/src/journal/journald-stream.c
@@ -223,7 +223,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
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;
@@ -258,7 +258,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
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);
}
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
index 21bc9679c2..ba6e3316d6 100644
--- a/src/journal/journald-syslog.c
+++ b/src/journal/journald-syslog.c
@@ -124,7 +124,8 @@ static void forward_syslog_raw(Server *s, int priority, const char *buffer, cons
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;
@@ -139,8 +140,7 @@ void server_forward_syslog(Server *s, int priority, const char *identifier, cons
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 */
@@ -159,8 +159,7 @@ void server_forward_syslog(Server *s, int priority, const char *identifier, cons
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);
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 9bc426faf8..9dea5470be 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1178,8 +1178,7 @@ static bool file_type_wanted(int flags, const char *filename) {
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;