Reduce gotos 78/242678/4
authorAgnieszka Baumann <a.baumann@samsung.com>
Tue, 25 Aug 2020 10:43:23 +0000 (12:43 +0200)
committerMichal Bloch <m.bloch@partner.samsung.com>
Mon, 31 Aug 2020 13:04:37 +0000 (13:04 +0000)
Change-Id: I85686289d0c3ab6c2f8a572a58a553c254b88a38

include/logcommon.h
src/libdlogutil/fdi_pipe.c
src/libdlogutil/lib.c
src/libdlogutil/logretrieve.c
src/logutil/logutil.c
src/shared/logconfig.c
src/shared/loglimiter.c
tests/test_filters.c

index f50814d..e259ae8 100644 (file)
@@ -81,7 +81,7 @@ static inline void close_FILE(FILE *const *file)
                fclose(*file);
 }
 
-static inline void close_fd(int *fd)
+static inline void close_fd(const int *fd)
 {
        assert(fd);
 
index f5acd54..0739571 100644 (file)
@@ -39,20 +39,17 @@ static int pipe_get_capacity(struct fd_info *fdi, unsigned int *capacity)
        if (r < 0)
                return r;
 
-       unsigned int *data;
+       __attribute__((cleanup(free_ptr))) unsigned int *data = NULL;
        int datalen;
        r = recv_dlog_reply(ppd->sock_fd, DLOG_REQ_GET_CAPACITY, (void **)&data, &datalen);
        if (r < 0)
-               goto cleanup;
-       if (!data || datalen != sizeof *data) {
-               r = -EINVAL;
-               goto cleanup;
-       }
+               return r;
+       if (!data || datalen != sizeof *data)
+               return -EINVAL;
+
        *capacity = *data;
 
-cleanup:
-       free(data);
-       return r;
+       return 0;
 }
 
 static int pipe_get_usage(struct fd_info *fdi, unsigned int *usage)
@@ -66,22 +63,18 @@ static int pipe_get_usage(struct fd_info *fdi, unsigned int *usage)
        if (r < 0)
                return r;
 
-       unsigned int *data;
+       __attribute__((cleanup(free_ptr))) unsigned int *data = NULL;
        int datalen;
        r = recv_dlog_reply(ppd->sock_fd, DLOG_REQ_GET_USAGE, (void **)&data, &datalen);
        if (r < 0)
-               goto cleanup;
+               return r;
 
-       if (!data || datalen != sizeof *data) {
-               r = -EINVAL;
-               goto cleanup;
-       }
+       if (!data || datalen != sizeof *data)
+               return -EINVAL;
 
        *usage = *data;
 
-cleanup:
-       free(data);
-       return r;
+       return 0;
 }
 
 /**
index 035ebd9..6ff30b3 100644 (file)
 #include <tizen.h>
 #include <logcommon.h>
 
+static void filter_options_destroy(dlogutil_filter_options_s **filter)
+{
+       dlogutil_filter_options_destroy(*filter);
+}
+
 EXPORT_API dlogutil_config_s *dlogutil_config_create(void)
 {
-       dlogutil_filter_options_s *filter = NULL;
+       __attribute__((cleanup(filter_options_destroy))) dlogutil_filter_options_s *filter = dlogutil_filter_options_create();
 
-       filter = dlogutil_filter_options_create();
        if (!filter)
-               goto fail;
+               return NULL;
 
        dlogutil_config_s *ret = calloc(1, sizeof(*ret));
        if (!ret)
-               goto fail;
+               return NULL;
 
        ret->mode = DLOGUTIL_MODE_NONPRINTING;
        ret->logs_size = DEFAULT_SORT_BUFFER_SIZE;
        ret->sort_by = DLOGUTIL_SORT_DEFAULT;
        ret->filter = filter;
-       return ret;
+       filter = NULL;
 
-fail:
-       dlogutil_filter_options_destroy(filter);
-
-       return NULL;
+       return ret;
 }
 
 EXPORT_API void dlogutil_config_destroy(dlogutil_config_s *config)
index 20dce14..4cac5cf 100644 (file)
@@ -29,7 +29,7 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, bool is_pip
        assert(conf);
 
        int ret;
-       struct fd_info **fdi_ptrs;
+       __attribute__((cleanup(fdi_array_free))) struct fd_info **fdi_ptrs = NULL;
        unsigned int mask;
        int fdi_cnt = 0;
        int r;
@@ -47,10 +47,8 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, bool is_pip
        for (int i = 0; i < LOG_ID_MAX; ++i) {
                mask &= ~(1u << i);
        }
-       if ((mask & enabled_buffers) != 0) {
-               ret = -EINVAL;
-               goto failure;
-       }
+       if ((mask & enabled_buffers) != 0)
+               return -EINVAL;
 
        fdi_cnt = 0;
        for (int i = 0; i < LOG_ID_MAX; ++i) {
@@ -67,10 +65,8 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, bool is_pip
                        fdi = fdi_create(is_pipe ? &ops_pipe : &ops_logger, i);
                        break;
                }
-               if (!fdi) {
-                       ret = -ENOMEM;
-                       goto failure;
-               }
+               if (!fdi)
+                       return -ENOMEM;
 
                r = fdi->ops->create(fdi, conf, i, &used_paths, &aliased[i]);
                if (r == 0)
@@ -83,18 +79,13 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, bool is_pip
                fdi_ptrs[fdi_cnt++] = fdi;
        }
 
-       if (fdi_cnt == 0) {
-               ret = -EINVAL; // TODO: This has literally nothing to do with EINVAL
-               goto failure;
-       }
+       if (fdi_cnt == 0)
+               return -EINVAL; // TODO: This has literally nothing to do with EINVAL
 
        *fdis = fdi_ptrs;
-       return fdi_cnt;
-
-failure:
+       fdi_ptrs = NULL;
 
-       fdi_array_free(&fdi_ptrs);
-       return ret;
+       return fdi_cnt;
 }
 
 struct fd_info *find_earliest_log(struct fd_info **data_fds, int fd_count, dlogutil_sorting_order_e sort_by)
index d23568f..eac4923 100644 (file)
@@ -51,6 +51,12 @@ static void show_version(const char *name)
        printf("%s version: %s-%s\n", name, __DLOG_VERSION, __DLOG_RELEASE);
 }
 
+static int enomem_err()
+{
+       ERR("Error: out of memory\n");
+       return -ENOMEM;
+}
+
 static int parse_options(int argc, char **argv, struct log_file *l_file, int *enabled_buffers, action_e *action,
        dlogutil_config_s *config, dlogutil_mode_e *mode, unsigned int *dump_size, dlogutil_sorting_order_e *sort_by)
 {
@@ -89,7 +95,7 @@ static int parse_options(int argc, char **argv, struct log_file *l_file, int *en
                        if (sscanf(optarg, "%d", &tid) != 1)
                                err_arg_nondigit = 1;
                        else if (dlogutil_config_filter_tid(config, tid))
-                               goto enomem;
+                               return enomem_err();
                        break;
                }
                case 1: { /* pid filter */
