From 04978bf147ac8b36e0d40221033e452f5a7ab89c Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Mon, 24 Oct 2016 21:54:13 +0200 Subject: [PATCH] Pipe: move validity check to the dedicated area Ideally, this series of checks (null message, total length, priority bounds) would be a separate function shared by all back-ends. This will be done in a further commit. Change-Id: I853c64a32e3ba5e6a72b28b5075f4fca39d6a15f Signed-off-by: Michal Bloch --- include/queued_entry.h | 2 +- src/libdlog/log_pipe.c | 19 +++++++++---------- src/shared/queued_entry.c | 17 +++++++++-------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/queued_entry.h b/include/queued_entry.h index b68b196..2fa175c 100644 --- a/include/queued_entry.h +++ b/include/queued_entry.h @@ -31,7 +31,7 @@ struct logger_entry { char msg[]; /* the entry's payload */ }; -int create_pipe_message(void * buf, int prio, char const * tag, char const * msg); +void create_pipe_message(void * buf, int prio, char const * tag, char const * msg); int parse_kmsg_message(char * buffer, int prime, int buffer_size); #endif /* _QUEUED_ENTRY_H */ diff --git a/src/libdlog/log_pipe.c b/src/libdlog/log_pipe.c index 022a628..93c7c35 100644 --- a/src/libdlog/log_pipe.c +++ b/src/libdlog/log_pipe.c @@ -111,17 +111,17 @@ static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *t char buf[LOG_MAX_SIZE]; struct logger_entry* le = (struct logger_entry*)buf; - if (log_id >= LOG_ID_MAX - || prio < DLOG_VERBOSE - || prio >= DLOG_PRIO_MAX - || !msg) - return DLOG_ERROR_INVALID_PARAMETER; - if (!tag) tag = ""; - if ((ret = create_pipe_message(buf, prio, tag, msg))) - return ret; + if (log_id >= LOG_ID_MAX || + prio < DLOG_VERBOSE || + prio >= DLOG_PRIO_MAX || + !msg || + strlen(tag) + strlen(msg) + sizeof(struct logger_entry) + 2 >= LOG_MAX_SIZE) + return DLOG_ERROR_INVALID_PARAMETER; + + create_pipe_message(buf, prio, tag, msg); if (pipe_fd[log_id] < 0 && !__reconnect_pipe(log_id)) return -1; @@ -140,8 +140,7 @@ static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *t return -1; ret = write(pipe_fd[log_id], buf, le->len); if (was_ebadf) { - if ((ret = create_pipe_message(buf, DLOG_FATAL, "LIBDLOG", "libdlog's internal state has been destroyed! The user application closed libdlog's file descriptor."))) - return ret; + create_pipe_message(buf, DLOG_FATAL, "LIBDLOG", "libdlog's internal state has been destroyed! The user application closed libdlog's file descriptor."); ret = write(pipe_fd[log_id], buf, le->len); } } diff --git a/src/shared/queued_entry.c b/src/shared/queued_entry.c index 665c0e8..e1d6cee 100644 --- a/src/shared/queued_entry.c +++ b/src/shared/queued_entry.c @@ -22,6 +22,7 @@ #include #include #include +#include /** * @brief Create pipe message @@ -30,11 +31,16 @@ * @param[in] prio Priority level * @param[in] tag Log tag * @param[in] msg Log message contents - * @return 0 on success, error value on failure - * @retval DLOG_ERROR_INVALID_PARAMETER Invalid data */ -int create_pipe_message(void * buf, int prio, char const * tag, char const * msg) +void create_pipe_message(void * buf, int prio, char const * tag, char const * msg) { + assert(buf); + assert(msg); + assert(tag); + assert(prio >= DLOG_VERBOSE); + assert(prio < DLOG_PRIO_MAX); + assert(strlen(tag) + strlen(msg) + sizeof(struct logger_entry) + 2 < LOG_MAX_SIZE); + struct logger_entry * le = (struct logger_entry *) buf; struct timespec ts; @@ -42,9 +48,6 @@ int create_pipe_message(void * buf, int prio, char const * tag, char const * msg int msg_l = strlen(msg) + 1; int len = 1 + msg_l + tag_l + sizeof(struct logger_entry); - if (len > LOG_MAX_SIZE) - return DLOG_ERROR_INVALID_PARAMETER; - clock_gettime(CLOCK_MONOTONIC, &ts); le->sec = ts.tv_sec; @@ -57,8 +60,6 @@ int create_pipe_message(void * buf, int prio, char const * tag, char const * msg memcpy(le->msg + 1, tag, tag_l); memcpy(le->msg + 1 + tag_l, msg, msg_l); le->len += le->len % 2; - - return 0; } /** -- 2.7.4