From 7042170c04855542cc01a8c1ca2733c0b98b53eb Mon Sep 17 00:00:00 2001 From: Alex Skinner Date: Fri, 27 Nov 2026 14:07:34 +0000 Subject: [PATCH] fix: return errors instead of UB in boolean evaluators boolean_not_unary_eval and the boolean binary evaluators used unreachable_unchecked on unexpected value types. Fuzzing can reach those arms when binder type checks are incomplete, so return UnsupportedUnaryOperator or Incomparable instead. Cover the NOT mismatch with a unit test and an SLT regression. Co-authored-by: Alex Skinner --- src/types/evaluator/boolean.rs | 68 ++++++++++++++++++++++++++----- src/types/evaluator/unary.rs | 2 +- tests/slt/boolean_type_safety.slt | 60 +++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 tests/slt/boolean_type_safety.slt diff --git a/src/types/evaluator/boolean.rs b/src/types/evaluator/boolean.rs index ba92b55e..8e0158ec 100644 --- a/src/types/evaluator/boolean.rs +++ b/src/types/evaluator/boolean.rs @@ -13,17 +13,22 @@ // limitations under the License. use crate::errors::DatabaseError; +use crate::expression::UnaryOperator; use crate::types::evaluator::cast::{to_char, to_varbinary, to_varchar}; use crate::types::evaluator::DataValue; use crate::types::CharLengthUnits; use ordered_float::OrderedFloat; -use std::hint; -pub fn boolean_not_unary_eval(value: &DataValue) -> DataValue { - match value { +pub fn boolean_not_unary_eval(value: &DataValue) -> Result { + Ok(match value { DataValue::Boolean(value) => DataValue::Boolean(!value), DataValue::Null => DataValue::Null, - _ => unsafe { hint::unreachable_unchecked() }, - } + _ => { + return Err(DatabaseError::UnsupportedUnaryOperator( + value.logical_type(), + UnaryOperator::Not, + )) + } + }) } pub fn boolean_and_binary_eval( left: &DataValue, @@ -36,7 +41,12 @@ pub fn boolean_and_binary_eval( (DataValue::Null, DataValue::Null) | (DataValue::Boolean(true), DataValue::Null) | (DataValue::Null, DataValue::Boolean(true)) => DataValue::Null, - _ => unsafe { hint::unreachable_unchecked() }, + _ => { + return Err(DatabaseError::Incomparable( + left.logical_type(), + right.logical_type(), + )) + } }) } pub fn boolean_or_binary_eval( @@ -50,7 +60,12 @@ pub fn boolean_or_binary_eval( (DataValue::Null, DataValue::Null) | (DataValue::Boolean(false), DataValue::Null) | (DataValue::Null, DataValue::Boolean(false)) => DataValue::Null, - _ => unsafe { hint::unreachable_unchecked() }, + _ => { + return Err(DatabaseError::Incomparable( + left.logical_type(), + right.logical_type(), + )) + } }) } pub fn boolean_eq_binary_eval( @@ -62,7 +77,12 @@ pub fn boolean_eq_binary_eval( (DataValue::Null, DataValue::Boolean(_)) | (DataValue::Boolean(_), DataValue::Null) | (DataValue::Null, DataValue::Null) => DataValue::Null, - _ => unsafe { hint::unreachable_unchecked() }, + _ => { + return Err(DatabaseError::Incomparable( + left.logical_type(), + right.logical_type(), + )) + } }) } @@ -123,7 +143,12 @@ pub fn boolean_not_eq_binary_eval( (DataValue::Null, DataValue::Boolean(_)) | (DataValue::Boolean(_), DataValue::Null) | (DataValue::Null, DataValue::Null) => DataValue::Null, - _ => unsafe { hint::unreachable_unchecked() }, + _ => { + return Err(DatabaseError::Incomparable( + left.logical_type(), + right.logical_type(), + )) + } }) } @@ -132,15 +157,19 @@ pub fn boolean_not_eq_binary_eval( #[maybe_async] mod test { use super::*; + use crate::expression::UnaryOperator; use crate::types::value::Utf8Type; #[test] fn test_boolean_binary_evaluators() { assert_eq!( - boolean_not_unary_eval(&DataValue::Boolean(true)), + boolean_not_unary_eval(&DataValue::Boolean(true)).unwrap(), DataValue::Boolean(false) ); - assert_eq!(boolean_not_unary_eval(&DataValue::Null), DataValue::Null); + assert_eq!( + boolean_not_unary_eval(&DataValue::Null).unwrap(), + DataValue::Null + ); assert_eq!( boolean_and_binary_eval(&DataValue::Boolean(true), &DataValue::Boolean(true)).unwrap(), DataValue::Boolean(true) @@ -184,6 +213,23 @@ mod test { ); } + #[test] + fn test_boolean_not_on_non_boolean_returns_error() { + let result = boolean_not_unary_eval(&DataValue::Int32(42)); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + DatabaseError::UnsupportedUnaryOperator(_, UnaryOperator::Not) + )); + + let result = boolean_not_unary_eval(&DataValue::Utf8 { + value: "test".into(), + ty: Utf8Type::Variable(None), + unit: CharLengthUnits::Characters, + }); + assert!(result.is_err()); + } + #[test] fn test_boolean_cast_evaluators() { let value = DataValue::Boolean(true); diff --git a/src/types/evaluator/unary.rs b/src/types/evaluator/unary.rs index f12f20ea..ef9a5cdb 100644 --- a/src/types/evaluator/unary.rs +++ b/src/types/evaluator/unary.rs @@ -61,7 +61,7 @@ pub(crate) fn eval_unary( UNARY_INT16_MINUS => int16_minus_unary_eval(value)?, UNARY_INT32_MINUS => int32_minus_unary_eval(value)?, UNARY_INT64_MINUS => int64_minus_unary_eval(value)?, - UNARY_BOOLEAN_NOT => boolean_not_unary_eval(value), + UNARY_BOOLEAN_NOT => boolean_not_unary_eval(value)?, UNARY_FLOAT32_MINUS => float32_minus_unary_eval(value), UNARY_FLOAT64_MINUS => float64_minus_unary_eval(value), UNARY_SQL_NULL => crate::types::value::DataValue::Null, diff --git a/tests/slt/boolean_type_safety.slt b/tests/slt/boolean_type_safety.slt new file mode 100644 index 00000000..dd6846d1 --- /dev/null +++ b/tests/slt/boolean_type_safety.slt @@ -0,0 +1,60 @@ +# Boolean operators reject non-boolean operands instead of invoking undefined behaviour. + +query B +SELECT NOT true +---- +false + +query B +SELECT NOT false +---- +true + +query B +SELECT NOT NULL +---- +NULL + +query error +SELECT NOT 42 + +query error +SELECT NOT 0 + +query error +SELECT NOT 'test' + +query error +SELECT NOT 'false' + +query error +SELECT NOT 1.5 + +query error +SELECT NOT CURRENT_TIMESTAMP + +query error +SELECT NOT (1 + 1) + +query error +SELECT NOT ('a' || 'b') + +query B +SELECT NOT (1 = 1) +---- +false + +query B +SELECT NOT (1 <> 1) +---- +true + +query B +SELECT NOT (NULL IS NULL) +---- +false + +query B +SELECT NOT (1 IS NULL) +---- +true