Author: asomers
Date: Tue Feb 28 22:18:05 2017
New Revision: 314425
URL: https://svnweb.freebsd.org/changeset/base/314425

Log:
  MFC r311572, r311895, r311928, r311985, r312395, r312417
  
  r311572:
  Fix file descriptor leaks in cmp(1)
  
  Also, add a few test cases
  
  Reported by:  Coverity
  CID:          271624 275338
  Reviewed by:  ngie
  MFC after:    4 weeks
  Sponsored by: Spectra Logic Corp
  Differential Revision:        https://reviews.freebsd.org/D9074
  
  r311895:
  Fix memory leaks during "tail -r" of an irregular file
  
  * Rewrite r_buf to use standard tail queues instead of a hand-rolled
    circular linked list. Free dynamic allocations when done.
  * Remove an optimization for the case where the file is a multiple of 128KB
    in size and there is a scarcity of memory.
  * Add ATF tests for "tail -r" and its variants.
  
  Reported by:  Valgrind
  Reviewed by:  ngie
  MFC after:    4 weeks
  Sponsored by: Spectra Logic Corp
  Differential Revision:        https://reviews.freebsd.org/D9067
  
  r311928:
  Fix build of usr.bin/tail with GCC
  
  Submitted by: pluknet
  Reported by:  pluknet
  MFC after:    27 days
  X-MFC-with:   311895
  Sponsored by: Spectra Logic Corp
  
  r311985:
  Fix uninitialized variable CIDs in route6d
  
  The variables in question are actually return arguments, but it's still good
  form to initialize them.
  
  Reported by:  Coverity
  CID:          979679 979680
  MFC after:    4 weeks
  Sponsored by: Spectra Logic Corp
  
  r312395:
  Fix several Coverity CIDs in devd
  
  CID 1362055, 1362054: File descriptor leaks during shutdown
  CID 1362013: Potential null-termination fail with long network device names
  CID 1362097: Uncaught exception during memory pressure
  CID 1362017, 1362016: Unchecked errors, possibly resulting in weird behavior
        if two devd instances start at the same time.
  CID 1362015:  Unchecked error that will probably never fail
  
  Reported by:  Coverity
  CID:  1362055 1362054 1362013 1362097 1362017 1362016 1362015
  MFC after:    4 weeks
  Sponsored by: Spectra Logic Corp
  
  r312417:
  Fix build of devd with GCC 4.2
  
  Reported by:  olivier
  Pointy-hat-to:        asomers
  MFC after:    27 days
  X-MFC-with:   312395
  Sponsored by: Spectra Logic Corp

Added:
  stable/10/usr.bin/cmp/tests/cmp_test2.sh
     - copied unchanged from r311572, head/usr.bin/cmp/tests/cmp_test2.sh
  stable/10/usr.bin/tail/tests/
     - copied from r311895, head/usr.bin/tail/tests/
Modified:
  stable/10/etc/mtree/BSD.tests.dist
  stable/10/sbin/devd/devd.cc
  stable/10/usr.bin/cmp/special.c
  stable/10/usr.bin/cmp/tests/Makefile
  stable/10/usr.bin/tail/Makefile
  stable/10/usr.bin/tail/reverse.c
  stable/10/usr.sbin/route6d/route6d.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/etc/mtree/BSD.tests.dist
==============================================================================
--- stable/10/etc/mtree/BSD.tests.dist  Tue Feb 28 21:47:00 2017        
(r314424)
+++ stable/10/etc/mtree/BSD.tests.dist  Tue Feb 28 22:18:05 2017        
(r314425)
@@ -608,6 +608,8 @@
             regress.multitest.out
             ..
         ..
+        tail
+        ..
         tar
         ..
         timeout

Modified: stable/10/sbin/devd/devd.cc
==============================================================================
--- stable/10/sbin/devd/devd.cc Tue Feb 28 21:47:00 2017        (r314424)
+++ stable/10/sbin/devd/devd.cc Tue Feb 28 22:18:05 2017        (r314425)
@@ -95,6 +95,7 @@ __FBSDID("$FreeBSD$");
 #include <map>
 #include <string>
 #include <list>
+#include <stdexcept>
 #include <vector>
 
 #include "devd.h"              /* C compatible definitions */
