public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/106853] New: compound literal storage duration
@ 2022-09-06 14:17 marcelgcc at firemail dot eu
  2022-09-06 14:23 ` [Bug c/106853] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: marcelgcc at firemail dot eu @ 2022-09-06 14:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106853
           Summary: compound literal storage duration
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marcelgcc at firemail dot eu
  Target Milestone: ---

According to the C Standard a compound literal has automatic storage duration.
Please, consider the following code. Note it's C code NOT C++:

% cat test.c
#include <stdio.h>

struct ab {
  int a, b;
} *p1, *p2;

int set_p1(struct ab *ab) {
  p1 = ab;
  return 0;
}
int set_p2(struct ab *ab) {
  p2 = ab;
  return 0;
}

int main() {

  if (0 != set_p1(&(struct ab){42, 42})) {
    return 1;
  }
  set_p2(&(struct ab){46, 46});

  printf("%d, %d\n", p1->a, p1->b);
  printf("%d, %d\n", p2->a, p2->b);
  return 0;
}
/*EOF*/


# Expected Output:
% clang-15 -O1 test.c && ./a.out
42, 42                                        
46, 46

#####GCC#####

% gcc-11 -O1 test.c && ./a.out 
0, 0
46, 46

% gcc-11 test.c && ./a.out
42, 42
46, 46

% gcc-12 test.c && ./a.out 
46, 46
46, 46

% gcc-12 -O1 test.c && ./a.out
-760366272, 32718
46, 46

% gcc-11 --version
gcc (Debian 11.3.0-5) 11.3.0

% gcc-12 --version         
gcc (GCC) 12.2.0

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

* [Bug c/106853] compound literal storage duration
  2022-09-06 14:17 [Bug c/106853] New: compound literal storage duration marcelgcc at firemail dot eu
@ 2022-09-06 14:23 ` pinskia at gcc dot gnu.org
  2022-09-06 14:41 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-06 14:23 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---

> if (0 != set_p1(&(struct ab){42, 42}))

Yes it is auto storage and the scope is only inside the condition statement as
specified by the standard.

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

* [Bug c/106853] compound literal storage duration
  2022-09-06 14:17 [Bug c/106853] New: compound literal storage duration marcelgcc at firemail dot eu
  2022-09-06 14:23 ` [Bug c/106853] " pinskia at gcc dot gnu.org
@ 2022-09-06 14:41 ` jakub at gcc dot gnu.org
  2022-09-06 14:41 ` pinskia at gcc dot gnu.org
  2022-09-06 14:49 ` marcelgcc at firemail dot eu
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-09-06 14:41 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
In C89 that would be valid scope-wise (but C89 doesn't have compound literals
on the other side), but in C99 and later compound statements like if introduce
a new block scope.  So, accessing *p1 is valid during evaluation of the if
condition or during evaluation of the substatements, but not after the compound
statement ends.  As the testcase does that, it invokes UB and anything can
happen.

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

* [Bug c/106853] compound literal storage duration
  2022-09-06 14:17 [Bug c/106853] New: compound literal storage duration marcelgcc at firemail dot eu
  2022-09-06 14:23 ` [Bug c/106853] " pinskia at gcc dot gnu.org
  2022-09-06 14:41 ` jakub at gcc dot gnu.org
@ 2022-09-06 14:41 ` pinskia at gcc dot gnu.org
  2022-09-06 14:49 ` marcelgcc at firemail dot eu
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-06 14:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The important part of the standards:
6.5.2.5 Compound literals

5 The value of the compound literal is that of an unnamed object initialized by
the initializer list. If the compound literal occurs outside the body of a
function, the object has static storage duration; otherwise, it has automatic
storage duration associated with the enclosing block.

-----
6.8.4 Selection statements

3 A selection statement is a block whose scope is a strict subset of the scope
of its enclosing block. Each associated substatement is also a block whose
scope is a strict subset of the scope of the selection statement.

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

* [Bug c/106853] compound literal storage duration
  2022-09-06 14:17 [Bug c/106853] New: compound literal storage duration marcelgcc at firemail dot eu
                   ` (2 preceding siblings ...)
  2022-09-06 14:41 ` pinskia at gcc dot gnu.org
@ 2022-09-06 14:49 ` marcelgcc at firemail dot eu
  3 siblings, 0 replies; 5+ messages in thread
From: marcelgcc at firemail dot eu @ 2022-09-06 14:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from marcelgcc at firemail dot eu ---
Thanks for the explanation.

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

end of thread, other threads:[~2022-09-06 14:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-06 14:17 [Bug c/106853] New: compound literal storage duration marcelgcc at firemail dot eu
2022-09-06 14:23 ` [Bug c/106853] " pinskia at gcc dot gnu.org
2022-09-06 14:41 ` jakub at gcc dot gnu.org
2022-09-06 14:41 ` pinskia at gcc dot gnu.org
2022-09-06 14:49 ` marcelgcc at firemail dot eu

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).