From 2b86a358d00130ba3de0ad0ae56e76595ba7538d Mon Sep 17 00:00:00 2001 From: jung Date: Sat, 10 Oct 2015 11:42:49 +0000 Subject: [PATCH] three conversions of fgetln() to getline() ok eric sunil --- usr.sbin/smtpd/delivery_filename.c | 16 +++++++++------- usr.sbin/smtpd/delivery_lmtp.c | 22 ++++++++-------------- usr.sbin/smtpd/util.c | 21 +++++++-------------- 3 files changed, 24 insertions(+), 35 deletions(-) diff --git a/usr.sbin/smtpd/delivery_filename.c b/usr.sbin/smtpd/delivery_filename.c index 41d879869df..0b55ba2d00b 100644 --- a/usr.sbin/smtpd/delivery_filename.c +++ b/usr.sbin/smtpd/delivery_filename.c @@ -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 @@ -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); diff --git a/usr.sbin/smtpd/delivery_lmtp.c b/usr.sbin/smtpd/delivery_lmtp.c index 162ff6018c3..5c139fdeb3d 100644 --- a/usr.sbin/smtpd/delivery_lmtp.c +++ b/usr.sbin/smtpd/delivery_lmtp.c @@ -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 @@ -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; diff --git a/usr.sbin/smtpd/util.c b/usr.sbin/smtpd/util.c index d51dea43105..a2d9cc6ce2e 100644 --- a/usr.sbin/smtpd/util.c +++ b/usr.sbin/smtpd/util.c @@ -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) -- 2.20.1