@@ -372,7 +373,7 @@ media::do_match(config &c)
        s = socket(PF_INET, SOCK_DGRAM, 0);
        if (s >= 0) {
                memset(&ifmr, 0, sizeof(ifmr));
-               strncpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
+               strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
 
                if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
                    ifmr.ifm_status & IFM_AVALID) {
@@ -856,8 +857,10 @@ create_socket(const char *name, int sock
        if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
                err(1, "bind");
        listen(fd, 4);
-       chown(name, 0, 0);      /* XXX - root.wheel */
-       chmod(name, 0666);
+       if (chown(name, 0, 0))  /* XXX - root.wheel */
+               err(1, "chown");
+       if (chmod(name, 0666))
+               err(1, "chmod");
        return (fd);
 }
 
@@ -1043,7 +1046,13 @@ event_loop(void)
                                buffer[rv] = '\0';
                                while (buffer[--rv] == '\n')
                                        buffer[rv] = '\0';
-                               process_event(buffer);
+                               try {
+                                       process_event(buffer);
+                               }
+                               catch (std::length_error e) {
+                                       devdlog(LOG_ERR, "Dropping event %s "
+                                           "due to low memory", buffer);
+                               }
                        } else if (rv < 0) {
                                if (errno != EINTR)
                                        break;
@@ -1061,6 +1070,8 @@ event_loop(void)
                if (FD_ISSET(seqpacket_fd, &fds))
                        new_client(seqpacket_fd, SOCK_SEQPACKET);
        }
+       close(seqpacket_fd);
+       close(stream_fd);
        close(fd);
 }
 
@@ -1203,7 +1214,8 @@ check_devd_enabled()
        if (val == 0) {
                warnx("Setting " SYSCTL " to 1000");
                val = 1000;
-               sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
+               if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
+                       err(1, "sysctlbyname");
        }
 }
 

Modified: stable/10/usr.bin/cmp/special.c
==============================================================================
--- stable/10/usr.bin/cmp/special.c     Tue Feb 28 21:47:00 2017        
(r314424)
+++ stable/10/usr.bin/cmp/special.c     Tue Feb 28 22:18:05 2017        
(r314425)
@@ -99,6 +99,8 @@ eof:  if (ferror(fp1))
        } else
                if (feof(fp2))
                        eofmsg(file2);
+       fclose(fp2);
+       fclose(fp1);
        if (dfound)
                exit(DIFF_EXIT);
 }

Modified: stable/10/usr.bin/cmp/tests/Makefile
==============================================================================
--- stable/10/usr.bin/cmp/tests/Makefile        Tue Feb 28 21:47:00 2017        
(r314424)
+++ stable/10/usr.bin/cmp/tests/Makefile        Tue Feb 28 22:18:05 2017        
(r314425)
@@ -2,6 +2,7 @@
 
 .include <bsd.own.mk>
 
+ATF_TESTS_SH+=         cmp_test2
 NETBSD_ATF_TESTS_SH=   cmp_test
 
 .include <netbsd-tests.test.mk>

Copied: stable/10/usr.bin/cmp/tests/cmp_test2.sh (from r311572, 
head/usr.bin/cmp/tests/cmp_test2.sh)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ stable/10/usr.bin/cmp/tests/cmp_test2.sh    Tue Feb 28 22:18:05 2017        
(r314425, copy of r311572, head/usr.bin/cmp/tests/cmp_test2.sh)
@@ -0,0 +1,67 @@
+# Copyright (c) 2017 Alan Somers
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# $FreeBSD$
+
+atf_test_case special
+special_head() {
+       atf_set "descr" "Test cmp(1)'s handling of non-regular files"
+}
+special_body() {
+       echo 0123456789abcdef > a
+       echo 0123456789abcdeg > b
+       cat a | atf_check -s exit:0 cmp a -
+       cat a | atf_check -s exit:0 cmp - a
+       cat b | atf_check -s not-exit:0 cmp a -
+       cat b | atf_check -s not-exit:0 cmp - a
+       true
+}
+
+atf_test_case symlink
+symlink_head() {
+       atf_set "descr" "Test cmp(1)'s handling of symlinks"
+}
+symlink_body() {
+       echo 0123456789abcdef > a
+       echo 0123456789abcdeg > b
+       ln -s a a.lnk
+       ln -s b b.lnk
+       ln -s a a2.lnk
+       cp a adup
+       ln -s adup adup.lnk
+       atf_check -s exit:0 cmp a a.lnk
+       atf_check -s exit:0 cmp a.lnk a
+       atf_check -s not-exit:0 -o ignore cmp a b.lnk
+       atf_check -s not-exit:0 -o ignore cmp b.lnk a
+       atf_check -s not-exit:0 -o ignore -e ignore cmp -h a a.lnk
+       atf_check -s not-exit:0 -o ignore -e ignore cmp -h a.lnk a
+       atf_check -s exit:0 cmp -h a.lnk a2.lnk
+       atf_check -s not-exit:0 -o ignore -e ignore cmp -h a.lnk adup.lnk
+}
+
+atf_init_test_cases()
+{
+       atf_add_test_case special
+       atf_add_test_case symlink
+}

