commit: 1218b73c00641407836ed951fda142f8783d2022
parent cff503f0a83af7a3ba4d6c26cdb5922720ddeabc
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 10 Sep 2024 09:44:47 +0200
cmd/base64: close stdin/stdout before returning successfully
Inspired by https://blog.sunfishcode.online/errors-from-close/
Diffstat:
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/cmd/base64.c b/cmd/base64.c
@@ -7,9 +7,8 @@
 #include <assert.h> /* assert */
 #include <ctype.h>  /* isspace */
 #include <errno.h>  /* errno */
-#include <fcntl.h>  /* open(), O_RDONLY */
 #include <stdint.h> /* uint8_t */
-#include <stdio.h>  /* fprintf(), BUFSIZ */
+#include <stdio.h>  /* fopen(), fprintf(), BUFSIZ */
 #include <stdlib.h> /* abort */
 #include <string.h> /* strerror(), strncmp() */
 #include <unistd.h> /* read(), write(), close(), getopt(), opt* */
@@ -348,5 +347,18 @@ end:
 	{
 		printf("\n");
 	}
+
+	if(fclose(stdin) != 0)
+	{
+		fprintf(stderr, "base64: Error closing <stdin>: %s\n", strerror(errno));
+		ret = 1;
+	}
+
+	if(fclose(stdout) != 0)
+	{
+		fprintf(stderr, "base64: Error closing <stdout>: %s\n", strerror(errno));
+		ret = 1;
+	}
+
 	return ret;
 }