Author: asomers
Date: Tue May 30 22:02:55 2017
New Revision: 319257
URL: https://svnweb.freebsd.org/changeset/base/319257

Log:
  MFC r316501, r316523
  
  r316501:
  Fix file descriptor and memory leaks in pr(1)
  
  Also, hook NetBSD's pr test into the build, and add three more test cases.
  
  Reported by:  Coverity, Valgrind
  CID:          271650 271651 271652 271653 271654 271655 271656 271656
  CID:          271657 271658 271659 1006939 1006940 1006941 1006942 1009098
  Reviewed by:  ngie
  Sponsored by: Spectra Logic Corp
  Differential Revision:        https://reviews.freebsd.org/D9137
  
  r316523:
  Remove a duplicate fclose() that snuck into r316501
  
  Reported by:  Coverity
  CID:          1373338
  X-MFC-With:   316501
  Sponsored by: Spectra Logic Corp

Added:
  stable/10/usr.bin/pr/tests/
     - copied from r316501, head/usr.bin/pr/tests/
Modified:
  stable/10/etc/mtree/BSD.tests.dist
  stable/10/usr.bin/pr/Makefile
  stable/10/usr.bin/pr/pr.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/etc/mtree/BSD.tests.dist
==============================================================================
--- stable/10/etc/mtree/BSD.tests.dist  Tue May 30 21:58:53 2017        
(r319256)
+++ stable/10/etc/mtree/BSD.tests.dist  Tue May 30 22:02:55 2017        
(r319257)
@@ -606,6 +606,8 @@
         ..
         opensm
         ..
+        pr
+        ..
         printf
         ..
         sed

Modified: stable/10/usr.bin/pr/Makefile
==============================================================================
--- stable/10/usr.bin/pr/Makefile       Tue May 30 21:58:53 2017        
(r319256)
+++ stable/10/usr.bin/pr/Makefile       Tue May 30 22:02:55 2017        
(r319257)
@@ -1,7 +1,13 @@
 #      @(#)Makefile    8.1 (Berkeley) 6/6/93
 # $FreeBSD$
 
+.include <bsd.own.mk>
+
 PROG=  pr
 SRCS=  pr.c egetopt.c
+
+.if ${MK_TESTS} != "no"
+SUBDIR+=        tests
+.endif
 
 .include <bsd.prog.mk>

Modified: stable/10/usr.bin/pr/pr.c
==============================================================================
--- stable/10/usr.bin/pr/pr.c   Tue May 30 21:58:53 2017        (r319256)
+++ stable/10/usr.bin/pr/pr.c   Tue May 30 22:02:55 2017        (r319257)
@@ -103,7 +103,7 @@ static char schar;          /* text column separation 
characte
 static int     sflag;          /* -s option for multiple columns */
 static int     nohead;         /* do not write head and trailer */
 static int     pgwd;           /* page width with multiple col output */
-static const char *timefrmt;   /* time conversion string */
+static char    *timefrmt;      /* time conversion string */
 
 /*
  * misc globals
@@ -135,6 +135,7 @@ main(int argc, char *argv[])
                        ret_val = horzcol(argc, argv);
                else
                        ret_val = vertcol(argc, argv);
+               free(timefrmt);
        } else
                usage();
        flsh_errs();
@@ -207,6 +208,7 @@ onecol(int argc, char *argv[])
         * allocate header buffer
         */
        if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
+               free(obuf);
                mfail();
                return(1);
        }
@@ -259,7 +261,7 @@ onecol(int argc, char *argv[])
                                        break;
                                if (!linecnt && !nohead &&
                                        prhead(hbuf, fname, pagecnt))
-                                       return(1);
+                                       goto err;
 
                                /*
                                 * start of new line.
@@ -268,9 +270,9 @@ onecol(int argc, char *argv[])
                                        if (num)
                                                addnum(nbuf, num, ++lncnt);
                                        if (otln(obuf,cnt+off, &ips, &ops, mor))
-                                               return(1);
+                                               goto err;
                                } else if (otln(lbuf, cnt, &ips, &ops, mor))
-                                       return(1);
+                                       goto err;
 
                                /*
                                 * if line bigger than buffer, get more
@@ -293,7 +295,7 @@ onecol(int argc, char *argv[])
                         * fill to end of page
                         */
                        if (linecnt && prtail(lines-linecnt-lrgln, lrgln))
