Apache DataFusion SQL Query Engine
fix: restore floating-point rounding mode after panic (#26412)
Emanuele Micheletti61c34724fcad
diff --git a/datafusion/common/src/rounding.rs b/datafusion/common/src/rounding.rsindex b3b514e863..11aede0b24 100644--- a/datafusion/common/src/rounding.rs+++ b/datafusion/common/src/rounding.rs@@ -271,11 +271,18 @@ wherenot(target_os = "windows")))]unsafe {- let current = fegetround();+ struct RoundingModeGuard(i32);++ impl Drop for RoundingModeGuard {+ fn drop(&mut self) {+ unsafe { fesetround(self.0) };+ }+ }++ // Restore the previous mode even if the operation panics.+ let _guard = RoundingModeGuard(fegetround());fesetround(if UPPER { FE_UPWARD } else { FE_DOWNWARD });- let result = operation(lhs, rhs);- fesetround(current);- result+ operation(lhs, rhs)}#[cfg(any(not(any(target_arch = "x86_64", target_arch = "aarch64")),@@ -288,6 +295,35 @@ wheremod tests {use super::{next_down, next_up};+ #[cfg(all(+ any(target_arch = "x86_64", target_arch = "aarch64"),+ not(target_os = "windows")+ ))]+ #[test]+ fn test_alter_fp_rounding_mode_restores_after_panic() {+ use super::{FE_DOWNWARD, alter_fp_rounding_mode, fegetround, fesetround};+ use crate::ScalarValue;++ unsafe {+ let original = fegetround();+ fesetround(FE_DOWNWARD);++ let result = std::panic::catch_unwind(|| {+ alter_fp_rounding_mode::<true, _>(+ &ScalarValue::Null,+ &ScalarValue::Null,+ |_, _| panic!("operation panicked"),+ )+ });++ let restored = fegetround();+ fesetround(original);++ assert!(result.is_err());+ assert_eq!(restored, FE_DOWNWARD);+ }+ }+#[test]fn test_next_down() {let x = 1.0f64;