From cec674795b1eb2cb7467c6031e67b2d3611ded9d Mon Sep 17 00:00:00 2001 From: sunil Date: Sun, 11 Oct 2015 12:09:06 +0000 Subject: [PATCH] Convert some fgetln to getline. Suggestion and ok millert@, ok gilles@, eric@ --- usr.sbin/smtpd/mda.c | 55 ++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/usr.sbin/smtpd/mda.c b/usr.sbin/smtpd/mda.c index 91d6968ab67..0ea145f087d 100644 --- a/usr.sbin/smtpd/mda.c +++ b/usr.sbin/smtpd/mda.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mda.c,v 1.109 2015/01/20 17:37:54 deraadt Exp $ */ +/* $OpenBSD: mda.c,v 1.110 2015/10/11 12:09:06 sunil Exp $ */ /* * Copyright (c) 2008 Gilles Chehade @@ -470,8 +470,9 @@ static void mda_io(struct io *io, int evt) { struct mda_session *s = io->arg; - char *ln; - size_t len; + char *ln = NULL; + size_t sz = 0; + ssize_t len; log_trace(TRACE_IO, "mda: %p: %s %s", s, io_strevent(evt), io_strio(io)); @@ -490,7 +491,7 @@ mda_io(struct io *io, int evt) } while (iobuf_queued(&s->iobuf) < MDA_HIWAT) { - if ((ln = fgetln(s->datafp, &len)) == NULL) + if ((len = getline(&ln, &sz, s->datafp)) == -1) break; if (iobuf_queue(&s->iobuf, ln, len) == -1) { m_create(p_parent, IMSG_MDA_KILL, @@ -499,6 +500,7 @@ mda_io(struct io *io, int evt) m_add_string(p_parent, "Out of memory"); m_close(p_parent); io_pause(io, IO_PAUSE_OUT); + free(ln); return; } #if 0 @@ -508,6 +510,7 @@ mda_io(struct io *io, int evt) #endif } + free(ln); if (ferror(s->datafp)) { log_debug("debug: mda: ferror on session %016"PRIx64, s->id); @@ -558,21 +561,14 @@ mda_io(struct io *io, int evt) static int mda_check_loop(FILE *fp, struct mda_envelope *e) { - char *buf, *lbuf; - size_t len; + char *buf = NULL; + size_t sz = 0; + ssize_t len; int ret = 0; - lbuf = NULL; - 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 */ - lbuf = xmalloc(len + 1, "mda_check_loop"); - memcpy(lbuf, buf, len); - lbuf[len] = '\0'; - buf = lbuf; - } if (strchr(buf, ':') == NULL && !isspace((unsigned char)*buf)) break; @@ -583,16 +579,10 @@ mda_check_loop(FILE *fp, struct mda_envelope *e) break; } } - if (lbuf) { - free(lbuf); - lbuf = NULL; - } } - if (lbuf) - free(lbuf); + free(buf); fseek(fp, SEEK_SET, 0); - return (ret); } @@ -600,10 +590,10 @@ static int mda_getlastline(int fd, char *dst, size_t dstsz) { FILE *fp; - char *ln, buf[LINE_MAX]; - size_t len; + char *ln = NULL; + size_t sz = 0; + ssize_t len; - memset(buf, 0, sizeof buf); if (lseek(fd, 0, SEEK_SET) < 0) { log_warn("warn: mda: lseek"); close(fd); @@ -615,24 +605,19 @@ mda_getlastline(int fd, char *dst, size_t dstsz) close(fd); return (-1); } - while ((ln = fgetln(fp, &len))) { + while ((len = getline(&ln, &sz, fp)) != -1) { if (ln[len - 1] == '\n') - len--; - if (len == 0) - continue; - if (len >= sizeof buf) - len = (sizeof buf) - 1; - memmove(buf, ln, len); - buf[len] = '\0'; + ln[len - 1] = '\0'; } fclose(fp); - if (buf[0]) { + if (sz != 0) { (void)strlcpy(dst, "\"", dstsz); - (void)strnvis(dst + 1, buf, dstsz - 2, VIS_SAFE | VIS_CSTYLE); + (void)strnvis(dst + 1, ln, dstsz - 2, VIS_SAFE | VIS_CSTYLE); (void)strlcat(dst, "\"", dstsz); } + free(ln); return (0); } -- 2.20.1