-                               return(1);
+                               goto err;
 
                        /*
                         * On EOF go to next file
@@ -306,8 +308,14 @@ onecol(int argc, char *argv[])
                        (void)fclose(inf);
        }
        if (eoptind < argc)
-               return(1);
+               goto err;
+       free(hbuf);
+       free(obuf);
        return(0);
+err:
+       free(hbuf);
+       free(obuf);
+       return(1);
 }
 
 /*
@@ -317,27 +325,27 @@ int
 vertcol(int argc, char *argv[])
 {
        char *ptbf;
-       char **lstdat;
+       char **lstdat = NULL;
        int i;
        int j;
        int cnt = -1;
        int pln;
-       int *indy;
+       int *indy = NULL;
        int cvc;
-       int *lindy;
+       int *lindy = NULL;
        int lncnt;
        int stp;
        int pagecnt;
        int col = colwd + 1;
        int mxlen = pgwd + offst + 1;
        int mclcnt = clcnt - 1;
-       struct vcol *vc;
+       struct vcol *vc = NULL;
        int mvc;
        int tvc;
        int cw = nmwd + 1;
        int fullcol;
-       char *buf;
-       char *hbuf;
+       char *buf = NULL;
+       char *hbuf = NULL;
        char *ohbuf;
        const char *fname;
        FILE *inf;
@@ -345,6 +353,7 @@ vertcol(int argc, char *argv[])
        int cps = 0;
        int ops = 0;
        int mor = 0;
+       int retval = 1;
 
        /*
         * allocate page buffer
@@ -359,7 +368,7 @@ vertcol(int argc, char *argv[])
         */
        if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
        ohbuf = hbuf + offst;
        if (offst)
