Pipe: move validity check to the dedicated area 32/93532/6
authorMichal Bloch <m.bloch@samsung.com>
Mon, 24 Oct 2016 19:54:13 +0000 (21:54 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 27 Oct 2016 20:43:08 +0000 (22:43 +0200)
  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 <m.bloch@samsung.com>
include/queued_entry.h
src/libdlog/log_pipe.c
src/shared/queued_entry.c

index b68b196..2fa175c 100644 (file)
@@ -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 */
index 022a628..93c7c35 100644 (file)
@@ -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);
                }
        }
index 665c0e8..e1d6cee 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <assert.h>
 
 /**
  * @brief Create pipe message
  * @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;
 }
 
 /**