Convert some fgetln to getline.
authorsunil <sunil@openbsd.org>
Sun, 11 Oct 2015 12:50:00 +0000 (12:50 +0000)
committersunil <sunil@openbsd.org>
Sun, 11 Oct 2015 12:50:00 +0000 (12:50 +0000)
Ok gilles@, giovanni@, millert@

usr.sbin/smtpd/table_ldap.c
usr.sbin/smtpd/table_passwd.c
usr.sbin/smtpd/table_sqlite.c
usr.sbin/smtpd/table_static.c

index 5cc48f7..cc650f3 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: table_ldap.c,v 1.14 2015/10/06 14:02:25 stsp Exp $    */
+/*     $OpenBSD: table_ldap.c,v 1.15 2015/10/11 12:50:00 sunil Exp $   */
 
 /*
  * Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
@@ -297,28 +297,18 @@ ldap_parse_attributes(struct query *query, const char *key, const char *line,
 static int
 ldap_config(void)
 {
-       size_t           flen;
+       size_t           sz = 0;
+       ssize_t          flen;
        FILE            *fp;
-       char            *key, *value, *buf, *lbuf;
+       char            *key, *value, *buf = NULL;
 
        fp = fopen(config, "r");
        if (fp == NULL)
                return (0);
 
-       lbuf = NULL;
-       while ((buf = fgetln(fp, &flen))) {
+       while ((flen = getline(&buf, &sz, fp)) != -1) {
                if (buf[flen - 1] == '\n')
                        buf[flen - 1] = '\0';
-               else {
-                       lbuf = malloc(flen + 1);
-                       if (lbuf == NULL) {
-                               log_warn("warn: table-ldap: malloc");
-                               return (0);
-                       }
-                       memcpy(lbuf, buf, flen);
-                       lbuf[flen] = '\0';
-                       buf = lbuf;
-               }
 
                key = buf;
                while (isspace((unsigned char)*key))
@@ -390,7 +380,7 @@ ldap_config(void)
                        log_warnx("warn: table-ldap: bogus entry \"%s\"", key);
        }
 
-       free(lbuf);
+       free(buf);
        fclose(fp);
        return (1);
 }
index bc15246..7cef61a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: table_passwd.c,v 1.10 2015/01/20 17:37:54 deraadt Exp $       */
+/*     $OpenBSD: table_passwd.c,v 1.11 2015/10/11 12:50:00 sunil Exp $ */
 
 /*
  * Copyright (c) 2013 Gilles Chehade <gilles@poolp.org>
@@ -88,9 +88,10 @@ static int
 table_passwd_update(void)
 {
        FILE           *fp;
-       char           *buf, *lbuf = NULL;
+       char           *buf = NULL, *p;
        char            tmp[LINE_MAX];
-       size_t          len;
+       size_t          sz = 0;
+       ssize_t         len;
        char           *line;
        struct passwd   pw;
        struct dict    *npasswd;
@@ -106,17 +107,9 @@ table_passwd_update(void)
 
        dict_init(npasswd);
 
-       while ((buf = fgetln(fp, &len))) {
+       while ((len = getline(&buf, &sz, fp)) != -1) {
                if (buf[len - 1] == '\n')
                        buf[len - 1] = '\0';
-               else {
-                       /* EOF without EOL, copy and add the NUL */
-                       if ((lbuf = malloc(len + 1)) == NULL)
-                               err(1, NULL);
-                       memcpy(lbuf, buf, len);
-                       lbuf[len] = '\0';
-                       buf = lbuf;
-               }
 
                if (strlcpy(tmp, buf, sizeof tmp) >= sizeof tmp) {
                        log_warnx("warn: table-passwd: line too long");
@@ -130,13 +123,12 @@ table_passwd_update(void)
                        err(1, NULL);
                dict_set(npasswd, pw.pw_name, line);
        }
-       free(lbuf);
        fclose(fp);
 
        /* swap passwd table and release old one*/
        if (passwd)
