Author: des
Date: Fri Dec 23 15:05:41 2016
New Revision: 310480
URL: https://svnweb.freebsd.org/changeset/base/310480

Log:
  MFH (r309269): use malloc()ed buffers instead of stack buffers

Modified:
  stable/10/lib/libutil/gr_util.c
  stable/10/lib/libutil/pw_util.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libutil/gr_util.c
==============================================================================
--- stable/10/lib/libutil/gr_util.c     Fri Dec 23 15:02:40 2016        
(r310479)
+++ stable/10/lib/libutil/gr_util.c     Fri Dec 23 15:05:41 2016        
(r310480)
@@ -164,11 +164,12 @@ gr_tmp(int mfd)
 int
 gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
 {
-       char buf[8192], *end, *line, *p, *q, *r, t;
+       char *buf, *end, *line, *p, *q, *r, *tmp;
        struct group *fgr;
        const struct group *sgr;
-       size_t len;
+       size_t len, size;
        int eof, readlen;
+       char t;
 
        if (old_gr == NULL && gr == NULL)
                return(-1);
@@ -186,6 +187,10 @@ gr_copy(int ffd, int tfd, const struct g
        if (sgr == NULL)
                sgr = gr;
 
+       /* initialize the buffer */
+       if ((buf = malloc(size = 1024)) == NULL)
+               goto err;
+
        eof = 0;
        len = 0;
        p = q = end = buf;
@@ -199,10 +204,16 @@ gr_copy(int ffd, int tfd, const struct g
                if (q >= end) {
                        if (eof)
                                break;
-                       if ((size_t)(q - p) >= sizeof(buf)) {
-                               warnx("group line too long");
-                               errno = EINVAL; /* hack */
-                               goto err;
+                       while ((size_t)(q - p) >= size) {
+                               if ((tmp = realloc(buf, size * 2)) == NULL) {
+                                       warnx("group line too long");
+                                       goto err;
+                               }
+                               p = tmp + (p - buf);
+                               q = tmp + (q - buf);
+                               end = tmp + (end - buf);
+                               buf = tmp;
+                               size = size * 2;
                        }
                        if (p < end) {
                                q = memmove(buf, p, end -p);
@@ -210,7 +221,7 @@ gr_copy(int ffd, int tfd, const struct g
                        } else {
                                p = q = end = buf;
                        }
-                       readlen = read(ffd, end, sizeof(buf) - (end -buf));
+                       readlen = read(ffd, end, size - (end - buf));
                        if (readlen == -1)
                                goto err;
                        else
@@ -219,7 +230,7 @@ gr_copy(int ffd, int tfd, const struct g
                                break;
                        end += len;
                        len = end - buf;
-                       if (len < (ssize_t)sizeof(buf)) {
+                       if (len < size) {
                                eof = 1;
                                if (len > 0 && buf[len -1] != '\n')
                                        ++len, *end++ = '\n';
@@ -281,7 +292,7 @@ gr_copy(int ffd, int tfd, const struct g
                        if (write(tfd, q, end - q) != end - q)
                                goto err;
                        q = buf;
-                       readlen = read(ffd, buf, sizeof(buf));
+                       readlen = read(ffd, buf, size);
                        if (readlen == 0)
                                break;
                        else
@@ -303,12 +314,12 @@ gr_copy(int ffd, int tfd, const struct g
           write(tfd, "\n", 1) != 1)
                goto err;
  done:
-       if (line != NULL)
-               free(line);
+       free(line);
+       free(buf);
        return (0);
  err:
-       if (line != NULL)
-               free(line);
+       free(line);
+       free(buf);
        return (-1);
 }
 

Modified: stable/10/lib/libutil/pw_util.c
==============================================================================
--- stable/10/lib/libutil/pw_util.c     Fri Dec 23 15:02:40 2016        
(r310479)
+++ stable/10/lib/libutil/pw_util.c     Fri Dec 23 15:05:41 2016        
(r310480)
@@ -428,11 +428,12 @@ pw_make_v7(const struct passwd *pw)
 int
 pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
 {
-       char buf[8192], *end, *line, *p, *q, *r, t;
+       char *buf, *end, *line, *p, *q, *r, *tmp;
        struct passwd *fpw;
        const struct passwd *spw;
-       size_t len;
+       size_t len, size;
        int eof, readlen;
+       char t;
 
        if (old_pw == NULL && pw == NULL)
                        return (-1);
@@ -450,6 +451,10 @@ pw_copy(int ffd, int tfd, const struct p
        if (spw == NULL)
                spw = pw;
 
+       /* initialize the buffer */
+       if ((buf = malloc(size = 1024)) == NULL)
+               goto err;
+
        eof = 0;
        len = 0;
        p = q = end = buf;
@@ -463,10 +468,16 @@ pw_copy(int ffd, int tfd, const struct p
                if (q >= end) {
                        if (eof)
                                break;
-                       if ((size_t)(q - p) >= sizeof(buf)) {
-                               warnx("passwd line too long");
-                               errno = EINVAL; /* hack */
-                               goto err;
+                       while ((size_t)(q - p) >= size) {
+                               if ((tmp = realloc(buf, size * 2)) == NULL) {
+                                       warnx("passwd line too long");
+                                       goto err;
+                               }
+                               p = tmp + (p - buf);
+                               q = tmp + (q - buf);
+                               end = tmp + (end - buf);
+                               buf = tmp;
+                               size = size * 2;
                        }
                        if (p < end) {
                                q = memmove(buf, p, end - p);
@@ -474,7 +485,7 @@ pw_copy(int ffd, int tfd, const struct p
                        } else {
                                p = q = end = buf;
                        }
-                       readlen = read(ffd, end, sizeof(buf) - (end - buf));
+                       readlen = read(ffd, end, size - (end - buf));
                        if (readlen == -1)
                                goto err;
                        else
@@ -483,7 +494,7 @@ pw_copy(int ffd, int tfd, const struct p
                                break;
                        end += len;
                        len = end - buf;
-                       if (len < (ssize_t)sizeof(buf)) {
+                       if (len < size) {
                                eof = 1;
                                if (len > 0 && buf[len - 1] != '\n')
                                        ++len, *end++ = '\n';
@@ -546,7 +557,7 @@ pw_copy(int ffd, int tfd, const struct p
                        if (write(tfd, q, end - q) != end - q)
                                goto err;
                        q = buf;
-                       readlen = read(ffd, buf, sizeof(buf));
+                       readlen = read(ffd, buf, size);
                        if (readlen == 0)
                                break;
                        else
@@ -568,12 +579,12 @@ pw_copy(int ffd, int tfd, const struct p
            write(tfd, "\n", 1) != 1)
                goto err;
  done:
-       if (line != NULL)
-               free(line);
+       free(line);
+       free(buf);
        return (0);
  err:
-       if (line != NULL)
-               free(line);
+       free(line);
+       free(buf);
        return (-1);
 }
 
_______________________________________________
svn-src-stable-10@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-stable-10
To unsubscribe, send any mail to "svn-src-stable-10-unsubscr...@freebsd.org"

Reply via email to