three conversions of fgetln() to getline()
authorjung <jung@openbsd.org>
Sat, 10 Oct 2015 11:42:49 +0000 (11:42 +0000)
committerjung <jung@openbsd.org>
Sat, 10 Oct 2015 11:42:49 +0000 (11:42 +0000)
ok eric sunil

usr.sbin/smtpd/delivery_filename.c
usr.sbin/smtpd/delivery_lmtp.c
usr.sbin/smtpd/util.c

index 41d8798..0b55ba2 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: delivery_filename.c,v 1.12 2015/01/20 17:37:54 deraadt Exp $  */
+/*     $OpenBSD: delivery_filename.c,v 1.13 2015/10/10 11:42:49 jung Exp $     */
 
 /*
  * Copyright (c) 2011 Gilles Chehade <gilles@poolp.org>
@@ -53,10 +53,11 @@ static void
 delivery_filename_open(struct deliver *deliver)
 {
        struct stat      sb;
-       size_t           len;
+       size_t           sz = 0;
+       ssize_t          len;
        int              fd;
        FILE            *fp;
-       char            *ln;
+       char            *ln = NULL;
        char            *msg;
        int              n;
        int              escape_from;
@@ -77,19 +78,20 @@ delivery_filename_open(struct deliver *deliver)
                error("fdopen");
 
        escape_from = 0;
-       while ((ln = fgetln(stdin, &len)) != NULL) {
+       while ((len = getline(&ln, &sz, stdin)) != -1) {
                if (ln[len - 1] == '\n')
-                       len--;
-               if (len >= 5 && memcmp(ln, "From ", 5) == 0) {
+                       ln[len - 1] = '\0';
+               if (strncmp(ln, "From ", 5) == 0) {
                        if (escape_from == 0)
                                escape_from = 1;
                        else
                                putc('>', fp);
                }
-               fprintf(fp, "%.*s\n", (int)len, ln);
+               fprintf(fp, "%s\n", ln);
                if (ferror(fp))
                        break;
        }
+       free(ln);
        if (ferror(stdin))
                error2("read error");
        putc('\n', fp);
index 162ff60..5c139fd 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: delivery_lmtp.c,v 1.7 2015/01/20 17:37:54 deraadt Exp $ */
+/* $OpenBSD: delivery_lmtp.c,v 1.8 2015/10/10 11:42:49 jung Exp $ */
 
 /*
  * Copyright (c) 2013 Ashish SHUKLA <ashish.is@lostca.se>
@@ -138,12 +138,12 @@ static void
 delivery_lmtp_open(struct deliver *deliver)
 {
         char *buffer;
-        char *lbuf;
         char lhloname[255];
         int s;
         FILE   *fp;
         enum lmtp_state state = LMTP_BANNER;
-        size_t len;
+        size_t sz;
+        ssize_t len;
 
         fp = NULL;
 
@@ -197,21 +197,15 @@ delivery_lmtp_open(struct deliver *deliver)
                 case LMTP_DATA:
                         if (buffer[0] != '3')
                                 errx(1, "DATA rejected: %s\n", buffer);
-                        lbuf = NULL;
-                        while ((buffer = fgetln(stdin, &len))) {
-                                if (buffer[len- 1] == '\n')
+                        buffer = NULL;
+                        sz = 0;
+                        while ((len = getline(&buffer, &sz, stdin)) != -1) {
+                                if (buffer[len - 1] == '\n')
                                         buffer[len - 1] = '\0';
-                                else {
-                                        if ((lbuf = malloc(len + 1)) == NULL)
-                                                err(1, NULL);
-                                        memcpy(lbuf, buffer, len);
-                                        lbuf[len] = '\0';
-                                        buffer = lbuf;
-                                }
                                 fprintf(fp, "%s%s\r\n",
                                     *buffer == '.' ? "." : "", buffer);
                         }
-                        free(lbuf);
+                        free(buffer);
                         fprintf(fp, ".\r\n");
                         state = LMTP_QUIT;
                         break;
index d51dea4..a2d9cc6 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: util.c,v 1.118 2015/10/06 06:04:46 gilles Exp $       */
+/*     $OpenBSD: util.c,v 1.119 2015/10/10 11:42:49 jung Exp $ */
 
 /*
  * Copyright (c) 2000,2001 Markus Friedl.  All rights reserved.
@@ -718,27 +718,20 @@ getmailname(char *hostname, size_t len)
 {
        struct addrinfo hints, *res = NULL;
        FILE    *fp;
-       char    *buf, *lbuf = NULL;
-       size_t   buflen;
+       char    *buf = NULL;
+       size_t   bufsz = 0;
+       ssize_t  buflen;
        int      error, ret = 0;
 
        /* First, check if we have MAILNAME_FILE */
        if ((fp = fopen(MAILNAME_FILE, "r")) == NULL)
                goto nomailname;
 
-       if ((buf = fgetln(fp, &buflen)) == NULL)
+       if ((buflen = getline(&buf, &bufsz, fp)) == -1)
                goto end;
 
-       if (buf[buflen-1] == '\n')
+       if (buf[buflen - 1] == '\n')
                buf[buflen - 1] = '\0';
-       else {
-               if ((lbuf = calloc(buflen + 1, 1)) == NULL) {
-                       log_warn("calloc");
-                       fatal("exiting");
-               }
-               memcpy(lbuf, buf, buflen);
-               buf = lbuf;
-       }
 
        if (strlcpy(hostname, buf, len) >= len)
                fprintf(stderr, MAILNAME_FILE " entry too long");
@@ -775,7 +768,7 @@ nomailname:
        ret = 1;
 
 end:
-       free(lbuf);
+       free(buf);
        if (res)
                freeaddrinfo(res);
        if (fp)