-               while (dict_poproot(passwd, (void**)&buf))
-                       free(buf);
+               while (dict_poproot(passwd, (void**)&p))
+                       free(p);
        passwd = npasswd;
 
        return (1);
@@ -144,12 +136,12 @@ table_passwd_update(void)
 err:
        if (fp)
                fclose(fp);
-       free(lbuf);
+       free(buf);
 
        /* release passwd table */
        if (npasswd) {
-               while (dict_poproot(npasswd, (void**)&buf))
-                       free(buf);
+               while (dict_poproot(npasswd, (void**)&p))
+                       free(p);
                free(npasswd);
        }
        return (0);
index dd9065b..f65be0e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: table_sqlite.c,v 1.16 2015/01/20 17:37:54 deraadt Exp $       */
+/*     $OpenBSD: table_sqlite.c,v 1.17 2015/10/11 12:50:00 sunil Exp $ */
 
 /*
  * Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
@@ -172,11 +172,11 @@ table_sqlite_update(void)
        sqlite3_stmt    *_stmt_fetch_source;
        char            *_query_fetch_source;
        char            *queries[SQL_MAX];
-       size_t           flen;
-       size_t           _source_refresh;
+       ssize_t          flen;
+       size_t           sz = 0, _source_refresh;
        int              _source_expire;
        FILE            *fp;
-       char            *key, *value, *buf, *lbuf, *dbpath;
+       char            *key, *value, *buf = NULL, *dbpath;
        const char      *e;
        int              i, ret;
        long long        ll;
@@ -199,20 +199,9 @@ table_sqlite_update(void)
        if (fp == NULL)
                return (0);
 
-       lbuf = NULL;
-       while ((buf = fgetln(fp, &flen))) {
+       while ((flen = getline(&buf, &sz, fp)) != -1) {
                if (buf[flen - 1] == '\n')
                        buf[flen - 1] = '\0';
-               else {
-                       lbuf = malloc(flen + 1);
-                       if (lbuf == NULL) {
-                               log_warn("warn: table-sqlite: malloc");
-                               return (0);
-                       }
-                       memcpy(lbuf, buf, flen);
-                       lbuf[flen] = '\0';
-                       buf = lbuf;
-               }
 
                key = buf;
                while (isspace((unsigned char)*key))
@@ -348,7 +337,7 @@ table_sqlite_update(void)
        free(dbpath);
        free(_query_fetch_source);
 
-       free(lbuf);
+       free(buf);
        fclose(fp);
        return (ret);
 }
index 9224a57..5ce9689 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: table_static.c,v 1.10 2015/01/20 17:37:54 deraadt Exp $       */
+/*     $OpenBSD: table_static.c,v 1.11 2015/10/11 12:50:00 sunil Exp $ */
 
 /*
  * Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
@@ -82,8 +82,9 @@ static int
 table_static_parse(struct table *t, const char *config, enum table_type type)
 {
        FILE    *fp;
-       char    *buf, *lbuf;
-       size_t   flen;
+       char    *buf = NULL;
+       size_t   sz = 0;
+       ssize_t  flen;
        char    *keyp;
        char    *valp;
        size_t   ret = 0;
@@ -92,16 +93,9 @@ table_static_parse(struct table *t, const char *config, enum table_type type)
        if (fp == NULL)
                return 0;
 
-       lbuf = NULL;
-       while ((buf = fgetln(fp, &flen))) {
+       while ((flen = getline(&buf, &sz, fp)) != -1) {
                if (buf[flen - 1] == '\n')
                        buf[flen - 1] = '\0';
-               else {
-                       lbuf = xmalloc(flen + 1, "table_config_parse");
-                       memcpy(lbuf, buf, flen);
-                       lbuf[flen] = '\0';
-                       buf = lbuf;
-               }
 
                keyp = buf;
                while (isspace((unsigned char)*keyp))
@@ -142,7 +136,7 @@ table_static_parse(struct table *t, const char *config, enum table_type type)
 
        ret = 1;
 end:
-       free(lbuf);
+       free(buf);
        fclose(fp);
        return ret;
 }