svn commit: r366982 - head/lib/libc/gen

Warner Losh imp at FreeBSD.org
Fri Oct 23 23:56:01 UTC 2020


Author: imp
Date: Fri Oct 23 23:56:00 2020
New Revision: 366982
URL: https://svnweb.freebsd.org/changeset/base/366982

Log:
  warnx: Save errno across calls that might change it.
  
  When warn() family of functions is being used after err_set_file() has
  been set to, for example, /dev/null, errno is being clobbered,
  rendering it unreliable after, for example, procstat_getpathname()
  when it is supposed to emit a warning. Then the errno is changed to
  Inappropriate ioctl for device, destroying the original value (via
  calls to fprintf()functions).
  
  Submitted by: Juraj Lutter
  Differential Revision: https://reviews.freebsd.org/D26871

Modified:
  head/lib/libc/gen/err.c

Modified: head/lib/libc/gen/err.c
==============================================================================
--- head/lib/libc/gen/err.c	Fri Oct 23 22:27:45 2020	(r366981)
+++ head/lib/libc/gen/err.c	Fri Oct 23 23:56:00 2020	(r366982)
@@ -161,6 +161,9 @@ warnc(int code, const char *fmt, ...)
 void
 vwarnc(int code, const char *fmt, va_list ap)
 {
+	static int saved_errno;
+
+	saved_errno = errno;
 	if (err_file == NULL)
 		err_set_file(NULL);
 	fprintf(err_file, "%s: ", _getprogname());
@@ -169,6 +172,7 @@ vwarnc(int code, const char *fmt, va_list ap)
 		fprintf(err_file, ": ");
 	}
 	fprintf(err_file, "%s\n", strerror(code));
+	errno = saved_errno;
 }
 
 void
@@ -183,10 +187,14 @@ warnx(const char *fmt, ...)
 void
 vwarnx(const char *fmt, va_list ap)
 {
+	static int saved_errno;
+
+	saved_errno = errno;
 	if (err_file == NULL)
 		err_set_file(NULL);
 	fprintf(err_file, "%s: ", _getprogname());
 	if (fmt != NULL)
 		vfprintf(err_file, fmt, ap);
 	fprintf(err_file, "\n");
+	errno = saved_errno;
 }


More information about the svn-src-all mailing list