@@ -372,7 +381,7 @@ vertcol(int argc, char *argv[])
        if ((vc =
            (struct vcol *)malloc((unsigned)mvc*sizeof(struct vcol))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
 
        /*
@@ -380,7 +389,7 @@ vertcol(int argc, char *argv[])
         */
        if ((lstdat = (char **)malloc((unsigned)lines*sizeof(char *))) == NULL){
                mfail();
-               return(1);
+               goto out;
        }
 
        /*
@@ -388,11 +397,11 @@ vertcol(int argc, char *argv[])
         */
        if ((indy = (int *)malloc((unsigned)lines*sizeof(int))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
        if ((lindy = (int *)malloc((unsigned)lines*sizeof(int))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
 
        if (nmwd)
@@ -533,12 +542,13 @@ vertcol(int argc, char *argv[])
                                 * print header
                                 */
                                if (!nohead && prhead(hbuf, fname, pagecnt))
-                                       return(1);
+                                       goto out;
                                for (i = 0; i < pln; ++i) {
                                        ips = 0;
                                        ops = 0;
-                                       if (offst&& otln(buf,offst,&ips,&ops,1))
-                                               return(1);
+                                       if (offst &&
+                                           otln(buf,offst,&ips,&ops,1))
+                                               goto out;
                                        tvc = i;
 
                                        for (j = 0; j < clcnt; ++j) {
@@ -563,7 +573,7 @@ vertcol(int argc, char *argv[])
                                                        cnt = fullcol;
                                                if (otln(vc[tvc].pt, cnt, &ips,
                                                                &ops, 1))
-                                                       return(1);
+                                                       goto out;
                                                tvc += pln;
                                                if (tvc >= cvc)
                                                        break;
@@ -572,13 +582,13 @@ vertcol(int argc, char *argv[])
                                         * terminate line
                                         */
                                        if (otln(buf, 0, &ips, &ops, 0))
-                                               return(1);
+                                               goto out;
                                }
                                /*
                                 * pad to end of page
                                 */
                                if (prtail((lines - pln), 0))
-                                       return(1);
+                                       goto out;
                                /*
                                 * done with output, go to next file
                                 */
@@ -597,7 +607,7 @@ vertcol(int argc, char *argv[])
                         * print header
                         */
                        if (pln && !nohead && prhead(hbuf, fname, pagecnt))
-                               return(1);
+                               goto out;
 
                        /*
                         * output each line
@@ -607,14 +617,14 @@ vertcol(int argc, char *argv[])
                                if ((j = lstdat[i] - ptbf) <= offst)
                                        break;
                                if (otln(ptbf, j, &ips, &ops, 0))
-                                       return(1);
+                                       goto out;
                        }
 
                        /*
                         * pad to end of page
                         */
                        if (pln && prtail((lines - pln), 0))
-                               return(1);
+                               goto out;
 
                        /*
                         * if EOF go to next file
@@ -627,8 +637,16 @@ vertcol(int argc, char *argv[])
                        (void)fclose(inf);
        }
        if (eoptind < argc)
-               return(1);
-       return(0);
+               goto out;
+       retval = 0;
+out:
+       free(lindy);
+       free(indy);
+       free(lstdat);
+       free(vc);
+       free(hbuf);
+       free(buf);
+       return(retval);
 }
 
 /*
@@ -665,6 +683,7 @@ horzcol(int argc, char *argv[])
         * page header
         */
        if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
+               free(buf);
                mfail();
                return(1);
        }
@@ -744,19 +763,19 @@ horzcol(int argc, char *argv[])
                                        break;
                                if (!i && !nohead &&
                                        prhead(hbuf, fname, pagecnt))
-                                       return(1);
+                                       goto err;
                                /*
                                 * output line
                                 */
                                if (otln(buf, j, &ips, &ops, 0))
-                                       return(1);
+                                       goto err;
                        }
 
                        /*
                         * pad to end of page
                         */
                        if (i && prtail(lines-i, 0))
-                               return(1);
+                               goto err;
 
                        /*
                         * if EOF go to next file
@@ -769,8 +788,14 @@ horzcol(int argc, char *argv[])
                        (void)fclose(inf);
        }
        if (eoptind < argc)
-               return(1);
+               goto err;
+       free(hbuf);
+       free(buf);
        return(0);
+err:
+       free(hbuf);
+       free(buf);
+       return(1);
 }
 
 /*
@@ -786,27 +811,28 @@ mulfile(int argc, char *argv[])
        int cnt;
        char *lstdat;
        int i;
-       FILE **fbuf;
+       FILE **fbuf = NULL;
        int actf;
        int lncnt;
        int col;
        int pagecnt;
        int fproc;
-       char *buf;
-       char *hbuf;
+       char *buf = NULL;
+       char *hbuf = NULL;
        char *ohbuf;
        const char *fname;
        int ips = 0;
        int cps = 0;
        int ops = 0;
        int mor = 0;
+       int retval = 1;
 
        /*
         * array of FILE *, one for each operand
         */
        if ((fbuf = (FILE **)malloc((unsigned)clcnt*sizeof(FILE *))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
 
        /*
@@ -814,7 +840,7 @@ mulfile(int argc, char *argv[])
         */
        if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
        ohbuf = hbuf + offst;
 
@@ -838,7 +864,7 @@ mulfile(int argc, char *argv[])
         * if no files, exit
         */
        if (!j)
-               return(1);
+               goto out;
 
        /*
         * calculate page boundaries based on open file count
@@ -854,7 +880,7 @@ mulfile(int argc, char *argv[])
        if (colwd < 1) {
                (void)fprintf(err,
                  "pr: page width too small for %d columns\n", clcnt);
-               return(1);
+               goto out;
        }
        actf = clcnt;
        col = colwd + 1;
@@ -864,7 +890,7 @@ mulfile(int argc, char *argv[])
         */
        if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) {
                mfail();
-               return(1);
+               goto out;
        }
        if (offst) {
                (void)memset(buf, (int)' ', offst);
@@ -951,13 +977,13 @@ mulfile(int argc, char *argv[])
                                break;
 
                        if (!i && !nohead && prhead(hbuf, fname, pagecnt))
-                               return(1);
+                               goto out;
 
                        /*
                         * output line
                         */
                        if (otln(buf, j, &ips, &ops, 0))
-                               return(1);
+                               goto out;
 
                        /*
                         * if no more active files, done
@@ -972,12 +998,17 @@ mulfile(int argc, char *argv[])
                 * pad to end of page
                 */
                if (i && prtail(lines-i, 0))
-                       return(1);
+                       goto out;
                ++pagecnt;
        }
        if (eoptind < argc)
-               return(1);
-       return(0);
+               goto out;
+       retval = 0;
+out:
+       free(buf);
+       free(hbuf);
+       free(fbuf);
+       return(retval);
 }
 
 /*
@@ -1344,6 +1375,7 @@ nxtfile(int argc, char **argv, const char **fname, cha
                                        (void)fprintf(err,
                                             "pr: cannot get time of day, %s\n",
                                             strerror(errno));
+                                       fclose(inf);
                                        return(NULL);
                                }
                                timeptr = localtime(&tv_sec);
@@ -1725,7 +1757,9 @@ setup(int argc, char *argv[])
                        break;
                case 'w':
                        ++wflag;
-                       if (!isdigit((unsigned char)*eoptarg) || ((pgwd = 
atoi(eoptarg)) < 1)){
+                       if ((eoptarg == NULL ) ||
+                           !isdigit((unsigned char)*eoptarg) ||
+                           ((pgwd = atoi(eoptarg)) < 1)){
                                (void)fputs(
                                   "pr: -w width must be 1 or more \n",err);
                                return(1);
_______________________________________________
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