git: de0b782545f7 - stable/12 - dma: restore addition of newline when missing from input

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Fri, 18 Nov 2022 20:59:49 UTC
The branch stable/12 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=de0b782545f725e719f9b3f1ec7f2e6442551086

commit de0b782545f725e719f9b3f1ec7f2e6442551086
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2022-09-27 19:19:19 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-11-18 20:57:45 +0000

    dma: restore addition of newline when missing from input
    
    If input mail does not have a newline on the last line dma must add
    one.  This was broken by the addition of long-line splitting, with the
    switch from strlen(line) to linelen returned by getline().
    
    PR:             266629
    Reviewed by:    bapt, Mikko Lehto
    Tested by:      Mikko Lehto
    MFC after:      1 week
    Fixes:          b0b2d05fd060 ("Split body of mails not respecting...")
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D36763
    
    (cherry picked from commit 169487223143b1232ec4686b720b028af8d6d42b)
    (cherry picked from commit bd3d597a6bc0e35273669cfd7a99b3a5b437d92a)
---
 contrib/dma/mail.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/contrib/dma/mail.c b/contrib/dma/mail.c
index 9641818b8814..aa8d51c28d56 100644
--- a/contrib/dma/mail.c
+++ b/contrib/dma/mail.c
@@ -35,6 +35,7 @@
 
 #include <errno.h>
 #include <inttypes.h>
+#include <malloc_np.h>
 #include <signal.h>
 #include <strings.h>
 #include <string.h>
@@ -418,8 +419,14 @@ readmail(struct queue *queue, int nodot, int recp_from_header)
 			 * If we fix it, it better be the last line of
 			 * the file.
 			 */
-			line[linelen] = '\n';
-			line[linelen + 1] = 0;
+			if ((size_t)linelen + 1 > linecap) {
+				line = realloc(line, linelen + 2);
+				if (line == NULL)
+					errlogx(EX_SOFTWARE, "realloc");
+				linecap = malloc_usable_size(line);
+			}
+			line[linelen++] = '\n';
+			line[linelen] = 0;
 			had_last_line = 1;
 		}
 		if (!had_first_line) {