changeset 4566:defe875f1d89 draft

logerr: Care about fprintf return codes Abort early if an error and return the overall result if we ever need it.
author Roy Marples <roy@marples.name>
date Tue, 23 Jul 2019 14:23:11 +0100
parents a5889b07c310
children dda61c0a55f5
files src/logerr.c
diffstat 1 files changed, 41 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/logerr.c	Mon Jul 22 16:19:27 2019 +0100
+++ b/src/logerr.c	Tue Jul 23 14:23:11 2019 +0100
@@ -92,7 +92,7 @@
 
 #ifndef SMALL
 /* Write the time, syslog style. month day time - */
-static void
+static int
 logprintdate(FILE *stream)
 {
 	struct timeval tv;
@@ -101,19 +101,22 @@
 	char buf[32];
 
 	if (gettimeofday(&tv, NULL) == -1)
-		return;
+		return -1;
 
 	now = tv.tv_sec;
 	tzset();
-	localtime_r(&now, &tmnow);
-	strftime(buf, sizeof(buf), "%b %d %T ", &tmnow);
-	fprintf(stream, "%s", buf);
+	if (localtime_r(&now, &tmnow) == NULL)
+		return -1;
+	if (strftime(buf, sizeof(buf), "%b %d %T ", &tmnow) == 0)
+		return -1;
+	return fprintf(stream, "%s", buf);
 }
 #endif
 
-__printflike(3, 0) static void
+__printflike(3, 0) static int
 vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args)
 {
+	int len = 0, e;
 	va_list a;
 #ifndef SMALL
 	bool log_pid;
@@ -123,7 +126,11 @@
 
 	if ((stream == stderr && ctx->log_opts & LOGERR_ERR_DATE) ||
 	    (stream != stderr && ctx->log_opts & LOGERR_LOG_DATE))
-		logprintdate(stream);
+	{
+		if ((e = logprintdate(stream)) == -1)
+			return -1;
+		len += e;
+	}
 
 #ifdef LOGERR_TAG
 	log_tag = ((stream == stderr && ctx->log_opts & LOGERR_ERR_TAG) ||
@@ -131,29 +138,43 @@
 	if (log_tag) {
 		if (ctx->log_tag == NULL)
 			ctx->log_tag = getprogname();
-		fprintf(stream, "%s", ctx->log_tag);
+		if ((e = fprintf(stream, "%s", ctx->log_tag)) == -1)
+			return -1;
+		len += e;
 	}
 #endif
 
 	log_pid = ((stream == stderr && ctx->log_opts & LOGERR_ERR_PID) ||
 	    (stream != stderr && ctx->log_opts & LOGERR_LOG_PID));
-	if (log_pid)
-		fprintf(stream, "[%d]", getpid());
+	if (log_pid) {
+		if ((e = fprintf(stream, "[%d]", getpid())) == -1)
+			return -1;
+		len += e;
+	}
 
 #ifdef LOGERR_TAG
 	if (log_tag || log_pid)
 #else
 	if (log_pid)
 #endif
-		fprintf(stream, ": ");
+	{
+		if ((e = fprintf(stream, ": ")) == -1)
+			return -1;
+		len += e;
+	}
 #else
 	UNUSED(ctx);
 #endif
 
 	va_copy(a, args);
-	vfprintf(stream, fmt, a);
-	fputc('\n', stream);
+	e = vfprintf(stream, fmt, a);
+	if (fputc('\n', stream) == EOF)
+		e = -1;
+	else if (e != -1)
+		e++;
 	va_end(a);
+
+	return e == -1 ? -1 : len + e;
 }
 
 /*
@@ -171,30 +192,31 @@
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wmissing-format-attribute"
 #endif
-__printflike(2, 0) static void
+__printflike(2, 0) static int
 vlogmessage(int pri, const char *fmt, va_list args)
 {
 	struct logctx *ctx = &_logctx;
+	int len = 0;
 
 	if (ctx->log_opts & LOGERR_ERR &&
 	    (pri <= LOG_ERR ||
 	    (!(ctx->log_opts & LOGERR_QUIET) && pri <= LOG_INFO) ||
 	    (ctx->log_opts & LOGERR_DEBUG && pri <= LOG_DEBUG)))
-		vlogprintf_r(ctx, stderr, fmt, args);
+		len = vlogprintf_r(ctx, stderr, fmt, args);
 
 	if (!(ctx->log_opts & LOGERR_LOG))
-		return;
+		return len;
 
 #ifdef SMALL
 	vsyslog(pri, fmt, args);
 #else
 	if (ctx->log_file == NULL) {
 		vsyslog(pri, fmt, args);
-		return;
+		return len;
 	}
 	if (pri == LOG_DEBUG && !(ctx->log_opts & LOGERR_DEBUG))
-		return;
-	vlogprintf_r(ctx, ctx->log_file, fmt, args);
+		return len;
+	return vlogprintf_r(ctx, ctx->log_file, fmt, args);
 #endif
 }
 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5))