This is the mail archive of the
mailing list for the GCC project.
Re: "Uninitialized array" warnings by c++ with -O2
- From: Jakub Jelinek <jakub at redhat dot com>
- To: K <kyb at online dot ru>
- Cc: gcc at gcc dot gnu dot org, gcc-help at gcc dot gnu dot org
- Date: Wed, 7 Jun 2017 16:29:07 +0200
- Subject: Re: "Uninitialized array" warnings by c++ with -O2
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jakub at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6C7B380474
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6C7B380474
- References: <firstname.lastname@example.org> <20170607092854.GR2154@tucnak> <email@example.com> <firstname.lastname@example.org> <email@example.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Wed, Jun 07, 2017 at 05:16:44PM +0300, K wrote:
> > ui16 *ptr = (ui16*)buf;
> > There's no need for any of this messing about with pointer casts, as has
> > been explained.
> Sorry, but I still can't get the idea. Cast from udp_pseudo to uint8_t
> doesn't have an aliasing problem (std 8.8) and a cast from uint8_t to ui16
> still doesn't have an aliasing problem (std 8.6), or may be I missed
This is not the right list to learn C or C++, so this should be moved
to gcc-help. The casts themselves are not the points of UB (unless the
pointer is misaligned for the pointee type you cast to), the problem is that
you have an object (tmp and its fields) that doesn't have unsigned short as
its dynamic type, nor cv-qualified version thereof, nor similar type, etc.
(see [basic.lval]/8 for details) and you are accessing that object
using a glvalue with unsigned short type. And that invokes the UB:
"If a program attempts to access the stored value of an object through a glvalue
of other than one of the following types the behavior is undefined:"
... long list of what is allowed.
Accessing it e.g. using glvalue of char, unsigned char or std::byte would be
fine, or say using ui16 __attribute__((may_alias)) type (by having
typedef ui16 ui16a __attribute__((may_alias));
ui16a *ptr = (ui16a*)buf;
... *ptr ...
or similar, or union that includes the type of tmp and ui16 as type of its