Fix uninitialized variables. 79/127479/8
authorMichal Bloch <m.bloch@samsung.com>
Wed, 26 Apr 2017 18:47:18 +0000 (20:47 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 16 May 2017 11:09:25 +0000 (11:09 +0000)
Change-Id: I59455794f050aff9b92334243cee797ac745831d
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/shared/queued_entry.c
src/tests/syslog_parser.c

index 0612c31..d9972c5 100644 (file)
@@ -25,6 +25,7 @@
 #include <assert.h>
 #include <limits.h>
 #include <ctype.h>
+#include <syslog.h>
 
 /**
  * @addtogroup SHARED_FUNCTIONS
@@ -195,55 +196,60 @@ static const int syslog_priority_mapping[] = {
 /**
  * @brief Translate syslog priority
  * @details Maps syslog priority to DLog priority
- * @param[in] prio Syslog priority
+ * @param[in] fac_prio Syslog facility/priority
  * @return DLog priority
  */
-static int translate_syslog_priority(int prio)
+static int translate_syslog_priority(int fac_prio)
 {
-       prio &= 7; // syslog priority is encoded on the three lowest bits.
+       const int prio = LOG_PRI(fac_prio);
 
+       assert(prio >= 0);
+       assert(prio < NELEMS(syslog_priority_mapping));
        return syslog_priority_mapping[prio];
 }
 
-static const char *syslog_facilities[] = {
-       "SYSLOG-KERN",
-       "SYSLOG-USER",
-       "SYSLOG-MAIL",
-       "SYSLOG-DAEMON",
-       "SYSLOG-AUTH",
-       "SYSLOG-SYSLOG",
-       "SYSLOG-LPR",
-       "SYSLOG-NEWS",
-       "SYSLOG-UUCP",
-       "SYSLOG-CRON",
-       "SYSLOG-AUTHPRIV",
-       "SYSLOG-SYSTEM",
-       "SYSLOG-SYSTEM",
-       "SYSLOG-SYSTEM",
-       "SYSLOG-SYSTEM",
-       "SYSLOG-SYSTEM",
-       "SYSLOG-LOCAL0",
-       "SYSLOG-LOCAL1",
-       "SYSLOG-LOCAL2",
-       "SYSLOG-LOCAL3",
-       "SYSLOG-LOCAL4",
-       "SYSLOG-LOCAL5",
-       "SYSLOG-LOCAL6",
-       "SYSLOG-LOCAL7",
+#define STR_FAC(x)[LOG_FAC(x)] = "SYS" #x // [LOG_FOO] = "SYSLOG_FOO"
+static const char *syslog_facilities[LOG_NFACILITIES] = {
+       STR_FAC(LOG_KERN),
+       STR_FAC(LOG_USER),
+       STR_FAC(LOG_MAIL),
+       STR_FAC(LOG_DAEMON),
+       STR_FAC(LOG_AUTH),
+       STR_FAC(LOG_SYSLOG),
+       STR_FAC(LOG_LPR),
+       STR_FAC(LOG_NEWS),
+       STR_FAC(LOG_UUCP),
+       STR_FAC(LOG_CRON),
+       STR_FAC(LOG_AUTHPRIV),
+       [11] = "SYSLOG_SYSTEM_11", // 11-15 are system reserved without any LOG_SYSTEM constants
+       [12] = "SYSLOG_SYSTEM_12",
+       [13] = "SYSLOG_SYSTEM_13",
+       [14] = "SYSLOG_SYSTEM_14",
+       [15] = "SYSLOG_SYSTEM_15",
+       STR_FAC(LOG_LOCAL0),
+       STR_FAC(LOG_LOCAL1),
+       STR_FAC(LOG_LOCAL2),
+       STR_FAC(LOG_LOCAL3),
+       STR_FAC(LOG_LOCAL4),
+       STR_FAC(LOG_LOCAL5),
+       STR_FAC(LOG_LOCAL6),
+       STR_FAC(LOG_LOCAL7),
 };
+#undef STR_FAC
 
 /**
  * @brief Translate syslog facility
  * @details Translates the integer representation of a syslog facility into a string
- * @param[in] facility The facility (as provided by syslog)
+ * @param[in] fac_prio The facility/priority (as provided by syslog)
  * @return String representation of the facility name
  */
-static const char *translate_syslog_facility(int facility)
+static const char *translate_syslog_facility(int fac_prio)
 {
-       facility >>= 3; // first three bits are priority, facility is encoded on the remaining higher bits
+       const int facility = LOG_FAC(fac_prio);
 
-       if (facility < 0 ||
-           facility >= NELEMS(syslog_facilities))
+       assert(facility >= 0);
+
+       if (facility >= NELEMS(syslog_facilities))
                return "SYSLOG-UNKNOWN";
 
        return syslog_facilities[facility];
@@ -281,6 +287,11 @@ static int parse_syslog_date(char **next, struct tm *timestamp)
  */
 static void construct_logger_entry_from_syslog(struct logger_entry *le, int priority, const char *tag, struct tm *timestamp, int pid, const char *msg)
 {
+       assert(le);
+       assert(tag);
+       assert(timestamp);
+       assert(msg);
+
        const int tag_len = strlen(tag) + 1;
        const int msg_len = strlen(msg) + 1;
 
@@ -402,9 +413,9 @@ static int parse_optional(const char **cursor, void *user_data)
  */
 int parse_syslog_datagram(const char *buffer, int buffer_size, struct logger_entry * le)
 {
-       int fac_prio, pid = 0;
+       int fac_prio = -1, pid = 0;
        struct tm timestamp;
-       const char *msg;
+       const char *msg = NULL;
 
        struct parser parsePid[] = {
                { parse_const_char, "[" },
index e3a1edc..c3ae4bb 100644 (file)
@@ -49,7 +49,7 @@ int main()
 
        assert(le.pid == 678);
        assert(le.priority == DLOG_FATAL);
-       assert(!strcmp("SYSLOG-USER", le.tag));
+       assert(!strcmp("SYSLOG_USER", le.tag));
        assert(!strcmp("msg", le.message));
 
        // Valid
@@ -59,7 +59,7 @@ int main()
 
        assert(le.pid == 0);
        assert(le.priority == DLOG_FATAL);
-       assert(!strcmp("SYSLOG-USER", le.tag));
+       assert(!strcmp("SYSLOG_USER", le.tag));
        assert(!strcmp("msg", le.message));
 
        return 0;