Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 May 2018 12:39:53 +0200
From:      Dimitry Andric <dim@FreeBSD.org>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        Thomas Munro <munro@ip9.org>, freebsd-hackers@freebsd.org
Subject:   Re: printf("%m") doesn't generate a warning -- shouldn't it?
Message-ID:  <77D988F8-ED6A-4F74-B81B-69BE4CC1D5FA@FreeBSD.org>
In-Reply-To: <20180521134306.GT6887@kib.kiev.ua>
References:  <CADLWmXU__ZRaS=gg_Y18WYbJJgk2Rs065RhkYmAZCFY8mp1Wjg@mail.gmail.com> <BAD690AA-C51A-4125-8F23-B637758C8EB6@FreeBSD.org> <20180521134306.GT6887@kib.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_5CE689C0-86AF-451E-BF75-E7749DA9771C
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

On 21 May 2018, at 15:43, Konstantin Belousov <kostikbel@gmail.com> =
wrote:
>=20
> On Mon, May 21, 2018 at 01:19:01PM +0200, Dimitry Andric wrote:
>> On 21 May 2018, at 04:38, Thomas Munro <munro@ip9.org> wrote:
>>>=20
>>> As discussed on the PostgreSQL[1] and NetBSD mailing lists[2][3],
>>> syslog-like printf("%m") is a GNU extension that doesn't generate a
>>> warning from Clang or GCC on other operating systems even though =
when
>>> it doesn't actually work.  That's because the __printf__ attribute
>>> that our __printflike macro in /usr/include/stdio.h expands to
>>> effectively means "like printf in glibc, allowing %m", not like =
POSIX
>>> or our actual libc which just prints out "m" when it sees it.
>>>=20
>>> It'd sure be nice to get a compiler warning on FreeBSD when porting
>>> software that uses that if it doesn't actually work (or ... to =
support
>>> it).
>>=20
>> Please submit upstream bug(s) for clang and gcc.  It turns out clang =
has
>> a -Wformat-non-iso warning flag which should warn about this, but I
>> can't get it to emit it:
>>=20
>> $ cat printf-m.c
>> #include <stdio.h>
>>=20
>> int main(void)
>> {
>>        printf("error: %m\n");
>>        return 0;
>> }
>>=20
>> $ clang -std=3Dc99 -Wformat-non-iso -c printf-m.c
>> <nothing>
>=20
> Why not add %m instead ?  It is very easy and several people did it in
> round-about ways.

Sure, that is certainly a nice thing to have, and let's commit it.  But
it's still an upstream bug if an explicit warning flag for non-ISO
printf specifiers doesn't work.  I'll take that discussion to the LLVM
bug tracker.


> diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c
> index 29fbd95b399..70f5074e2f7 100644
> --- a/lib/libc/stdio/vfprintf.c
> +++ b/lib/libc/stdio/vfprintf.c
> @@ -317,6 +317,7 @@ __vfprintf(FILE *fp, locale_t locale, const char =
*fmt0, va_list ap)
> 	int ret;		/* return value accumulator */
> 	int width;		/* width from format (%8d), or 0 */
> 	int prec;		/* precision from format; <0 for N/A */
> +	int saved_errno;
> 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
> 	struct grouping_state gs; /* thousands' grouping info */
>=20
> @@ -466,6 +467,7 @@ __vfprintf(FILE *fp, locale_t locale, const char =
*fmt0, va_list ap)
> 	savserr =3D fp->_flags & __SERR;
> 	fp->_flags &=3D ~__SERR;
>=20
> +	saved_errno =3D errno;
> 	convbuf =3D NULL;
> 	fmt =3D (char *)fmt0;
> 	argtable =3D NULL;
> @@ -776,6 +778,11 @@ reswitch:	switch (ch) {
> 			}
> 			break;
> #endif /* !NO_FLOATING_POINT */
> +		case 'm':
> +			cp =3D strerror(saved_errno);
> +			size =3D (prec >=3D 0) ? strnlen(cp, prec) : =
strlen(cp);
> +			sign =3D '\0';
> +			break;
> 		case 'n':
> 			/*
> 			 * Assignment-like behavior is specified if the

LGTM.

-Dimitry


--Apple-Mail=_5CE689C0-86AF-451E-BF75-E7749DA9771C
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.2

iF0EARECAB0WIQR6tGLSzjX8bUI5T82wXqMKLiCWowUCWwPzeQAKCRCwXqMKLiCW
o9/MAJ4/KUmhGqZFxC4W/KAOi0VXQlOLQwCeLmazNogPYWcaVKVSRbki7dIoChE=
=/GuJ
-----END PGP SIGNATURE-----

--Apple-Mail=_5CE689C0-86AF-451E-BF75-E7749DA9771C--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?77D988F8-ED6A-4F74-B81B-69BE4CC1D5FA>