about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2018-08-30 00:08:16 -0400
committerRich Felker <dalias@aerifal.cx>2018-08-30 00:08:16 -0400
commitcff4b910ab210e5c35f768c75c4562b3ae3e86f2 (patch)
tree0bf59a18f7bb2abd4482b317dec2ca366a0d68e5
parentcd8d8309975893736fe03e10b72de9678c5784fb (diff)
downloadmusl-cff4b910ab210e5c35f768c75c4562b3ae3e86f2.tar.gz
musl-cff4b910ab210e5c35f768c75c4562b3ae3e86f2.tar.xz
musl-cff4b910ab210e5c35f768c75c4562b3ae3e86f2.zip
prevent perror from clobbering stderr's orientation
this requirement is specified by POSIX.
-rw-r--r--src/stdio/perror.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/stdio/perror.c b/src/stdio/perror.c
index fdcb4d71..d0943f26 100644
--- a/src/stdio/perror.c
+++ b/src/stdio/perror.c
@@ -9,6 +9,11 @@ void perror(const char *msg)
 	char *errstr = strerror(errno);
 
 	FLOCK(f);
+
+	/* Save stderr's orientation and encoding rule, since perror is not
+	 * permitted to change them. */
+	void *old_locale = f->locale;
+	int old_mode = f->mode;
 	
 	if (msg && *msg) {
 		fwrite(msg, strlen(msg), 1, f);
@@ -18,5 +23,8 @@ void perror(const char *msg)
 	fwrite(errstr, strlen(errstr), 1, f);
 	fputc('\n', f);
 
+	f->mode = old_mode;
+	f->locale = old_locale;
+
 	FUNLOCK(f);
 }