Modified: stable/10/usr.bin/tail/Makefile
==============================================================================
--- stable/10/usr.bin/tail/Makefile     Tue Feb 28 21:47:00 2017        
(r314424)
+++ stable/10/usr.bin/tail/Makefile     Tue Feb 28 22:18:05 2017        
(r314425)
@@ -1,7 +1,13 @@
 # $FreeBSD$
 #      @(#)Makefile    8.1 (Berkeley) 6/6/93
 
+.include <bsd.own.mk>
+
 PROG=  tail
 SRCS=  forward.c misc.c read.c reverse.c tail.c
 
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
+
 .include <bsd.prog.mk>

Modified: stable/10/usr.bin/tail/reverse.c
==============================================================================
--- stable/10/usr.bin/tail/reverse.c    Tue Feb 28 21:47:00 2017        
(r314424)
+++ stable/10/usr.bin/tail/reverse.c    Tue Feb 28 22:18:05 2017        
(r314425)
@@ -40,6 +40,7 @@ static char sccsid[] = "@(#)reverse.c 8.
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
+#include <sys/queue.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
@@ -169,12 +170,12 @@ r_reg(FILE *fp, const char *fn, enum STY
                ierr(fn);
 }
 
-typedef struct bf {
-       struct bf *next;
-       struct bf *prev;
-       int len;
-       char *l;
-} BF;
+#define BSZ    (128 * 1024)
+typedef struct bfelem {
+       TAILQ_ENTRY(bfelem) entries;
+       size_t len;
+       char l[BSZ];
+} bfelem_t;
 
 /*
  * r_buf -- display a non-regular file in reverse order by line.
@@ -189,64 +190,44 @@ typedef struct bf {
 static void
 r_buf(FILE *fp, const char *fn)
 {
-       BF *mark, *tl, *tr;
-       int ch, len, llen;
+       struct bfelem *tl, *first = NULL;
+       size_t llen;
        char *p;
-       off_t enomem;
+       off_t enomem = 0;
+       TAILQ_HEAD(bfhead, bfelem) head;
+
+       TAILQ_INIT(&head);
+
+       while (!feof(fp)) {
+               size_t len;
 
-       tl = NULL;
-#define        BSZ     (128 * 1024)
-       for (mark = NULL, enomem = 0;;) {
                /*
                 * Allocate a new block and link it into place in a doubly
                 * linked list.  If out of memory, toss the LRU block and
                 * keep going.
                 */
