public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/65106] New: C99 intialization of struct with const member through a non-const pointer
@ 2015-02-18 13:28 andrii.riabushenko at barclays dot com
  2015-02-18 14:27 ` [Bug c/65106] " joseph at codesourcery dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: andrii.riabushenko at barclays dot com @ 2015-02-18 13:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65106

            Bug ID: 65106
           Summary: C99 intialization of struct with const member through
                    a non-const pointer
           Product: gcc
           Version: 4.9.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrii.riabushenko at barclays dot com

I noticed that such C99 code is valid in Clang, but not in GCC:

#include<stdlib.h>

struct A {
  int x;
  const int y;
}

struct A* make() 
{
  struct A *ptr = malloc(sizeof(struct A));
  *ptr = (struct A) {10, 10};
}

I find this language construct rather useful. I quickly scanned the C standard
and I can't find the evidence that is actually prohibited. There is a
difference between assignment a const member (ptr->y = 10) that should be
prohibited and actual struct initialization through non-const pointer. For some
reason GCC does not distinguish these 2 cases.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/65106] C99 intialization of struct with const member through a non-const pointer
  2015-02-18 13:28 [Bug c/65106] New: C99 intialization of struct with const member through a non-const pointer andrii.riabushenko at barclays dot com
@ 2015-02-18 14:27 ` joseph at codesourcery dot com
  2015-02-19 14:51 ` andrii.riabushenko at barclays dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: joseph at codesourcery dot com @ 2015-02-18 14:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65106

--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
See the definition of "modifiable lvalue" (6.3.2.1#1): "... if it is a 
structure or union, does not have any member (including, recursively, any 
member or element of all contained aggregates or unions) with a 
const-qualified type".  (Together with 6.5.16#2 "An assignment operator 
shall have a modifiable lvalue as its left operand.".)

(It is however valid to pass arguments and return values of such types, 
notwithstanding references to assignment in such contexts; see DR#427.)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/65106] C99 intialization of struct with const member through a non-const pointer
  2015-02-18 13:28 [Bug c/65106] New: C99 intialization of struct with const member through a non-const pointer andrii.riabushenko at barclays dot com
  2015-02-18 14:27 ` [Bug c/65106] " joseph at codesourcery dot com
@ 2015-02-19 14:51 ` andrii.riabushenko at barclays dot com
  2015-02-19 15:00 ` redi at gcc dot gnu.org
  2015-10-22  9:17 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: andrii.riabushenko at barclays dot com @ 2015-02-19 14:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65106

--- Comment #2 from Andrey Riabushenko <andrii.riabushenko at barclays dot com> ---
Thanks Joseph for pointing out relevant standard sections, hence standard does
not allow for that.

Given that implicit copies of structs with constant members are happening today 
(pass as argument by value and return value) and some compilers do support it,
can we consider this as compiler extension?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/65106] C99 intialization of struct with const member through a non-const pointer
  2015-02-18 13:28 [Bug c/65106] New: C99 intialization of struct with const member through a non-const pointer andrii.riabushenko at barclays dot com
  2015-02-18 14:27 ` [Bug c/65106] " joseph at codesourcery dot com
  2015-02-19 14:51 ` andrii.riabushenko at barclays dot com
@ 2015-02-19 15:00 ` redi at gcc dot gnu.org
  2015-10-22  9:17 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2015-02-19 15:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65106

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Function arguments and return values don't modify existing objects, the copies
are new objects. In the assignment in your code you modify an existing object.
If it was allowed what would prevent you from doing:

  struct A a = { 1, 2 }
  struct A *ptr = &a;
  *ptr = (struct A) {10, 10};

Or simply:

  struct A *ptr = malloc(sizeof(struct A));
  *ptr = (struct A) {10, 10};
  *ptr = (struct A) {11, 11};

(Clang allows both of these, despite the fact they modify a const object).


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/65106] C99 intialization of struct with const member through a non-const pointer
  2015-02-18 13:28 [Bug c/65106] New: C99 intialization of struct with const member through a non-const pointer andrii.riabushenko at barclays dot com
                   ` (2 preceding siblings ...)
  2015-02-19 15:00 ` redi at gcc dot gnu.org
@ 2015-10-22  9:17 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-10-22  9:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65106

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |mpolacek at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Looks like this doesn't point to a bug in GCC.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-10-22  9:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-18 13:28 [Bug c/65106] New: C99 intialization of struct with const member through a non-const pointer andrii.riabushenko at barclays dot com
2015-02-18 14:27 ` [Bug c/65106] " joseph at codesourcery dot com
2015-02-19 14:51 ` andrii.riabushenko at barclays dot com
2015-02-19 15:00 ` redi at gcc dot gnu.org
2015-10-22  9:17 ` mpolacek at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).