__vprintf(): switch from strerror() to strerror_rl()

This eliminates the use of non-thread-safe function in printf*() family,
and make the call locale-aware.  Also, it stops obliterating the
strerror() static buffer, which aligns with the POSIX requirement that
implementations must behave as if no standard-mandated functions call
strerror().

PR:	278556
Reported by:	Jonathan Gruber <jonathan.gruber.jg@gmail.com>
Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44916
This commit is contained in:
Konstantin Belousov 2024-04-23 20:10:30 +03:00
parent 92771bc00a
commit f887667694

View File

@ -312,6 +312,8 @@ __vfprintf(FILE *fp, locale_t locale, const char *fmt0, va_list ap)
int width; /* width from format (%8d), or 0 */
int prec; /* precision from format; <0 for N/A */
int saved_errno;
int error;
char errnomsg[NL_TEXTMAX];
char sign; /* sign prefix (' ', '+', '-', or \0) */
struct grouping_state gs; /* thousands' grouping info */
@ -829,7 +831,9 @@ fp_common:
break;
#endif /* !NO_FLOATING_POINT */
case 'm':
cp = strerror(saved_errno);
error = __strerror_rl(saved_errno, errnomsg,
sizeof(errnomsg), locale);
cp = error == 0 ? errnomsg : "<strerror failure>";
size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
sign = '\0';
break;