]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: Use gotos in filter control functions
authorStephen Worley <sworley@cumulusnetworks.com>
Tue, 18 Jun 2019 21:57:57 +0000 (17:57 -0400)
committerStephen Worley <sworley@cumulusnetworks.com>
Wed, 19 Jun 2019 21:20:24 +0000 (17:20 -0400)
Use gotos to reduce return paths in the log filter
add/del/dump functions.

Signed-off-by: Stephen Worley <sworley@cumulusnetworks.com>
lib/log.c

index 4a616c77801d578eacf0ad4e0dfe35fa1bbe05ce..b46e6cb2caa6102a7696dc844a7f203540cf3cdb 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -92,30 +92,33 @@ int zlog_filter_add(const char *filter)
 {
        pthread_mutex_lock(&loglock);
 
+       int ret = 0;
+
        if (zlog_filter_count >= ZLOG_FILTERS_MAX) {
-               pthread_mutex_unlock(&loglock);
-               return 1;
+               ret = 1;
+               goto done;
        }
 
        if (zlog_filter_lookup(filter) != -1) {
                /* Filter already present */
-               pthread_mutex_unlock(&loglock);
-               return -1;
+               ret = -1;
+               goto done;
        }
 
        strlcpy(zlog_filters[zlog_filter_count], filter,
                sizeof(zlog_filters[0]));
 
-       if (zlog_filters[zlog_filter_count] == NULL
-           || zlog_filters[zlog_filter_count][0] == '\0') {
-               pthread_mutex_unlock(&loglock);
-               return -1;
+       if (zlog_filters[zlog_filter_count][0] == '\0') {
+               /* Filter was either empty or didn't get copied correctly */
+               ret = -1;
+               goto done;
        }
 
        zlog_filter_count++;
 
+done:
        pthread_mutex_unlock(&loglock);
-       return 0;
+       return ret;
 }
 
 int zlog_filter_del(const char *filter)
@@ -124,11 +127,12 @@ int zlog_filter_del(const char *filter)
 
        int found_idx = zlog_filter_lookup(filter);
        int last_idx = zlog_filter_count - 1;
+       int ret = 0;
 
        if (found_idx == -1) {
                /* Didn't find the filter to delete */
-               pthread_mutex_unlock(&loglock);
-               return -1;
+               ret = -1;
+               goto done;
        }
 
        /* Adjust the filter array */
@@ -137,8 +141,9 @@ int zlog_filter_del(const char *filter)
 
        zlog_filter_count--;
 
+done:
        pthread_mutex_unlock(&loglock);
-       return 0;
+       return ret;
 }
 
 /* Dump all filters to buffer, delimited by new line */
@@ -154,13 +159,13 @@ int zlog_filter_dump(char *buf, size_t max_size)
                               zlog_filters[i]);
                len += ret;
                if ((ret < 0) || ((size_t)len >= max_size)) {
-                       pthread_mutex_unlock(&loglock);
-                       return -1;
+                       len = -1;
+                       goto done;
                }
        }
 
+done:
        pthread_mutex_unlock(&loglock);
-
        return len;
 }
 
@@ -342,7 +347,7 @@ static int vzlog_filter(struct zlog *zl, struct timestamp_control *tsctl,
 
        len += ret;
        if ((ret < 0) || ((size_t)len >= sizeof(buf)))
-               return search_buf(buf);
+               goto search;
 
        if (zl && zl->record_priority)
                snprintf(buf + len, sizeof(buf) - len, "%s: %s: %s",
@@ -351,6 +356,7 @@ static int vzlog_filter(struct zlog *zl, struct timestamp_control *tsctl,
                snprintf(buf + len, sizeof(buf) - len, "%s: %s", proto_str,
                         msg);
 
+search:
        return search_buf(buf);
 }