public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110642] New: Undefined behavior in same constant expression is found not always
@ 2023-07-12 13:52 fchelnokov at gmail dot com
  0 siblings, 0 replies; only message in thread
From: fchelnokov at gmail dot com @ 2023-07-12 13:52 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110642
           Summary: Undefined behavior in same constant expression is
                    found not always
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

This lambda

auto f = [](int i) {
    const auto y = i;
    return [&] () {
        return y;
    };
};

returns another lambda that makes undefined behavior: it accesses a stack
variable (y) after the end of its life.

And it is successfully caught during constant expression evaluation by GCC and
other compilers, if verified like this:

constexpr auto g = f(3)();
static_assert( f(3)() == 3 );
Online demo: https://gcc.godbolt.org/z/3Y4cGMG15

But if one changes the order of lines:

static_assert( f(3)() == 3 );
constexpr auto g = f(3)();

then GCC stops detecting undefined behavior. Online demo:
https://gcc.godbolt.org/z/dojhKasPs

Could you please review why this happens?

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-12 13:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-12 13:52 [Bug c++/110642] New: Undefined behavior in same constant expression is found not always fchelnokov at gmail dot com

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