Author: kevans
Date: Wed Jan  1 18:49:57 2020
New Revision: 356254
URL: https://svnweb.freebsd.org/changeset/base/356254

Log:
  inetd: final round of trivial cleanup, NFC
  
  Highlights:
  - Use MAX() for maxsock raising; small readability improvement IMO
  - malloc(3) + memset(3) -> calloc(3) where appropriate
  - stop casting the return value of malloc(3)
  - mallloc(3) -> reallocarray(3) where appropriate
  
  A future change may enter capability mode when forking for some of the
  built-in handlers.

Modified:
  head/usr.sbin/inetd/inetd.c

Modified: head/usr.sbin/inetd/inetd.c
==============================================================================
--- head/usr.sbin/inetd/inetd.c Wed Jan  1 17:32:20 2020        (r356253)
+++ head/usr.sbin/inetd/inetd.c Wed Jan  1 18:49:57 2020        (r356254)
@@ -564,10 +564,7 @@ main(int argc, char **argv)
 #ifdef SANITY_CHECK
        nsock++;
 #endif
-       if (signalpipe[0] > maxsock)
-           maxsock = signalpipe[0];
-       if (signalpipe[1] > maxsock)
-           maxsock = signalpipe[1];
+       maxsock = MAX(MAX(maxsock, signalpipe[0]), signalpipe[1]);
 
        for (;;) {
            int n, ctrl;
@@ -931,12 +928,11 @@ addchild(struct servtab *sep, pid_t pid)
                exit(EX_SOFTWARE);
        }
 #endif
-       sc = malloc(sizeof(*sc));
+       sc = calloc(1, sizeof(*sc));
        if (sc == NULL) {
-               syslog(LOG_ERR, "malloc: %m");
+               syslog(LOG_ERR, "calloc: %m");
                exit(EX_OSERR);
        }
-       memset(sc, 0, sizeof(*sc));
        sc->sc_pid = pid;
        LIST_INSERT_HEAD(&sep->se_children, sc, sc_link);
        ++sep->se_numchild;
@@ -1501,8 +1497,8 @@ enter(struct servtab *cp)
        struct servtab *sep;
        long omask;
 
-       sep = (struct servtab *)malloc(sizeof (*sep));
-       if (sep == (struct servtab *)0) {
+       sep = malloc(sizeof(*sep));
+       if (sep == NULL) {
                syslog(LOG_ERR, "malloc: %m");
                exit(EX_OSERR);
        }
@@ -1540,8 +1536,7 @@ enable(struct servtab *sep)
        nsock++;
 #endif
        FD_SET(sep->se_fd, &allsock);
-       if (sep->se_fd > maxsock)
-               maxsock = sep->se_fd;
+       maxsock = MAX(maxsock, sep->se_fd);
 }
 
 static void
@@ -1985,20 +1980,14 @@ freeconfig(struct servtab *cp)
        struct stabchild *sc;
        int i;
 
-       if (cp->se_service)
-               free(cp->se_service);
-       if (cp->se_proto)
-               free(cp->se_proto);
-       if (cp->se_user)
-               free(cp->se_user);
-       if (cp->se_group)
-               free(cp->se_group);
+       free(cp->se_service);
+       free(cp->se_proto);
+       free(cp->se_user);
+       free(cp->se_group);
 #ifdef LOGIN_CAP
-       if (cp->se_class)
-               free(cp->se_class);
+       free(cp->se_class);
 #endif
-       if (cp->se_server)
-               free(cp->se_server);
+       free(cp->se_server);
        while (!LIST_EMPTY(&cp->se_children)) {
                sc = LIST_FIRST(&cp->se_children);
                LIST_REMOVE(sc, sc_link);
@@ -2009,8 +1998,7 @@ freeconfig(struct servtab *cp)
                        free(cp->se_argv[i]);
        free_connlist(cp);
 #ifdef IPSEC
-       if (cp->se_policy)
-               free(cp->se_policy);
+       free(cp->se_policy);
 #endif
 }
 
@@ -2290,8 +2278,7 @@ cpmip(const struct servtab *sep, int ctrl)
                    strcmp(sep->se_service, chBest->ch_Service) != 0) {
                        chBest->ch_Family = sin4->sin_family;
                        chBest->ch_Addr4 = sin4->sin_addr;
-                       if (chBest->ch_Service)
-                               free(chBest->ch_Service);
+                       free(chBest->ch_Service);
                        chBest->ch_Service = strdup(sep->se_service);
                        memset(chBest->ch_Times, 0, sizeof(chBest->ch_Times));
                } 
@@ -2304,8 +2291,7 @@ cpmip(const struct servtab *sep, int ctrl)
                    strcmp(sep->se_service, chBest->ch_Service) != 0) {
                        chBest->ch_Family = sin6->sin6_family;
                        chBest->ch_Addr6 = sin6->sin6_addr;
-                       if (chBest->ch_Service)
-                               free(chBest->ch_Service);
+                       free(chBest->ch_Service);
                        chBest->ch_Service = strdup(sep->se_service);
                        memset(chBest->ch_Times, 0, sizeof(chBest->ch_Times));
                }
@@ -2398,9 +2384,10 @@ search_conn(struct servtab *sep, int ctrl)
                        syslog(LOG_ERR, "malloc: %m");
                        exit(EX_OSERR);
                }
-               conn->co_proc = malloc(sep->se_maxperip * 
sizeof(*conn->co_proc));
+               conn->co_proc = reallocarray(NULL, sep->se_maxperip,
+                   sizeof(*conn->co_proc));
                if (conn->co_proc == NULL) {
-                       syslog(LOG_ERR, "malloc: %m");
+                       syslog(LOG_ERR, "reallocarray: %m");
                        exit(EX_OSERR);
                }
                memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen);
@@ -2489,10 +2476,10 @@ resize_conn(struct servtab *sep, int maxpip)
                LIST_FOREACH(conn, &sep->se_conn[i], co_link) {
                        for (j = maxpip; j < conn->co_numchild; ++j)
                                free_proc(conn->co_proc[j]);
-                       conn->co_proc = realloc(conn->co_proc,
-                           maxpip * sizeof(*conn->co_proc));
+                       conn->co_proc = reallocarray(conn->co_proc, maxpip,
+                           sizeof(*conn->co_proc));
                        if (conn->co_proc == NULL) {
-                               syslog(LOG_ERR, "realloc: %m");
+                               syslog(LOG_ERR, "reallocarray: %m");
                                exit(EX_OSERR);
                        }
                        if (conn->co_numchild > maxpip)
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to