public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gccbugbjorn at fahller dot se" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/100047] New: False -Wmaybe-uninitialized on one var depending on type of other var
Date: Mon, 12 Apr 2021 13:18:49 +0000	[thread overview]
Message-ID: <bug-100047-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 100047
           Summary: False -Wmaybe-uninitialized on one var depending on
                    type of other var
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gccbugbjorn at fahller dot se
  Target Milestone: ---

With the following C++17 code, there's a warning that m_size can be used
uninitialized if m_ptr is a std::unique_ptr<>, but not if it is a raw pointer.
This only happens when some optimization is enabled. -Og is enough. The bug
also applies to gcc-10, gcc-9 and gcc-8 (have not checked older versions).

#include <cstdint>
#include <cstdlib>
#include <optional>
#include <memory>
#include <string>

#if EXPOSE_BUG
using buffer_ptr = std::unique_ptr<uint8_t>;
#else
using buffer_ptr = const uint8_t*;
#endif

const uint8_t* get(const std::unique_ptr<uint8_t>& ptr)
{
    return ptr.get();
}
const uint8_t* get(const uint8_t* ptr)
{
    return ptr;
}

class packet_buffer
{
public:
    using const_iterator = const uint8_t*;
    packet_buffer() : m_ptr(nullptr), m_size(0) {}
    packet_buffer(buffer_ptr ptr, uint16_t size)
        : m_ptr(std::move(ptr)), m_size(size)
    {
    }
    size_t size() const { return m_size; }           // <- here
    const uint8_t* data() const { return get(m_ptr); }
    const_iterator begin() const { return data(); }
    const_iterator end() const { return data() + size(); }
private:
    buffer_ptr m_ptr;
    uint16_t m_size = 0;
};


std::string func()
{
    auto f =
        [](std::optional<packet_buffer> result) {
          auto&& b = result.value();
          return std::string(b.begin(), b.end());
        };
    return f(std::nullopt);
}

Godbolt link:
https://godbolt.org/z/63z8qzcxv

             reply	other threads:[~2021-04-12 13:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-12 13:18 gccbugbjorn at fahller dot se [this message]
2021-04-12 14:06 ` [Bug c++/100047] " gccbugbjorn at fahller dot se
2021-04-12 15:48 ` [Bug tree-optimization/100047] " msebor at gcc dot gnu.org
2021-11-30 17:20 ` aldyh at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-100047-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).