From 01f59d7875f2a29299a60b434ca14da419b26c1a Mon Sep 17 00:00:00 2001 From: op Date: Mon, 5 Jun 2023 08:07:18 +0000 Subject: [PATCH] use getline(3) instead of fgetln(3) while here simplify the "From " check too. ok millert@ --- libexec/mail.local/mail.local.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/libexec/mail.local/mail.local.c b/libexec/mail.local/mail.local.c index 815fe58323c..7d0dfb4abb1 100644 --- a/libexec/mail.local/mail.local.c +++ b/libexec/mail.local/mail.local.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mail.local.c,v 1.41 2023/05/15 12:02:40 op Exp $ */ +/* $OpenBSD: mail.local.c,v 1.42 2023/06/05 08:07:18 op Exp $ */ /*- * Copyright (c) 1996-1998 Theo de Raadt @@ -111,9 +111,10 @@ storemail(char *from) { FILE *fp = NULL; time_t tval; - int fd, eline; - size_t len; - char *line, *tbuf; + int fd, eline = 1; + char *tbuf, *line = NULL; + size_t linesize = 0; + ssize_t linelen; if ((tbuf = strdup(_PATH_LOCTMP)) == NULL) merr(EX_OSERR, "unable to allocate memory"); @@ -125,23 +126,13 @@ storemail(char *from) (void)time(&tval); (void)fprintf(fp, "From %s %s", from, ctime(&tval)); - for (eline = 1, tbuf = NULL; (line = fgetln(stdin, &len));) { - /* We have to NUL-terminate the line since fgetln does not */ - if (line[len - 1] == '\n') - line[len - 1] = '\0'; - else { - /* No trailing newline, so alloc space and copy */ - if ((tbuf = malloc(len + 1)) == NULL) - merr(EX_OSERR, "unable to allocate memory"); - memcpy(tbuf, line, len); - tbuf[len] = '\0'; - line = tbuf; - } + while ((linelen = getline(&line, &linesize, stdin)) != -1) { + if (line[linelen - 1] == '\n') + line[linelen - 1] = '\0'; if (line[0] == '\0') eline = 1; else { - if (eline && line[0] == 'F' && len > 5 && - !memcmp(line, "From ", 5)) + if (eline && !strncmp(line, "From ", 5)) (void)putc('>', fp); eline = 0; } @@ -149,7 +140,7 @@ storemail(char *from) if (ferror(fp)) break; } - free(tbuf); + free(line); /* Output a newline; note, empty messages are allowed. */ (void)putc('\n', fp); -- 2.20.1