Module Name:    src
Committed By:   rillig
Date:           Thu Dec 12 05:51:50 UTC 2024

Modified Files:
        src/usr.bin/indent: args.c debug.c indent.c io.c

Log Message:
indent: add error handling for I/O errors

Suggested by lint2.


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 src/usr.bin/indent/args.c
cvs rdiff -u -r1.70 -r1.71 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.390 -r1.391 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.235 -r1.236 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/args.c
diff -u src/usr.bin/indent/args.c:1.87 src/usr.bin/indent/args.c:1.88
--- src/usr.bin/indent/args.c:1.87	Sun Dec 10 17:45:35 2023
+++ src/usr.bin/indent/args.c	Thu Dec 12 05:51:50 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: args.c,v 1.87 2023/12/10 17:45:35 rillig Exp $	*/
+/*	$NetBSD: args.c,v 1.88 2024/12/12 05:51:50 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: args.c,v 1.87 2023/12/10 17:45:35 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.88 2024/12/12 05:51:50 rillig Exp $");
 
 /* Read options from profile files and from the command line. */
 
@@ -130,7 +130,7 @@ add_typedefs_from_file(const char *fname
 	char line[BUFSIZ];
 
 	if ((file = fopen(fname, "r")) == NULL) {
-		fprintf(stderr, "indent: cannot open file %s\n", fname);
+		(void)fprintf(stderr, "indent: cannot open file %s\n", fname);
 		exit(1);
 	}
 	while ((fgets(line, sizeof(line), file)) != NULL) {
@@ -292,7 +292,7 @@ load_profile(const char *fname, bool mus
 		if (n > 0) {
 			buf[n] = '\0';
 			if (opt.verbose)
-				fprintf(stderr, "profile: %s\n", buf);
+				(void)fprintf(stderr, "profile: %s\n", buf);
 			if (buf[0] != '-')
 				errx(1,
 				    "%s: option \"%s\" must start with '-'",

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.70 src/usr.bin/indent/debug.c:1.71
--- src/usr.bin/indent/debug.c:1.70	Tue Jun 27 04:41:23 2023
+++ src/usr.bin/indent/debug.c	Thu Dec 12 05:51:50 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.70 2023/06/27 04:41:23 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.71 2024/12/12 05:51:50 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.70 2023/06/27 04:41:23 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.71 2024/12/12 05:51:50 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -162,11 +162,11 @@ debug_printf(const char *fmt, ...)
 	va_list ap;
 
 	if (state.heading != NULL) {
-		fprintf(f, "%s\n", state.heading);
+		(void)fprintf(f, "%s\n", state.heading);
 		state.heading = NULL;
 	}
 	va_start(ap, fmt);
-	vfprintf(f, fmt, ap);
+	(void)vfprintf(f, fmt, ap);
 	va_end(ap);
 	state.wrote_newlines = 0;
 }
@@ -178,14 +178,14 @@ debug_println(const char *fmt, ...)
 	va_list ap;
 
 	if (state.heading != NULL) {
-		fprintf(f, "%s\n", state.heading);
+		(void)fprintf(f, "%s\n", state.heading);
 		state.heading = NULL;
 		state.wrote_newlines = 1;
 	}
 	va_start(ap, fmt);
-	vfprintf(f, fmt, ap);
+	(void)vfprintf(f, fmt, ap);
 	va_end(ap);
-	fprintf(f, "\n");
+	(void)fprintf(f, "\n");
 	state.wrote_newlines = fmt[0] == '\0' ? state.wrote_newlines + 1 : 1;
 }
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.390 src/usr.bin/indent/indent.c:1.391
--- src/usr.bin/indent/indent.c:1.390	Sun Dec  3 21:44:42 2023
+++ src/usr.bin/indent/indent.c	Thu Dec 12 05:51:50 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.390 2023/12/03 21:44:42 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.391 2024/12/12 05:51:50 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.390 2023/12/03 21:44:42 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.391 2024/12/12 05:51:50 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -155,10 +155,10 @@ diag(int level, const char *msg, ...)
 		found_err = true;
 
 	va_start(ap, msg);
-	fprintf(stderr, "%s: %s:%d: ",
+	(void)fprintf(stderr, "%s: %s:%d: ",
 	    level == 0 ? "warning" : "error", in_name, in.token_start_line);
-	vfprintf(stderr, msg, ap);
-	fprintf(stderr, "\n");
+	(void)vfprintf(stderr, msg, ap);
+	(void)fprintf(stderr, "\n");
 	va_end(ap);
 }
 
@@ -243,7 +243,7 @@ copy_to_bak_file(void)
 	/* now the original input file will be the output */
 	output = fopen(in_name, "w");
 	if (output == NULL) {
-		remove(backup_name);
+		(void)remove(backup_name);
 		err(1, "%s", in_name);
 	}
 }
@@ -1103,7 +1103,7 @@ indent(void)
 {
 	debug_parser_state();
 
-	for (;;) {		/* loop until we reach eof */
+	for (;;) {
 		lexer_symbol lsym = lexi();
 
 		debug_blank_line();

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.235 src/usr.bin/indent/io.c:1.236
--- src/usr.bin/indent/io.c:1.235	Sun Dec  3 21:44:42 2023
+++ src/usr.bin/indent/io.c	Thu Dec 12 05:51:50 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.235 2023/12/03 21:44:42 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.236 2024/12/12 05:51:50 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,8 +38,9 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.235 2023/12/03 21:44:42 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.236 2024/12/12 05:51:50 rillig Exp $");
 
+#include <err.h>
 #include <stdio.h>
 
 #include "indent.h"
@@ -122,7 +123,8 @@ static void
 write_buffered_newlines(void)
 {
 	for (; buffered_newlines > 0; buffered_newlines--) {
-		fputc('\n', output);
+		if (fputc('\n', output) == EOF)
+			err(1, "cannot write output");
 		debug_println("write_newline");
 	}
 }
@@ -131,7 +133,8 @@ static void
 write_range(const char *s, size_t len)
 {
 	write_buffered_newlines();
-	fwrite(s, 1, len, output);
+	if (fwrite(s, 1, len, output) != len)
+		err(1, "cannot write output");
 	debug_printf("write_range ");
 	debug_vis_range(s, len);
 	debug_println("");
@@ -152,13 +155,15 @@ write_indent(int new_ind)
 		if (n > 0) {
 			ind = ind - ind % opt.tabsize + n * opt.tabsize;
 			while (n-- > 0)
-				fputc('\t', output);
+				if (fputc('\t', output) == EOF)
+					err(1, "cannot write output");
 			newlines = 0;
 		}
 	}
 
 	for (; ind < new_ind; ind++) {
-		fputc(' ', output);
+		if (fputc(' ', output) == EOF)
+			err(1, "cannot write output");
 		newlines = 0;
 	}
 
@@ -447,5 +452,6 @@ finish_output(void)
 		indent_enabled = indent_last_off_line;
 		output_line();
 	}
-	fflush(output);
+	if (fflush(output) != 0)
+		err(1, "output file");
 }

Reply via email to