git: fa778f0ce5ef - stable/14 - bintrans: Error out if writing to the output failed.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sat, 24 Feb 2024 12:14:59 UTC
The branch stable/14 has been updated by des:

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

commit fa778f0ce5ef2bee0ce288a7c8eef9a88ddda8be
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-02-01 13:10:31 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-02-24 12:12:49 +0000

    bintrans: Error out if writing to the output failed.
    
    - Cover all code paths.
    - When decoding, check all output files, not just the last one.
    - A simple `ferror()` check is not enough as an error may later occur
      while flushing whatever remains in the output buffer.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D43532
    
    (cherry picked from commit 5cb28f7979773715615cc2131fe40e0c5879ed1d)
    
    bintrans: Fix uninitialized variable.
    
    `prev` may be used uninitialized if `body` starts with a newline.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    bapt, emaste
    Differential Revision:  https://reviews.freebsd.org/D43534
    
    (cherry picked from commit bce34cba07bcfed9cd519a658e594c9910c8f210)
    
    bintrans: Add base64 to name list in manual page.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    0mp, pstef
    Differential Revision:  https://reviews.freebsd.org/D43558
    
    (cherry picked from commit 64028ac3ba9668cff31bfe2c79d85a3b89e10953)
    
    bintrans: Remove unused variable.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D43559
    
    (cherry picked from commit bc2913d1736c2b299a265741a779014d001bd108)
---
 usr.bin/bintrans/bintrans.1 |  9 ++++-----
 usr.bin/bintrans/qp.c       |  5 ++---
 usr.bin/bintrans/uudecode.c | 33 ++++++++++++++++++++++-----------
 usr.bin/bintrans/uuencode.c |  4 ++--
 4 files changed, 30 insertions(+), 21 deletions(-)

diff --git a/usr.bin/bintrans/bintrans.1 b/usr.bin/bintrans/bintrans.1
index e7e7f4c56cdf..16ae66aa21d1 100644
--- a/usr.bin/bintrans/bintrans.1
+++ b/usr.bin/bintrans/bintrans.1
@@ -25,9 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\"     From: @(#)uuencode.1	8.1 (Berkeley) 6/6/93
-.\"
-.Dd April 18, 2022
+.Dd January 23, 2024
 .Dt BINTRANS 1
 .Os
 .Sh NAME
@@ -35,8 +33,9 @@
 .Nm uuencode ,
 .Nm uudecode ,
 .Nm b64encode ,
-.Nm b64decode
-.Nd encode/decode a binary file
+.Nm b64decode ,
+.Nm base64
+.Nd encode / decode a binary file
 .Sh SYNOPSIS
 .Nm
 .Op algorithm
diff --git a/usr.bin/bintrans/qp.c b/usr.bin/bintrans/qp.c
index 74fa1ec3ae90..c2c9dfa7a224 100644
--- a/usr.bin/bintrans/qp.c
+++ b/usr.bin/bintrans/qp.c
@@ -90,9 +90,9 @@ decode_quoted_printable(const char *body, FILE *fpo)
 static void
 encode_quoted_printable(const char *body, FILE *fpo)
 {
-	char prev;
 	const char *end = body + strlen(body);
 	size_t linelen = 0;
+	char prev = '\0';
 
 	while (*body != '\0') {
 		if (linelen == 75) {
@@ -138,12 +138,11 @@ qp(FILE *fp, FILE *fpo, bool encode)
 {
 	char *line = NULL;
 	size_t linecap = 0;
-	ssize_t linelen;
 	void (*codec)(const char *line, FILE *f);
 
 	codec = encode ? encode_quoted_printable : decode_quoted_printable ;
 
-	while ((linelen = getline(&line, &linecap, fp)) > 0)
+	while (getline(&line, &linecap, fp) > 0)
 		codec(line, fpo);
 	free(line);
 }
diff --git a/usr.bin/bintrans/uudecode.c b/usr.bin/bintrans/uudecode.c
index b0ab415486c8..6f716b0f2ec1 100644
--- a/usr.bin/bintrans/uudecode.c
+++ b/usr.bin/bintrans/uudecode.c
@@ -343,11 +343,22 @@ checkend(const char *ptr, const char *end, const char *msg)
 		warnx("%s: %s: %s", infile, outfile, msg);
 		return (1);
 	}
-	if (fclose(outfp) != 0) {
+	return (0);
+}
+
+static int
+checkout(int rval)
+{
+	if (fflush(outfp) != 0) {
 		warn("%s: %s", infile, outfile);
-		return (1);
+		rval = 1;
 	}
-	return (0);
+	if (outfp != stdout) {
+		(void)fclose(outfp);
+		outfp = stdout;
+	}
+	outfile = "/dev/stdout";
+	return (rval);
 }
 
 static int
@@ -361,9 +372,9 @@ uu_decode(void)
 	for (;;) {
 		switch (get_line(buf, sizeof(buf))) {
 		case 0:
-			return (0);
+			return (checkout(0));
 		case 1:
-			return (1);
+			return (checkout(1));
 		}
 
 #define	DEC(c)		(((c) - ' ') & 077)	/* single character decode */
@@ -420,11 +431,11 @@ uu_decode(void)
 	}
 	switch (get_line(buf, sizeof(buf))) {
 	case 0:
-		return (0);
+		return (checkout(0));
 	case 1:
-		return (1);
+		return (checkout(1));
 	default:
-		return (checkend(buf, "end", "no \"end\" line"));
+		return (checkout(checkend(buf, "end", "no \"end\" line")));
 	}
 }
 
@@ -442,9 +453,9 @@ base64_decode(void)
 		switch (get_line(inbuf + strlen(inbuf),
 		    sizeof(inbuf) - strlen(inbuf))) {
 		case 0:
-			return (0);
+			return (checkout(0));
 		case 1:
-			return (1);
+			return (checkout(1));
 		}
 
 		count = 0;
@@ -471,7 +482,7 @@ base64_decode(void)
 			break;
 		fwrite(outbuf, 1, n, outfp);
 	}
-	return (checkend(inbuf, "====", "error decoding base64 input stream"));
+	return (checkout(checkend(inbuf, "====", "error decoding base64 input stream")));
 }
 
 static void
diff --git a/usr.bin/bintrans/uuencode.c b/usr.bin/bintrans/uuencode.c
index 267ab7deb8d1..d6a377fcabae 100644
--- a/usr.bin/bintrans/uuencode.c
+++ b/usr.bin/bintrans/uuencode.c
@@ -86,7 +86,7 @@ main_base64_encode(const char *in, const char *w)
 	if (w != NULL)
 		columns = arg_to_col(w);
 	base64_encode();
-	if (ferror(output))
+	if (fflush(output) != 0)
 		errx(1, "write error");
 	exit(0);
 }
@@ -156,7 +156,7 @@ main_encode(int argc, char *argv[])
 		base64_encode();
 	else
 		encode();
-	if (ferror(output))
+	if (fflush(output) != 0)
 		errx(1, "write error");
 	exit(0);
 }