aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLexi Winter <lexi@hemlock.eden.le-fay.org>2025-03-29 04:30:06 +0000
committerLexi Winter <lexi@hemlock.eden.le-fay.org>2025-03-29 04:30:06 +0000
commitfdcef3e5582466785811446acda21cd1b4b76efd (patch)
tree5f096820298434eaa2673bf4eecec2b153638a96
parent25586f6e996f3c05277beb10a1dcc74e6be96cc7 (diff)
downloadlibnvxx-fdcef3e5582466785811446acda21cd1b4b76efd.tar.gz
libnvxx-fdcef3e5582466785811446acda21cd1b4b76efd.tar.bz2
fix error()/set_error() error handling
-rw-r--r--libnvxx/const_nv_list.cc4
-rw-r--r--libnvxx/nv_list.cc3
-rw-r--r--libnvxx/nvxx_base.h4
-rw-r--r--libnvxx/tests/nvxx_basic.cc35
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
@@ -29,6 +29,37 @@ TEST_CASE(nvxx_ctor_default)
}
/*
+ * 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);