public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2
@ 2020-05-13 12:10 sbergman at redhat dot com
  2020-05-14  6:14 ` [Bug c++/95103] " rguenth at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: sbergman at redhat dot com @ 2020-05-13 12:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95103
           Summary: Unexpected -Wclobbered in bits/vector.tcc with -O2
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sbergman at redhat dot com
  Target Milestone: ---

I have seen this with at least some GCC 7, and still see it with GCC 10 and
with recent trunk:

> $ cat test.cc
> #include <csetjmp>
> #include <vector>
> struct S {
>     S(int);
>     ~S();
> };
> void f1();
> bool f2(char const (& s)[3]) {
>     for (int i = 0; i != 2; ++i) {
>         if (s[i] != 'x') {
>             return false;
>         }
>     }
>     return true;
> }
> void f3() {
>     std::vector<S> v;
>     for (int i = 0; i != 2; ++i) {
>         if (!f2("xx")) f1();
>         v.push_back(0);
>     }
>     std::jmp_buf b;
>     setjmp(b);
> }

> $ g++ -Wclobbered -O2 -c test.cc
> In file included from /usr/include/c++/10/vector:72,
>                  from test.cc:2:
> /usr/include/c++/10/bits/vector.tcc: In function ‘void f3()’:
> /usr/include/c++/10/bits/vector.tcc:441:15: warning: variable ‘__new_finish’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
>   441 |       pointer __new_finish(__new_start);
>       |               ^~~~~~~~~~~~

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

* [Bug c++/95103] Unexpected -Wclobbered in bits/vector.tcc with -O2
  2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
@ 2020-05-14  6:14 ` rguenth at gcc dot gnu.org
  2020-05-14  6:36 ` sbergman at redhat dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-05-14  6:14 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|unknown                     |10.1.0
           Keywords|                            |diagnostic

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Likely because of the std::vector<S> DTOR invocation which has to access
'v' which is not declared volatile but still "live" across the setjmp.

Does it work placing the initial part of the function in a separate { }?

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

* [Bug c++/95103] Unexpected -Wclobbered in bits/vector.tcc with -O2
  2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
  2020-05-14  6:14 ` [Bug c++/95103] " rguenth at gcc dot gnu.org
@ 2020-05-14  6:36 ` sbergman at redhat dot com
  2020-05-14  9:33 ` amonakov at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sbergman at redhat dot com @ 2020-05-14  6:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Stephan Bergmann <sbergman at redhat dot com> ---
(In reply to Richard Biener from comment #1)
> Does it work placing the initial part of the function in a separate { }?

Yes,

> @@ -14,11 +14,13 @@
>      return true;
>  }
>  void f3() {
> +    {
>      std::vector<S> v;
>      for (int i = 0; i != 2; ++i) {
>          if (!f2("xx")) f1();
>          v.push_back(0);
>      }
> +    }
>      std::jmp_buf b;
>      setjmp(b);
>  }

stops the warning for the reproducer.

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

* [Bug c++/95103] Unexpected -Wclobbered in bits/vector.tcc with -O2
  2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
  2020-05-14  6:14 ` [Bug c++/95103] " rguenth at gcc dot gnu.org
  2020-05-14  6:36 ` sbergman at redhat dot com
@ 2020-05-14  9:33 ` amonakov at gcc dot gnu.org
  2020-05-14 10:22 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: amonakov at gcc dot gnu.org @ 2020-05-14  9:33 UTC (permalink / raw)
  To: gcc-bugs

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

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

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

--- Comment #3 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
Richard's explanation in comment #1 is correct. The compiler assumes any
external call in the destructor can transfer control back to setjmp.

In principle in this case the warning is avoidable by observing that jmp_buf is
local and does not escape, but for any other returns_twice function the problem
would remain, as there's no jmp_buf-like key to track (think vfork).

(iow: solving this would need special-casing warning code for setjmp, which
currently works the same for all functions with the returns_twice attribute)

Let's close this?

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

* [Bug c++/95103] Unexpected -Wclobbered in bits/vector.tcc with -O2
  2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
                   ` (2 preceding siblings ...)
  2020-05-14  9:33 ` amonakov at gcc dot gnu.org
@ 2020-05-14 10:22 ` redi at gcc dot gnu.org
  2020-05-14 10:35 ` amonakov at gcc dot gnu.org
  2021-07-22 22:25 ` [Bug middle-end/95103] " pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-14 10:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The content of the warning isn't very helpful, but I think it's pointing out a
real issue in the code, not a false positive.

Any valid longjmp which followed that setjmp would have undefined behaviour if
executed, because 'v' has a non-trivial destructor.

The current wording is unclear, but CWG 2361 should clarify it, and LWG 1265
(closed as NAD) suggested different wording which would re-formulate the
function as:

void f3() try {
    std::vector<S> v;
    for (int i = 0; i != 2; ++i) {
        if (!f2("xx")) f1();
        v.push_back(0);
    }
    // ...
} catch (...) {
}

An exception thrown from the "..." part would cause the destruction of 'v' and
since that destructor is non-trivial, it would have undefined behaviour.

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

* [Bug c++/95103] Unexpected -Wclobbered in bits/vector.tcc with -O2
  2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
                   ` (3 preceding siblings ...)
  2020-05-14 10:22 ` redi at gcc dot gnu.org
@ 2020-05-14 10:35 ` amonakov at gcc dot gnu.org
  2021-07-22 22:25 ` [Bug middle-end/95103] " pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: amonakov at gcc dot gnu.org @ 2020-05-14 10:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
No, this analogy does not work. setjmp both sets up a buffer and receives
control, so it corresponds to both try and catch together. A matching "C++"
code would look like:

> void f3() {
>     std::vector<S> v;
>     for (int i = 0; i != 2; ++i) {
>         if (!f2("xx")) f1();
>         v.push_back(0);
>     }
>     try {
>     catch (...) {
>     }
> }

where it's evident that v does not leave scope and its desctructor cannot be
reached.

(comment #1 and #3 still stand)

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

* [Bug middle-end/95103] Unexpected -Wclobbered in bits/vector.tcc with -O2
  2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
                   ` (4 preceding siblings ...)
  2020-05-14 10:35 ` amonakov at gcc dot gnu.org
@ 2021-07-22 22:25 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-22 22:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-07-22
          Component|c++                         |middle-end
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There was a patch series to move -Wclobbered to gimple but it never went in.
https://gcc.gnu.org/pipermail/gcc-patches/2019-October/530992.html


Confirmed, this should say the variable is from inlined functions even.

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

end of thread, other threads:[~2021-07-22 22:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-13 12:10 [Bug c++/95103] New: Unexpected -Wclobbered in bits/vector.tcc with -O2 sbergman at redhat dot com
2020-05-14  6:14 ` [Bug c++/95103] " rguenth at gcc dot gnu.org
2020-05-14  6:36 ` sbergman at redhat dot com
2020-05-14  9:33 ` amonakov at gcc dot gnu.org
2020-05-14 10:22 ` redi at gcc dot gnu.org
2020-05-14 10:35 ` amonakov at gcc dot gnu.org
2021-07-22 22:25 ` [Bug middle-end/95103] " pinskia 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).