This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] fix a couple of bugs in const string folding (PR 86532)


On Sun, Jul 22, 2018 at 04:47:45PM -0600, Martin Sebor wrote:
> > No, I mean something like:
> > 
> > $ cat y.c
> > const char a[2][3] = { "1234", "xyz" };
> > char b[6];
> > 
> > int main ()
> > {
> >    __builtin_memcpy(b, a, 4);
> >    __builtin_memset(b + 4, 'a', 2);
> >    __builtin_printf("%.6s\n", b);
> > }
> > $ gcc y.c
> > y.c:1:24: warning: initializer-string for array of chars is too long
> >   const char a[2][3] = { "1234", "xyz" };
> >                          ^~~~~~
> > y.c:1:24: note: (near initialization for 'a[0]')
> > $ ./a.out
> > 1234aa
> > 
> > but expected would be "123xaa".
> 
> Hmm.  I assumed this was undefined in C but after double
> checking I'm not sure.  If it's in fact valid and the excess
> elements are required to be ignored I'll of course fix it in
> a subsequent patch.  Let me find out.

If we just warn about the initializer and treat it some way, an optimization
should not change how the initializer is treated.
The memcpy and memset themselves must be valid and they should just copy
whatever is in the initializer without optimizations.

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]