Module Name:    src
Committed By:   christos
Date:           Mon May 15 16:09:09 UTC 2017

Modified Files:
        src/lib/libc/gen: assert.c

Log Message:
Don't make assert use stdio; saves >100K on an empty static binary.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/lib/libc/gen/assert.c

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

Modified files:

Index: src/lib/libc/gen/assert.c
diff -u src/lib/libc/gen/assert.c:1.17 src/lib/libc/gen/assert.c:1.18
--- src/lib/libc/gen/assert.c:1.17	Mon Jun 25 18:32:43 2012
+++ src/lib/libc/gen/assert.c	Mon May 15 12:09:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: assert.c,v 1.17 2012/06/25 22:32:43 abs Exp $	*/
+/*	$NetBSD: assert.c,v 1.18 2017/05/15 16:09:09 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)assert.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: assert.c,v 1.17 2012/06/25 22:32:43 abs Exp $");
+__RCSID("$NetBSD: assert.c,v 1.18 2017/05/15 16:09:09 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -44,19 +44,31 @@ __RCSID("$NetBSD: assert.c,v 1.17 2012/0
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <syslog.h>
+#include "extern.h"
 
-void
-__assert13(const char *file, int line, const char *function,
-    const char *failedexpr)
+static int
+fmtassert(char *buf, size_t len, const char *file, int line,
+    const char *function, const char *failedexpr)
 {
-
-	(void)fprintf(stderr,
+	return snprintf_ss(buf, len,
 	    "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
 	    failedexpr, file, line,
 	    function ? ", function \"" : "",
 	    function ? function : "",
 	    function ? "\"" : "");
+}
+
+void
+__assert13(const char *file, int line, const char *function,
+    const char *failedexpr)
+{
+	char buf[1024];
+	int l = fmtassert(buf, sizeof(buf), file, line, function, failedexpr);
+	if (l < 0)
+		abort();
+	(void)write(STDERR_FILENO, buf, (size_t)l);
 	abort();
 	/* NOTREACHED */
 }
@@ -113,16 +125,19 @@ __diagassert13(const char *file, int lin
 		}
 	}
 
-	snprintf(buf, sizeof(buf),
-	    "assertion \"%s\" failed: file \"%s\", line %d%s%s%s",
-	    failedexpr, file, line,
-	    function ? ", function \"" : "",
-	    function ? function : "",
-	    function ? "\"" : "");
-	if (diagassert_flags & DIAGASSERT_STDERR)
-		(void)fprintf(stderr, "%s: %s\n", getprogname(), buf);
-	if (diagassert_flags & DIAGASSERT_SYSLOG)
-		syslog(LOG_DEBUG | LOG_USER, "%s", buf);
+	fmtassert(buf, sizeof(buf), file, line, function, failedexpr);
+	if (diagassert_flags & DIAGASSERT_STDERR) {
+		char ebuf[1024];
+		int l = snprintf_ss(ebuf, sizeof(ebuf), "%s: %s\n",
+		    getprogname(), buf);
+		if (l == -1)
+			abort();
+		(void)write(STDERR_FILENO, ebuf, (size_t)l);
+	}
+	if (diagassert_flags & DIAGASSERT_SYSLOG) {
+		struct syslog_data sdata = SYSLOG_DATA_INIT;
+		syslog_ss(LOG_DEBUG | LOG_USER, &sdata, "%s", buf);
+	}
 	if (diagassert_flags & DIAGASSERT_ABORT)
 		abort();
 }

Reply via email to