From: Agnieszka Baumann Date: Tue, 25 Aug 2020 10:43:23 +0000 (+0200) Subject: Reduce gotos X-Git-Tag: accepted/tizen/unified/20200908.060805~16 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=677f1fb46d4bd6f5ec8d3ae07f9d5c71adcebc37;p=platform%2Fcore%2Fsystem%2Fdlog.git Reduce gotos Change-Id: I85686289d0c3ab6c2f8a572a58a553c254b88a38 --- diff --git a/include/logcommon.h b/include/logcommon.h index f50814d..e259ae8 100644 --- a/include/logcommon.h +++ b/include/logcommon.h @@ -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); diff --git a/src/libdlogutil/fdi_pipe.c b/src/libdlogutil/fdi_pipe.c index f5acd54..0739571 100644 --- a/src/libdlogutil/fdi_pipe.c +++ b/src/libdlogutil/fdi_pipe.c @@ -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; } /** diff --git a/src/libdlogutil/lib.c b/src/libdlogutil/lib.c index 035ebd9..6ff30b3 100644 --- a/src/libdlogutil/lib.c +++ b/src/libdlogutil/lib.c @@ -22,28 +22,29 @@ #include #include +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) diff --git a/src/libdlogutil/logretrieve.c b/src/libdlogutil/logretrieve.c index 20dce14..4cac5cf 100644 --- a/src/libdlogutil/logretrieve.c +++ b/src/libdlogutil/logretrieve.c @@ -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) diff --git a/src/logutil/logutil.c b/src/logutil/logutil.c index d23568f..eac4923 100644 --- a/src/logutil/logutil.c +++ b/src/logutil/logutil.c @@ -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) { diff --git a/src/shared/logconfig.c b/src/shared/logconfig.c index ad35315..910d1e9 100644 --- a/src/shared/logconfig.c +++ b/src/shared/logconfig.c @@ -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); } /** diff --git a/src/shared/loglimiter.c b/src/shared/loglimiter.c index 8324174..5a3eb01 100644 --- a/src/shared/loglimiter.c +++ b/src/shared/loglimiter.c @@ -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; diff --git a/tests/test_filters.c b/tests/test_filters.c index 3dd98dc..cb973e8 100644 --- a/tests/test_filters.c +++ b/tests/test_filters.c @@ -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; }