[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master d1e29f2 6/6: Check return values of <cfenv> f
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master d1e29f2 6/6: Check return values of <cfenv> functions |
Date: |
Thu, 5 Jan 2017 21:52:29 +0000 (UTC) |
branch: master
commit d1e29f27a213d737f824e8bebdd750cd7d526221
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Check return values of <cfenv> functions
Failing to check return values of functions that use them to signal
failure is a poor practice. That an example in the C99 standard fails
to check feholdexcept()'s return value [F.9.6.6/2] is no excuse. That
C++11 fails to wrap this function and throw an error on failure is
lamentable.
---
fenv_lmi.cpp | 7 ++++---
fenv_lmi_test.cpp | 6 +++---
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/fenv_lmi.cpp b/fenv_lmi.cpp
index 8e0cb1d..f2e0f2c 100644
--- a/fenv_lmi.cpp
+++ b/fenv_lmi.cpp
@@ -66,8 +66,8 @@ void fenv_initialize()
x87_control_word(default_x87_control_word());
#else // !defined LMI_X87
std::fenv_t saved_env;
- std::feholdexcept(&saved_env);
- std::fesetround(FE_TONEAREST);
+ LMI_ASSERT(0 == std::feholdexcept(&saved_env));
+ LMI_ASSERT(0 == std::fesetround(FE_TONEAREST));
// Standard C++ provides no way to set hardware precision.
// Here is an example of a C99 7.6/9 extension that controls
// hardware precision for MinGW32:
@@ -122,6 +122,7 @@ e_ieee754_rounding fenv_rounding()
;
#else // !defined LMI_X87
int z = std::fegetround();
+ LMI_ASSERT(0 <= z); // Returns negative on failure [C99 7.6.3.1/3].
return
(FE_TONEAREST == z) ? fe_tonearest
: (FE_DOWNWARD == z) ? fe_downward
@@ -153,7 +154,7 @@ void fenv_rounding(e_ieee754_rounding rounding_mode)
: (fe_towardzero == rounding_mode) ? FE_TOWARDZERO
: throw std::runtime_error("Failed to set rounding mode.")
;
- std::fesetround(z);
+ LMI_ASSERT(0 == std::fesetround(z));
#endif // !defined LMI_X87
}
diff --git a/fenv_lmi_test.cpp b/fenv_lmi_test.cpp
index 5973664..4672b78 100644
--- a/fenv_lmi_test.cpp
+++ b/fenv_lmi_test.cpp
@@ -135,13 +135,13 @@ int test_main(int, char*[])
# if defined __MINGW32__
// Test the C99 method, as extended by MinGW.
- std::fesetenv(FE_PC53_ENV);
+ BOOST_TEST_EQUAL(0, std::fesetenv(FE_PC53_ENV));
BOOST_TEST_EQUAL_BITS(0x027f, x87_control_word());
- std::fesetenv(FE_PC64_ENV);
+ BOOST_TEST_EQUAL(0, std::fesetenv(FE_PC64_ENV));
BOOST_TEST_EQUAL_BITS(0x037f, x87_control_word());
- std::fesetenv(FE_DFL_ENV);
+ BOOST_TEST_EQUAL(0, std::fesetenv(FE_DFL_ENV));
BOOST_TEST_EQUAL_BITS(0x037f, x87_control_word());
# endif // defined __MINGW32__
- [lmi-commits] [lmi] master updated (df07870 -> d1e29f2), Greg Chicares, 2017/01/05
- [lmi-commits] [lmi] master 6b06fd4 2/6: Use standard <cfenv> macros with standard <cfenv> functions, Greg Chicares, 2017/01/05
- [lmi-commits] [lmi] master ba30a9f 5/6: Sequester <cfenv> from code that includes 'fenv_lmi.hpp', Greg Chicares, 2017/01/05
- [lmi-commits] [lmi] master 6ecceb5 4/6: Rename a local variable for clarity, Greg Chicares, 2017/01/05
- [lmi-commits] [lmi] master 17b62a5 1/6: Update history of C and C++ numerics, Greg Chicares, 2017/01/05
- [lmi-commits] [lmi] master d1e29f2 6/6: Check return values of <cfenv> functions,
Greg Chicares <=
- [lmi-commits] [lmi] master e04cad4 3/6: Explicitly qualify standard functions, Greg Chicares, 2017/01/05