Module Name:    src
Committed By:   kre
Date:           Sun Jan 13 06:10:34 UTC 2019

Modified Files:
        src/lib/libwrap: expandm.c

Log Message:
Always return from expandm() with errno unaltered, so on the
off chance it failed, there's still the possibility that whatever
processes the result will be able to deal with the %m that would
(presumably) be left in the format string.

And as a frill, don't call strerror() until we know we are
going to use its result (still call it only once, no matter
how many %m's are in the format string).


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/lib/libwrap/expandm.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/libwrap/expandm.c
diff -u src/lib/libwrap/expandm.c:1.6 src/lib/libwrap/expandm.c:1.7
--- src/lib/libwrap/expandm.c:1.6	Sun Jan 13 01:32:51 2019
+++ src/lib/libwrap/expandm.c	Sun Jan 13 06:10:34 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: expandm.c,v 1.6 2019/01/13 01:32:51 christos Exp $	*/
+/*	$NetBSD: expandm.c,v 1.7 2019/01/13 06:10:34 kre Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: expandm.c,v 1.6 2019/01/13 01:32:51 christos Exp $");
+__RCSID("$NetBSD: expandm.c,v 1.7 2019/01/13 06:10:34 kre Exp $");
 
 #include <limits.h>
 #include <stdio.h>
@@ -48,7 +48,8 @@ __RCSID("$NetBSD: expandm.c,v 1.6 2019/0
 const char * __attribute__((__format_arg__(1)))
 expandm(const char *fmt, const char *sf, char **rbuf)
 {
-	const char *e = strerror(errno);
+	const int err = errno;
+	const char *e = NULL;
 	char *buf, *m, *nbuf;
 	const char *ptr;
 
@@ -74,6 +75,8 @@ expandm(const char *fmt, const char *sf,
                         buf = nbuf;
                 }
 
+		if (__predict_true(e == NULL && (cnt & 1) != 0))
+			e = strerror(err);
 		if (asprintf(&nbuf, "%s%.*s%s", buf ? buf : "",
 		    (int)(m - ptr), ptr, (cnt & 1) ? e : "%m") == -1)
 			goto out;
@@ -87,11 +90,13 @@ expandm(const char *fmt, const char *sf,
 	free(buf);
 	if (rbuf)
 		*rbuf = nbuf;
+	errno = err;
 	return nbuf;
 out:
 	free(buf);
 	if (rbuf)
 		*rbuf = NULL;
+	errno = err;
 	return fmt;
 }
 

Reply via email to