This is the mail archive of the
mailing list for the GCC project.
Re: strlen optimizations based on whether stpcpy is declared?
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Martin Sebor <msebor at gmail dot com>
- Cc: Alan Modra <amodra at gmail dot com>, GCC Mailing List <gcc at gcc dot gnu dot org>
- Date: Mon, 2 Oct 2017 19:07:55 +0200
- Subject: Re: strlen optimizations based on whether stpcpy is declared?
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jakub at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BCC0EC04AC6A
- References: <firstname.lastname@example.org> <20171002071153.GR1701@tucnak> <20171002103506.GT25070@bubble.grove.modra.org> <20171002104056.GU1701@tucnak> <email@example.com> <20171002150613.GA1701@tucnak> <firstname.lastname@example.org>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Mon, Oct 02, 2017 at 10:41:59AM -0600, Martin Sebor wrote:
> I also take back what I said about application programs being
> unaffected by this. Using the declaration to make these decisions
> results in less optimal code when compiling in strict conformance
> mode (e.g., -std=c11 or -std=c++14) than in "relaxed mode" (-std=
> gnu11 or -std=gnu++14). This can be seen using the following test
Only for C, and even for -std=c99 or -std=c11 one can use -D_GNU_SOURCE
or -D_POSIX_C_SOURCE=200809 or -D_XOPEN_SOURCE=700 and various others
to make stpcpy available. For C++ we define _GNU_SOURCE (unfortunately)
> #include <string.h>
> void f (char *d, const char *s)
> strcpy (d, s);
> if (__builtin_strlen (d) != __builtin_strlen (s))
> __builtin_abort ();
> I understand this is because, as you said, in strict mode, stpcpy
> could be declared to be a different symbol. After our discussion
> I will (hopefully) remember this and avoid getting surprised by
> it in the future. But it still feels like a subtlety that should
> be more prominently advertised somehow/somewhere to help others
> avoid falling into the same trap.
Why should it be advertised? It is an optimization. We use it when
we feel it is safe to do so. It isn't something that should be documented
in user manuals IMNSHO.