>From ceba1bc2e924412913b5bd4b1d52c069de3d2f1e Mon Sep 17 00:00:00 2001
From: Ondrej Kos <okos@redhat.com>
Date: Tue, 7 Aug 2012 15:17:11 +0200
Subject: [PATCH] Backward GOTOs rewritten into do-while loops.

---
 src/providers/proxy/proxy_id.c       | 280 ++++++++++++++++++-----------------
 src/providers/proxy/proxy_services.c | 161 ++++++++++----------
 2 files changed, 232 insertions(+), 209 deletions(-)

diff --git a/src/providers/proxy/proxy_id.c b/src/providers/proxy/proxy_id.c
index 06a15b8ce5bfa701cddf04ab90d5bb91d7c53743..9b2a2fba54b424e3086c06c1366be5bc78597073 100644
--- a/src/providers/proxy/proxy_id.c
+++ b/src/providers/proxy/proxy_id.c
@@ -353,8 +353,9 @@ static int enum_users(TALLOC_CTX *mem_ctx,
     char *buffer;
     char *newbuf;
     int ret;
+    bool again;
 
-    DEBUG(7, ("Enumerating users\n"));
+    DEBUG(SSSDBG_TRACE_LIBS, ("Enumerating users\n"));
 
     tmpctx = talloc_new(mem_ctx);
     if (!tmpctx) {
@@ -386,76 +387,83 @@ static int enum_users(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-again:
-    /* always zero out the pwd structure */
-    memset(pwd, 0, sizeof(struct passwd));
-
-    /* get entry */
-    status = ctx->ops.getpwent_r(pwd, buffer, buflen, &ret);
-
-    switch (status) {
-    case NSS_STATUS_TRYAGAIN:
-        /* buffer too small ? */
-        if (buflen < MAX_BUF_SIZE) {
-            buflen *= 2;
-        }
-        if (buflen > MAX_BUF_SIZE) {
-            buflen = MAX_BUF_SIZE;
-        }
-        newbuf = talloc_realloc_size(tmpctx, buffer, buflen);
-        if (!newbuf) {
-            ret = ENOMEM;
-            goto done;
-        }
-        buffer = newbuf;
-        goto again;
-
-    case NSS_STATUS_NOTFOUND:
-
-        /* we are done here */
-        DEBUG(7, ("Enumeration completed.\n"));
-
-        ret = sysdb_transaction_commit(sysdb);
-        in_transaction = false;
-        break;
-
-    case NSS_STATUS_SUCCESS:
-
-        DEBUG(7, ("User found (%s, %d, %d)\n",
-                  pwd->pw_name, pwd->pw_uid, pwd->pw_gid));
-
-        /* uid=0 or gid=0 are invalid values */
-        /* also check that the id is in the valid range for this domain */
-        if (OUT_OF_ID_RANGE(pwd->pw_uid, dom->id_min, dom->id_max) ||
-            OUT_OF_ID_RANGE(pwd->pw_gid, dom->id_min, dom->id_max)) {
-
-            DEBUG(2, ("User [%s] filtered out! (id out of range)\n",
-                      pwd->pw_name));
-
-            goto again; /* skip */
-        }
-
-        ret = save_user(sysdb, !dom->case_sensitive, pwd,
+    do {
+        again = false;
+
+        /* always zero out the pwd structure */
+        memset(pwd, 0, sizeof(struct passwd));
+
+        /* get entry */
+        status = ctx->ops.getpwent_r(pwd, buffer, buflen, &ret);
+
+        switch (status) {
+            case NSS_STATUS_TRYAGAIN:
+                /* buffer too small ? */
+                if (buflen < MAX_BUF_SIZE) {
+                    buflen *= 2;
+                }
+                if (buflen > MAX_BUF_SIZE) {
+                    buflen = MAX_BUF_SIZE;
+                }
+                newbuf = talloc_realloc_size(tmpctx, buffer, buflen);
+                if (!newbuf) {
+                    ret = ENOMEM;
+                    goto done;
+                }
+                buffer = newbuf;
+                again = true;
+                break;
+
+            case NSS_STATUS_NOTFOUND:
+
+                /* we are done here */
+                DEBUG(SSSDBG_TRACE_LIBS, ("Enumeration completed.\n"));
+
+                ret = sysdb_transaction_commit(sysdb);
+                in_transaction = false;
+                break;
+
+            case NSS_STATUS_SUCCESS:
+
+                DEBUG(SSSDBG_TRACE_LIBS, ("User found (%s, %d, %d)\n",
+                            pwd->pw_name, pwd->pw_uid, pwd->pw_gid));
+
+                /* uid=0 or gid=0 are invalid values */
+                /* also check that the id is in the valid range for this domain
+                 */
+                if (OUT_OF_ID_RANGE(pwd->pw_uid, dom->id_min, dom->id_max) ||
+                    OUT_OF_ID_RANGE(pwd->pw_gid, dom->id_min, dom->id_max)) {
+
+                    DEBUG(SSSDBG_OP_FAILURE, ("User [%s] filtered out! (id out
+                        of range)\n", pwd->pw_name));
+
+                    again = true;
+                    break;
+                }
+
+                ret = save_user(sysdb, !dom->case_sensitive, pwd,
                         pwd->pw_name, NULL, dom->user_timeout);
-        if (ret) {
-            /* Do not fail completely on errors.
-             * Just report the failure to save and go on */
-            DEBUG(2, ("Failed to store user %s. Ignoring.\n",
-                      pwd->pw_name));
+                if (ret) {
+                    /* Do not fail completely on errors.
+                     * Just report the failure to save and go on */
+                    DEBUG(SSSDBG_OP_FAILURE, ("Failed to store user %s.
+                                Ignoring.\n", pwd->pw_name));
+                }
+                again = true;
+                break;
+
+            case NSS_STATUS_UNAVAIL:
+                /* "remote" backend unavailable. Enter offline mode */
+                ret = ENXIO;
+                break;
+
+            default:
+                ret = EIO;
+                DEBUG(SSSDBG_OP_FAILURE, ("proxy -> getpwent_r failed (%d)[%s]
+                            \n", ret, strerror(ret)));
+                break;
         }
-        goto again; /* next */
-
-    case NSS_STATUS_UNAVAIL:
-        /* "remote" backend unavailable. Enter offline mode */
-        ret = ENXIO;
-        break;
-
-    default:
-        ret = EIO;
-        DEBUG(2, ("proxy -> getpwent_r failed (%d)[%s]\n",
-                  ret, strerror(ret)));
-        break;
-    }
+    } while (again);
 
 done:
     talloc_zfree(tmpctx);
@@ -940,8 +948,9 @@ static int enum_groups(TALLOC_CTX *mem_ctx,
     char *buffer;
     char *newbuf;
     int ret;
+    bool again;
 
-    DEBUG(7, ("Enumerating groups\n"));
+    DEBUG(SSSDBG_TRACE_LIBS, ("Enumerating groups\n"));
 
     tmpctx = talloc_new(mem_ctx);
     if (!tmpctx) {
@@ -973,74 +982,82 @@ static int enum_groups(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-again:
-    /* always zero out the grp structure */
-    memset(grp, 0, sizeof(struct group));
+    do {
+        again = false;
 
-    /* get entry */
-    status = ctx->ops.getgrent_r(grp, buffer, buflen, &ret);
+        /* always zero out the grp structure */
+        memset(grp, 0, sizeof(struct group));
 
-    switch (status) {
-    case NSS_STATUS_TRYAGAIN:
-        /* buffer too small ? */
-        if (buflen < MAX_BUF_SIZE) {
-            buflen *= 2;
-        }
-        if (buflen > MAX_BUF_SIZE) {
-            buflen = MAX_BUF_SIZE;
-        }
-        newbuf = talloc_realloc_size(tmpctx, buffer, buflen);
-        if (!newbuf) {
-            ret = ENOMEM;
-            goto done;
-        }
-        buffer = newbuf;
-        goto again;
+        /* get entry */
+        status = ctx->ops.getgrent_r(grp, buffer, buflen, &ret);
 
-    case NSS_STATUS_NOTFOUND:
+        switch (status) {
+            case NSS_STATUS_TRYAGAIN:
+                /* buffer too small ? */
+                if (buflen < MAX_BUF_SIZE) {
+                    buflen *= 2;
+                }
+                if (buflen > MAX_BUF_SIZE) {
+                    buflen = MAX_BUF_SIZE;
+                }
+                newbuf = talloc_realloc_size(tmpctx, buffer, buflen);
+                if (!newbuf) {
+                    ret = ENOMEM;
+                    goto done;
+                }
+                buffer = newbuf;
+                again = true;
+                break;
 
-        /* we are done here */
-        DEBUG(7, ("Enumeration completed.\n"));
+            case NSS_STATUS_NOTFOUND:
 
-        ret = sysdb_transaction_commit(sysdb);
-        in_transaction = false;
-        break;
+                /* we are done here */
+                DEBUG(SSSDBG_TRACE_LIBS, ("Enumeration completed.\n"));
 
-    case NSS_STATUS_SUCCESS:
+                ret = sysdb_transaction_commit(sysdb);
+                in_transaction = false;
+                break;
 
-        DEBUG(7, ("Group found (%s, %d)\n",
-                  grp->gr_name, grp->gr_gid));
+            case NSS_STATUS_SUCCESS:
 
-        /* gid=0 is an invalid value */
-        /* also check that the id is in the valid range for this domain */
-        if (OUT_OF_ID_RANGE(grp->gr_gid, dom->id_min, dom->id_max)) {
+                DEBUG(SSSDBG_OP_FAILURE, ("Group found (%s, %d)\n",
+                            grp->gr_name, grp->gr_gid));
 
-                DEBUG(2, ("Group [%s] filtered out! (id out of range)\n",
-                          grp->gr_name));
+                /* gid=0 is an invalid value */
+                /* also check that the id is in the valid range for this domain
+                 */
+                if (OUT_OF_ID_RANGE(grp->gr_gid, dom->id_min, dom->id_max)) {
 
-            goto again; /* skip */
-        }
+                    DEBUG(SSSDBG_OP_FAILURE, ("Group [%s] filtered out! (id out
+                        of range)\n", grp->gr_name));
 
-        ret = save_group(sysdb, dom, grp, grp->gr_name,
-                         NULL, dom->group_timeout);
-        if (ret) {
-            /* Do not fail completely on errors.
-             * Just report the failure to save and go on */
-            DEBUG(2, ("Failed to store group. Ignoring.\n"));
-        }
-        goto again; /* next */
+                    again = true;
+                    break;
+                }
+
+                ret = save_group(sysdb, dom, grp, grp->gr_name,
+                        NULL, dom->group_timeout);
+                if (ret) {
+                    /* Do not fail completely on errors.
+                     * Just report the failure to save and go on */
+                    DEBUG(SSSDBG_OP_FAILURE, ("Failed to store group. Ignoring.
+                                \n"));
+                }
+                again = true;
+                break;
 
-    case NSS_STATUS_UNAVAIL:
-        /* "remote" backend unavailable. Enter offline mode */
-        ret = ENXIO;
-        break;
+            case NSS_STATUS_UNAVAIL:
+                /* "remote" backend unavailable. Enter offline mode */
+                ret = ENXIO;
+                break;
 
-    default:
-        ret = EIO;
-        DEBUG(2, ("proxy -> getgrent_r failed (%d)[%s]\n",
-                  ret, strerror(ret)));
-        break;
-    }
+            default:
+                ret = EIO;
+                DEBUG(SSSDBG_OP_FAILURE, ("proxy -> getgrent_r failed (%d)[%s]
+                            \n", ret, strerror(ret)));
+                break;
+        }
+    } while (again);
 
 done:
     talloc_zfree(tmpctx);
@@ -1225,13 +1242,11 @@ static int get_initgr_groups_process(TALLOC_CTX *memctx,
         return ENOMEM;
     }
 
-again:
     /* FIXME: should we move this call outside the transaction to keep the
      * transaction as short as possible ? */
-    status = ctx->ops.initgroups_dyn(pwd->pw_name, pwd->pw_gid, &num_gids,
-                                     &num, &gids, limit, &ret);
-    switch (status) {
-    case NSS_STATUS_TRYAGAIN:
+    while ( (status = ctx->ops.initgroups_dyn(pwd->pw_name, pwd->pw_gid,
+                                    &num_gids, &num, &gids, limit, &ret))
+                                    == NSS_STATUS_TRYAGAIN) {
         /* buffer too small ? */
         if (size < MAX_BUF_SIZE) {
             num *= 2;
@@ -1246,10 +1261,11 @@ again:
         if (!gids) {
             return ENOMEM;
         }
-        goto again; /* retry with more memory */
+    }
 
+    switch (status) {
     case NSS_STATUS_SUCCESS:
-        DEBUG(4, ("User [%s] appears to be member of %lu groups\n",
+        DEBUG(SSSDBG_CONF_SETTINGS, ("User [%s] appears to be member of %lu groups\n",
                   pwd->pw_name, num_gids));
 
         now = time(NULL);
diff --git a/src/providers/proxy/proxy_services.c b/src/providers/proxy/proxy_services.c
index 2b606064ce0e367f3d7273c5065c9e3b4007e349..aa19ccb68773d8bff606a5d2fe560db1cebc39d5 100644
--- a/src/providers/proxy/proxy_services.c
+++ b/src/providers/proxy/proxy_services.c
@@ -199,6 +199,7 @@ enum_services(struct proxy_id_ctx *ctx,
     time_t now = time(NULL);
     const char *protocols[2] = { NULL, NULL };
     const char **cased_aliases;
+    bool again;
 
     DEBUG(SSSDBG_TRACE_FUNC, ("Enumerating services\n"));
 
@@ -232,96 +233,102 @@ enum_services(struct proxy_id_ctx *ctx,
         goto done;
     }
 
-again:
-    /* always zero out the svc structure */
-    memset(svc, 0, sizeof(struct servent));
+    do {
+        again = false;
 
-    /* get entry */
-    status = ctx->ops.getservent_r(svc, buffer, buflen, &ret);
+        /* always zero out the svc structure */
+        memset(svc, 0, sizeof(struct servent));
 
-    switch (status) {
-    case NSS_STATUS_TRYAGAIN:
-        /* buffer too small ? */
-        if (buflen < MAX_BUF_SIZE) {
-            buflen *= 2;
-        }
-        if (buflen > MAX_BUF_SIZE) {
-            buflen = MAX_BUF_SIZE;
-        }
-        newbuf = talloc_realloc_size(tmpctx, buffer, buflen);
-        if (!newbuf) {
-            ret = ENOMEM;
-            goto done;
-        }
-        buffer = newbuf;
-        goto again;
+        /* get entry */
+        status = ctx->ops.getservent_r(svc, buffer, buflen, &ret);
 
-    case NSS_STATUS_NOTFOUND:
+        switch (status) {
+            case NSS_STATUS_TRYAGAIN:
+                /* buffer too small ? */
+                if (buflen < MAX_BUF_SIZE) {
+                    buflen *= 2;
+                }
+                if (buflen > MAX_BUF_SIZE) {
+                    buflen = MAX_BUF_SIZE;
+                }
+                newbuf = talloc_realloc_size(tmpctx, buffer, buflen);
+                if (!newbuf) {
+                    ret = ENOMEM;
+                    goto done;
+                }
+                buffer = newbuf;
+                again = true;
+                break;
 
-        /* we are done here */
-        DEBUG(SSSDBG_TRACE_FUNC, ("Enumeration completed.\n"));
+            case NSS_STATUS_NOTFOUND:
 
-        ret = sysdb_transaction_commit(sysdb);
-        if (ret != EOK) goto done;
+                /* we are done here */
+                DEBUG(SSSDBG_TRACE_FUNC, ("Enumeration completed.\n"));
 
-        in_transaction = false;
-        break;
+                ret = sysdb_transaction_commit(sysdb);
+                if (ret != EOK) goto done;
 
-    case NSS_STATUS_SUCCESS:
+                in_transaction = false;
+                break;
 
-        DEBUG(SSSDBG_TRACE_INTERNAL,
-              ("Service found (%s, %d/%s)\n",
-               svc->s_name, svc->s_port, svc->s_proto));
+            case NSS_STATUS_SUCCESS:
 
-        protocols[0] = sss_get_cased_name(protocols, svc->s_proto,
-                                          dom->case_sensitive);
-        if (!protocols[0]) {
-            ret = ENOMEM;
-            goto done;
-        }
-        protocols[1] = NULL;
+                DEBUG(SSSDBG_TRACE_INTERNAL,
+                        ("Service found (%s, %d/%s)\n",
+                         svc->s_name, svc->s_port, svc->s_proto));
 
-        ret = sss_get_cased_name_list(tmpctx,
-                                      (const char * const *) svc->s_aliases,
-                                      dom->case_sensitive, &cased_aliases);
-        if (ret != EOK) {
-            /* Do not fail completely on errors.
-             * Just report the failure to save and go on */
-            DEBUG(SSSDBG_OP_FAILURE,
-                  ("Failed to store service [%s]. Ignoring.\n",
-                   strerror(ret)));
-            goto again; /* next */
-        }
+                protocols[0] = sss_get_cased_name(protocols, svc->s_proto,
+                        dom->case_sensitive);
+                if (!protocols[0]) {
+                    ret = ENOMEM;
+                    goto done;
+                }
+                protocols[1] = NULL;
 
-        ret = sysdb_store_service(sysdb,
-                                  svc->s_name,
-                                  svc->s_port,
-                                  cased_aliases,
-                                  protocols,
-                                  NULL, NULL,
-                                  dom->service_timeout,
-                                  now);
-        if (ret) {
-            /* Do not fail completely on errors.
-             * Just report the failure to save and go on */
-            DEBUG(SSSDBG_OP_FAILURE,
-                  ("Failed to store service [%s]. Ignoring.\n",
-                   strerror(ret)));
-        }
-        goto again; /* next */
+                ret = sss_get_cased_name_list(tmpctx,
+                        (const char * const *) svc->s_aliases,
+                        dom->case_sensitive, &cased_aliases);
+                if (ret != EOK) {
+                    /* Do not fail completely on errors.
+                     * Just report the failure to save and go on */
+                    DEBUG(SSSDBG_OP_FAILURE,
+                            ("Failed to store service [%s]. Ignoring.\n",
+                             strerror(ret)));
+                    again = true;
+                    break;
+                }
 
-    case NSS_STATUS_UNAVAIL:
-        /* "remote" backend unavailable. Enter offline mode */
-        ret = ENXIO;
-        break;
+                ret = sysdb_store_service(sysdb,
+                        svc->s_name,
+                        svc->s_port,
+                        cased_aliases,
+                        protocols,
+                        NULL, NULL,
+                        dom->service_timeout,
+                        now);
+                if (ret) {
+                    /* Do not fail completely on errors.
+                     * Just report the failure to save and go on */
+                    DEBUG(SSSDBG_OP_FAILURE,
+                            ("Failed to store service [%s]. Ignoring.\n",
+                             strerror(ret)));
+                }
+                again = true;
+                break;
 
-    default:
-        ret = EIO;
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              ("proxy -> getservent_r failed (%d)[%s]\n",
-               ret, strerror(ret)));
-        break;
-    }
+            case NSS_STATUS_UNAVAIL:
+                /* "remote" backend unavailable. Enter offline mode */
+                ret = ENXIO;
+                break;
+
+            default:
+                ret = EIO;
+                DEBUG(SSSDBG_CRIT_FAILURE,
+                        ("proxy -> getservent_r failed (%d)[%s]\n",
+                         ret, strerror(ret)));
+                break;
+        }
+    } while (again);
 
 done:
     talloc_zfree(tmpctx);
-- 
1.7.11.2

