From fdcef3e5582466785811446acda21cd1b4b76efd Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sat, 29 Mar 2025 04:30:06 +0000 Subject: fix error()/set_error() error handling --- libnvxx/const_nv_list.cc | 4 +++- libnvxx/nv_list.cc | 3 ++- libnvxx/nvxx_base.h | 4 ++-- libnvxx/tests/nvxx_basic.cc | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/libnvxx/const_nv_list.cc b/libnvxx/const_nv_list.cc index d382691..4f6504f 100644 --- a/libnvxx/const_nv_list.cc +++ b/libnvxx/const_nv_list.cc @@ -60,8 +60,10 @@ const_nv_list::ptr() const namespace __detail { std::error_code -__const_nv_list::error() const noexcept +__const_nv_list::error() const { + __throw_if_null(); + if (auto const err = ::nvlist_error(__m_nv); err != 0) return (std::make_error_code(std::errc(err))); return {}; diff --git a/libnvxx/nv_list.cc b/libnvxx/nv_list.cc index 533edd7..8f868d9 100644 --- a/libnvxx/nv_list.cc +++ b/libnvxx/nv_list.cc @@ -142,8 +142,9 @@ namespace __detail { */ void -__nv_list::set_error(int error) noexcept +__nv_list::set_error(int error) { + __throw_if_null(); // nvlist does not allow changing an existing error state __throw_if_error(); ::nvlist_set_error(__m_nv, error); diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index f47eb6b..0f6b254 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -128,7 +128,7 @@ struct __const_nv_list : virtual __nv_list_base { * Return the error code associated with this nvlist, if any, by * calling nvlist_error(). */ - [[nodiscard]] std::error_code error() const noexcept; + [[nodiscard]] std::error_code error() const; /* * Return true if this nvlist is in a non-error state, otherwise false. @@ -205,7 +205,7 @@ struct __nv_list : virtual __nv_list_base { /* * Set the error code on this nvlist to the given value. */ - void set_error(int) noexcept; + void set_error(int); /* * Convert this nv_list into a const_nv_list. This is a shallow copy diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index d16426f..7638a0b 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -28,6 +28,37 @@ TEST_CASE(nvxx_ctor_default) auto nvl = bsd::nv_list(); } +/* + * error/set_error + */ + +TEST_CASE(nvxx_set_error) +{ + auto nvl = bsd::nv_list(); + + ATF_REQUIRE_EQ(true, !nvl.error()); + nvl.set_error(EINVAL); + ATF_REQUIRE_EQ(true, (nvl.error() == std::errc::invalid_argument)); + + ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.set_error(EEXIST)); +} + +TEST_CASE(nvxx_error_null) +{ + auto nvl = bsd::nv_list(); + auto nvl2 = std::move(nvl); + + ATF_REQUIRE_THROW(std::logic_error, (void)nvl.error()); +} + +TEST_CASE(nvxx_set_error_null) +{ + auto nvl = bsd::nv_list(); + auto nvl2 = std::move(nvl); + + ATF_REQUIRE_THROW(std::logic_error, nvl.set_error(EINVAL)); +} + /* * exists(_type) */ @@ -1534,6 +1565,10 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, nvxx_ctor_default); ATF_ADD_TEST_CASE(tcs, nvxx_ignore_case); + ATF_ADD_TEST_CASE(tcs, nvxx_set_error); + ATF_ADD_TEST_CASE(tcs, nvxx_set_error_null); + ATF_ADD_TEST_CASE(tcs, nvxx_error_null); + ATF_ADD_TEST_CASE(tcs, nvxx_exists); ATF_ADD_TEST_CASE(tcs, nvxx_exists_nul_key); ATF_ADD_TEST_CASE(tcs, nvxx_exists_type); -- cgit v1.2.3