Fix coverity issues 67/209667/4 accepted/tizen/unified/20190809.035645 submit/tizen/20190808.060025
authorYunmi Ha <yunmi.ha@samsung.com>
Wed, 10 Jul 2019 02:48:12 +0000 (11:48 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Thu, 8 Aug 2019 05:39:47 +0000 (05:39 +0000)
- check return value
- initialize the memory

Change-Id: Iebf46f98466d94991e949687ffb6310a4c1c85d5
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
src/libdlog/dynamic_config.c
src/libdlog/log.c
src/libdlog/log_pipe.c
src/shared/logconfig.c
src/tests/sort_vector.c

index 16d654c..335da1c 100644 (file)
@@ -78,9 +78,12 @@ static void __apply_update()
        __update_plog(&config);
 
        if (limiter) {
-               pthread_rwlock_wrlock(&log_limiter_lock);
-               __log_limiter_update(&config);
-               pthread_rwlock_unlock(&log_limiter_lock);
+               if (!pthread_rwlock_wrlock(&log_limiter_lock)) {
+                       __log_limiter_update(&config);
+                       pthread_rwlock_unlock(&log_limiter_lock);
+               } else {
+                       syslog_critical_failure("DLog: Failed to pthread_rwlock_wrlock for log_limiter_update.");
+               }
        }
 }
 
index 67f3916..4c4ff1c 100644 (file)
@@ -272,9 +272,11 @@ static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
                __dynamic_config_update();
 
        if (limiter) {
-               pthread_rwlock_rdlock(&log_limiter_lock);
-               int should_log = __log_limiter_pass_log(tag, prio);
-               pthread_rwlock_unlock(&log_limiter_lock);
+               int should_log = 0;
+               if (!pthread_rwlock_rdlock(&log_limiter_lock)) {
+                       should_log = __log_limiter_pass_log(tag, prio);
+                       pthread_rwlock_unlock(&log_limiter_lock);
+               }
 
                if (!should_log) {
                        return DLOG_ERROR_NOT_PERMITTED;
@@ -314,12 +316,14 @@ static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char
         * pointer could be made to point at a null handler instead of a true NULL)
         * but giving this guarantee makes everything a lot simpler as it removes
         * the risk of something suddenly becoming NULL during processing. */
-       pthread_rwlock_rdlock(&log_destruction_lock);
+       if (!pthread_rwlock_rdlock(&log_destruction_lock)) {
                ret = !initialize() || !write_to_log
                        ? DLOG_ERROR_NOT_PERMITTED
                        : __write_to_log_critical_section(log_id, prio, tag, fmt, ap, check_should_log)
                ;
-       pthread_rwlock_unlock(&log_destruction_lock);
+               pthread_rwlock_unlock(&log_destruction_lock);
+       } else
+               return DLOG_ERROR_NOT_PERMITTED;
 
        return ret;
 }
@@ -407,8 +411,8 @@ static void __attribute__((destructor))
 #endif
 __dlog_destroy(void)
 {
-       pthread_rwlock_wrlock(&log_destruction_lock);
-       __dlog_fini();
+       if (!pthread_rwlock_wrlock(&log_destruction_lock))
+               __dlog_fini();
 
        /* IMPORTANT! The lock is NEVER RELEASED. This is done ON PURPOSE.
         * The critical section can still be reached in some ways, NONE LEGAL.
index 8367bfc..789154d 100644 (file)
@@ -71,9 +71,11 @@ static int connect_pipe(const char * path)
 static int __reconnect_pipe(log_id_t log_id)
 {
        int new_fd, old_fd, ret = 0;
+       int wrlock;
 
        pthread_rwlock_unlock(&log_pipe_lock);
-       pthread_rwlock_wrlock(&log_pipe_lock);
+       if ((wrlock = pthread_rwlock_wrlock(&log_pipe_lock)) != 0)
+               goto finish;
 
        new_fd = connect_pipe(log_pipe_path[log_id]);
        if (new_fd < 0) {
@@ -108,8 +110,11 @@ static int __reconnect_pipe(log_id_t log_id)
        ret = (pipe_fd[log_id] >= 0);
 
 finish:
-       pthread_rwlock_unlock(&log_pipe_lock);
-       pthread_rwlock_rdlock(&log_pipe_lock);
+       if (!wrlock)
+               pthread_rwlock_unlock(&log_pipe_lock);
+
+       if (pthread_rwlock_rdlock(&log_pipe_lock))
+               return 0;
 
        return ret;
 }
@@ -156,7 +161,7 @@ static int _write_to_log_pipe_critical_section(log_id_t log_id, char *buf)
  */
 static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
 {
-       ssize_t ret;
+       ssize_t ret = 0;
        char buf[LOG_MAX_PAYLOAD_SIZE + sizeof(struct pipe_logger_entry)];
 
        if (!tag)
@@ -172,9 +177,10 @@ static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *t
 
        create_pipe_message(buf, prio, tag, msg);
 
-       pthread_rwlock_rdlock(&log_pipe_lock);
-       ret = _write_to_log_pipe_critical_section(log_id, buf);
-       pthread_rwlock_unlock(&log_pipe_lock);
+       if (!pthread_rwlock_rdlock(&log_pipe_lock)) {
+               ret = _write_to_log_pipe_critical_section(log_id, buf);
+               pthread_rwlock_unlock(&log_pipe_lock);
+       }
 
        return ret;
 }
index 2e45105..7d87c4d 100644 (file)
@@ -124,6 +124,8 @@ static void log_config_add(struct log_config *config, const char *key, const cha
        assert(value);
 
        struct log_conf_entry *e = calloc(1, sizeof(struct log_conf_entry));
+       if (!e)
+               return;
 
        snprintf(e->key, sizeof e->key, "%s", key);
        snprintf(e->value, sizeof e->value, "%s", value);
index ce4b33c..74cc23a 100644 (file)
@@ -78,6 +78,7 @@ int main()
        should_print_line = true;
        sv.dump = 0;
        struct log_file lf;
+       memset(&lf, 0, sizeof(struct log_file));
 
 #define ADD(x) assert(!sort_vector_push(&sv, (struct logger_entry *) x, &lf))
 #define POP(x) \