public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jincikang at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/109448] New: _M_exception_object’ may be used uninitialized [-Werror=maybe-uninitialized]
Date: Sat, 08 Apr 2023 05:10:41 +0000	[thread overview]
Message-ID: <bug-109448-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109448
           Summary: _M_exception_object’ may be used uninitialized
                    [-Werror=maybe-uninitialized]
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jincikang at gmail dot com
  Target Milestone: ---

```cpp
// test.cpp
#include <variant>
#include <vector>
#include <cassert>
#include <cstdint>
#include <exception>
#include <atomic>

template <typename T>
class Try {
public:
    Try() = default;
    ~Try() = default;

    Try(Try<T>&& other) = default;

    Try& operator=(Try<T>&& other) = default;
    Try& operator=(std::exception_ptr error) {
        if (std::holds_alternative<std::exception_ptr>(_value) &&
            std::get<std::exception_ptr>(_value) == error) {
            return *this;
        }

        _value.template emplace<std::exception_ptr>(error);
        return *this;
    }

    template <class... U>
    Try(U&&... value)
        requires std::is_constructible_v<T, U...>
        : _value(std::in_place_type<T>, std::forward<U>(value)...) {}

    Try(std::exception_ptr error) : _value(error) {}

private:
    Try(const Try&) = delete;
    Try& operator=(const Try&) = delete;

public:
    constexpr bool available() const noexcept {
        return !std::holds_alternative<std::monostate>(_value);
    }
    constexpr bool hasError() const noexcept {
        return std::holds_alternative<std::exception_ptr>(_value);
    }
    const T& value() const& {
        checkHasTry();
        return std::get<T>(_value);
    }
    T& value() & {
        checkHasTry();
        return std::get<T>(_value);
    }
    T&& value() && {
        checkHasTry();
        return std::move(std::get<T>(_value));
    }
    const T&& value() const&& {
        checkHasTry();
        return std::move(std::get<T>(_value));
    }

    template <class... Args>
    T& emplace(Args&&... args) {
        return _value.template emplace<T>(std::forward<Args>(args)...);
    }

    void setException(std::exception_ptr error) {
        if (std::holds_alternative<std::exception_ptr>(_value) &&
            std::get<std::exception_ptr>(_value) == error) {
            return;
        }
        _value.template emplace<std::exception_ptr>(error);
    }
    std::exception_ptr getException() const {
        logicAssert(std::holds_alternative<std::exception_ptr>(_value),
                    "Try object do not has on error");
        return std::get<std::exception_ptr>(_value);
    }
private:
    inline void checkHasTry() const {
        if (std::holds_alternative<T>(_value))
           { return; }
        else if (std::holds_alternative<std::exception_ptr>(_value)) {
            std::rethrow_exception(std::get<std::exception_ptr>(_value));
        } else if (std::holds_alternative<std::monostate>(_value)) {
            throw std::logic_error("Try object is empty");
        } else {
            assert(false);
        }
    }

private:
    std::variant<std::monostate, T, std::exception_ptr> _value;
};


namespace detail {

enum class State : uint8_t {
    START = 0,
    ONLY_RESULT = 1 << 0,
    ONLY_CONTINUATION = 1 << 1,
    DONE = 1 << 5,
};

constexpr State operator|(State lhs, State rhs) {
    return State((uint8_t)lhs | (uint8_t)rhs);
}

constexpr State operator&(State lhs, State rhs) {
    return State((uint8_t)lhs & (uint8_t)rhs);
}

}  // namespace detail

template <class T>
struct TestPromise {
public:
    TestPromise() : _state(detail::State::START) {}
    ~TestPromise() {}

public:
    bool hasResult() const noexcept {
        constexpr auto allow = detail::State::DONE |
detail::State::ONLY_RESULT;
        auto state = _state.load(std::memory_order_acquire);
        return (state & allow) != detail::State();
    }

    void setResult(Try<T>&& value) {
        assert(!hasResult());
        _try_value = std::move(value);
    }

    std::atomic<detail::State> _state;
    Try<T> _try_value;
};

struct Empty {};

int main() {
    int n = 10;
    std::vector<TestPromise<Empty>> promise(n);
    for (int i = 0; i < n; ++i) {
        promise[i].setResult(Empty());
    }
}
```

# no problem
$ g++-11 -std=c++20 -Wall -Werror -O3 test.cpp

# no problem
$ g++ -std=c++20 -Wall -Werror -O0 test.cpp

# Error:‘*(std::__exception_ptr::exception_ptr*)((char*)&<unnamed> +
offsetof(Try<Empty>,Try<Empty>::_value.std::variant<std::monostate, Empty,
std::__exception_ptr::exception_ptr>::<unnamed>.std::__detail::__variant::_Variant_base<std::monostate,
Empty,
std::__exception_ptr::exception_ptr>::<unnamed>.std::__detail::__variant::_Move_assign_base<false,
std::monostate, Empty,
std::__exception_ptr::exception_ptr>::<unnamed>.std::__detail::__variant::_Copy_assign_base<false,
std::monostate, Empty,
std::__exception_ptr::exception_ptr>::<unnamed>.std::__detail::__variant::_Move_ctor_base<false,
std::monostate, Empty,
std::__exception_ptr::exception_ptr>::<unnamed>.std::__detail::__variant::_Copy_ctor_base<false,
std::monostate, Empty,
std::__exception_ptr::exception_ptr>::<unnamed>.std::__detail::__variant::_Variant_storage<false,
std::monostate, Empty,
std::__exception_ptr::exception_ptr>::_M_u)).std::__exception_ptr::exception_ptr::_M_exception_object’
may be used uninitialized [-Werror=maybe-uninitialized]
$ g++ -std=c++20 -Wall -Werror -O3 test.cpp

             reply	other threads:[~2023-04-08  5:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-08  5:10 jincikang at gmail dot com [this message]
2023-04-08  5:14 ` [Bug c++/109448] " jincikang at gmail dot com
2023-04-08 22:09 ` [Bug tree-optimization/109448] [12/13 Regression] wrong uninitialized warning with std::variant of an empty class and std::exception_ptr pinskia at gcc dot gnu.org
2023-04-12 13:17 ` rguenth at gcc dot gnu.org
2023-05-08 12:26 ` [Bug tree-optimization/109448] [12/13/14 " rguenth at gcc dot gnu.org
2024-06-20  9:12 ` [Bug tree-optimization/109448] [12/13/14/15 " rguenth 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-109448-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).