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)
This commit is contained in:
Dag-Erling Smørgrav 2024-02-01 14:10:31 +01:00
parent e705ac7788
commit fa778f0ce5
4 changed files with 31 additions and 22 deletions

View file

@ -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

View file

@ -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);
}

View file

@ -343,13 +343,24 @@ checkend(const char *ptr, const char *end, const char *msg)
warnx("%s: %s: %s", infile, outfile, msg);
return (1);
}
if (fclose(outfp) != 0) {
warn("%s: %s", infile, outfile);
return (1);
}
return (0);
}
static int
checkout(int rval)
{
if (fflush(outfp) != 0) {
warn("%s: %s", infile, outfile);
rval = 1;
}
if (outfp != stdout) {
(void)fclose(outfp);
outfp = stdout;
}
outfile = "/dev/stdout";
return (rval);
}
static int
uu_decode(void)
{
@ -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

View file

@ -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);
}