@@ -97,7 +103,7 @@ static int parse_options(int argc, char **argv, struct log_file *l_file, int *en
                        if (sscanf(optarg, "%d", &pid) != 1)
                                err_arg_nondigit = 1;
                        else if (dlogutil_config_filter_pid(config, pid))
-                               goto enomem;
+                               return enomem_err();
                        break;
                }
                case 2: { /* version */
@@ -166,7 +172,7 @@ static int parse_options(int argc, char **argv, struct log_file *l_file, int *en
                }
                case 'f':
                        if (logfile_set_path(l_file, optarg) < 0)
-                               goto enomem;
+                               return enomem_err();
                        break;
                case 'v': {
                        l_file->format.format = log_format_from_string(optarg);
@@ -178,7 +184,7 @@ static int parse_options(int argc, char **argv, struct log_file *l_file, int *en
                }
                case 's':
                        if (dlogutil_config_filter_filterspec(config, "*:S"))
-                               goto enomem;
+                               return enomem_err();
                        break;
                case 'r':
                        if (sscanf(optarg, "%zu", &l_file->rotate_size_kbytes) != 1)
@@ -228,7 +234,7 @@ static int parse_options(int argc, char **argv, struct log_file *l_file, int *en
                        show_help(argv[0], false);
                        return -EINVAL;
                case TIZEN_ERROR_OUT_OF_MEMORY:
-                       goto enomem;
+                       return enomem_err();
                case TIZEN_ERROR_NONE:
                        continue;
                default:
@@ -245,10 +251,6 @@ static int parse_options(int argc, char **argv, struct log_file *l_file, int *en
                        // Again, not an error, since we can continue
 
        return 0;
-
-enomem:
-       ERR("Error: out of memory\n");
-       return -ENOMEM;
 }
 
 static void config_cleanup(dlogutil_config_s *const *config) {
index ad35315..910d1e9 100644 (file)
@@ -278,14 +278,14 @@ void log_config_read_dir(struct log_config *config, const char *dir_path)
        assert(config);
        assert(dir_path);
 
-       const int dir_fd = open(dir_path, O_DIRECTORY | O_RDONLY);
+       __attribute__((cleanup(close_fd))) const int dir_fd = open(dir_path, O_DIRECTORY | O_RDONLY);
        if (dir_fd < 0)
                return;
 
        struct dirent **entries;
        int entry_count = scandirat(dir_fd, ".", &entries, config_entry_filter, alphasort);
        if (entry_count < 0)
-               goto err_close;
+               return;
 
        int i;
        for (i = 0; i < entry_count; ++i) {
@@ -293,9 +293,6 @@ void log_config_read_dir(struct log_config *config, const char *dir_path)
                free(entries[i]);
        }
        free(entries);
-
-err_close:
-       close(dir_fd);
 }
 
 /**
index 8324174..5a3eb01 100644 (file)
@@ -216,13 +216,10 @@ unsigned util_hash_key(const char* s, int c)
 
        hash = ((hash << 5) + hash) + c;
 
-       if (!s || !s[0])
-               goto finish;
+       if (s)
+               while ('\0' != (c = *s++))
+                       hash = ((hash << 5) + hash) + c;
 
-       while ('\0' != (c = *s++))
-               hash = ((hash << 5) + hash) + c;
-
-finish:
        /* Makes the hash more diverse */
        hash *= HASH_MAGIC_THINGY;
 
index 3dd98dc..cb973e8 100644 (file)
@@ -35,7 +35,7 @@ int set_config(const char *filename, int flag, char prio, const char *tag, int l
        int r;
        ssize_t s;
 
-       const int fd = open(filename, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
+       __attribute__((cleanup(close_fd))) const int fd = open(filename, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
        if (fd < 0) {
                int ret = -errno;
                perror("error opening dynamic conf file");
@@ -51,7 +51,7 @@ int set_config(const char *filename, int flag, char prio, const char *tag, int l
        if (r < 0) {
                r = -errno;
                perror("snprintf error");
-               goto finish;
+               return r;
        }
 
        s = write(fd, buf, r);
@@ -61,8 +61,6 @@ int set_config(const char *filename, int flag, char prio, const char *tag, int l
        } else
                r = 0;
 
-finish:
-       assert(!close(fd));
        return r;
 }