lmi-commits
[Top][All Lists]
Advanced

[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__
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]