-               if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
-                   (tl->l = malloc(BSZ)) == NULL) {
-                       if (!mark)
+               while ((tl = malloc(sizeof(bfelem_t))) == NULL) {
+                       first = TAILQ_FIRST(&head);
+                       if (TAILQ_EMPTY(&head))
                                err(1, "malloc");
-                       if (enomem)
-                               tl = tl->next;
-                       else {
-                               if (tl)
-                                       free(tl);
-                               tl = mark;
-                       }
-                       enomem += tl->len;
-               } else if (mark) {
-                       tl->next = mark;
-                       tl->prev = mark->prev;
-                       mark->prev->next = tl;
-                       mark->prev = tl;
-               } else {
-                       mark = tl;
-                       mark->next = mark->prev = mark;
+                       enomem += first->len;
+                       TAILQ_REMOVE(&head, first, entries);
+                       free(first);
                }
+               TAILQ_INSERT_TAIL(&head, tl, entries);
 
                /* Fill the block with input data. */
-               for (p = tl->l, len = 0;
-                   len < BSZ && (ch = getc(fp)) != EOF; ++len)
-                       *p++ = ch;
-
-               if (ferror(fp)) {
-                       ierr(fn);
-                       return;
-               }
-
-               /*
-                * If no input data for this block and we tossed some data,
-                * recover it.
-                */
-               if (!len && enomem) {
-                       enomem -= tl->len;
-                       tl = tl->prev;
-                       break;
+               len = 0;
+               while ((!feof(fp)) && len < BSZ) {
+                       p = tl->l + len;
+                       len += fread(p, 1, BSZ - len, fp);
+                       if (ferror(fp)) {
+                               ierr(fn);
+                               return;
+                       }
                }
 
                tl->len = len;
-               if (ch == EOF)
-                       break;
        }
 
        if (enomem) {
@@ -255,37 +236,46 @@ r_buf(FILE *fp, const char *fn)
        }
 
        /*
-        * Step through the blocks in the reverse order read.  The last char
-        * is special, ignore whether newline or not.
+        * Now print the lines in reverse order
+        * Outline:
+        *    Scan backward for "\n",
+        *    print forward to the end of the buffers
+        *    free any buffers that start after the "\n" just found
+        *    Loop
         */
-       for (mark = tl;;) {
-               for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
-                   --p, ++llen)
-                       if (*p == '\n') {
-                               if (llen) {
+       tl = TAILQ_LAST(&head, bfhead);
+       first = TAILQ_FIRST(&head);
+       while (tl != NULL) {
+               struct bfelem *temp;
+
+               for (p = tl->l + tl->len - 1, llen = 0; p >= tl->l;
+                   --p, ++llen) {
+                       int start = (tl == first && p == tl->l);
+
+                       if ((*p == '\n') || start) {
+                               struct bfelem *tr;
+
+                               if (start && llen)
+                                       WR(p, llen + 1);
+                               else if (llen)
                                        WR(p + 1, llen);
-                                       llen = 0;
-                               }
-                               if (tl == mark)
-                                       continue;
-                               for (tr = tl->next; tr->len; tr = tr->next) {
-                                       WR(tr->l, tr->len);
-                                       tr->len = 0;
-                                       if (tr == mark)
-                                               break;
+                               tr = TAILQ_NEXT(tl, entries);
+                               llen = 0;
+                               if (tr != NULL) {
+                                       TAILQ_FOREACH_FROM_SAFE(tr, &head,
+                                           entries, temp) {
+                                               if (tr->len)
+                                                       WR(&tr->l, tr->len);
+                                               TAILQ_REMOVE(&head, tr,
+                                                   entries);
+                                               free(tr);
+                                       }
                                }
                        }
+               }
                tl->len = llen;
-               if ((tl = tl->prev) == mark)
-                       break;
-       }
-       tl = tl->next;
-       if (tl->len) {
-               WR(tl->l, tl->len);
-               tl->len = 0;
-       }
-       while ((tl = tl->next)->len) {
-               WR(tl->l, tl->len);
-               tl->len = 0;
+               tl = TAILQ_PREV(tl, bfhead, entries);
        }
+       TAILQ_REMOVE(&head, first, entries);
+       free(first);
 }

Modified: stable/10/usr.sbin/route6d/route6d.c
==============================================================================
--- stable/10/usr.sbin/route6d/route6d.c        Tue Feb 28 21:47:00 2017        
(r314424)
+++ stable/10/usr.sbin/route6d/route6d.c        Tue Feb 28 22:18:05 2017        
(r314425)
@@ -1063,6 +1063,7 @@ sendpacket(struct sockaddr_in6 *sin6, in
        iov[0].iov_len = len;
        m.msg_iov = iov;
        m.msg_iovlen = 1;
+       m.msg_flags = 0;
        if (!idx) {
                m.msg_control = NULL;
                m.msg_controllen = 0;
@@ -1127,6 +1128,7 @@ riprecv(void)
        cm = (struct cmsghdr *)cmsgbuf;
        m.msg_control = (caddr_t)cm;
        m.msg_controllen = sizeof(cmsgbuf);
+       m.msg_flags = 0;
        if ((len = recvmsg(ripsock, &m, 0)) < 0) {
                fatal("recvmsg");
                /*NOTREACHED*/
_______________________________________________
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