From 92b7b0d22ab7d8ebf0625fa95f2f12e12a081e8d Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 18 Oct 2023 17:35:41 -0700 Subject: [PATCH] Make validation reject 64-bit floating-point literals. Make expression validation and constant expression validation reject `Literal` expressions containing `F64` literals unless the `FLOAT64` capability is enabled. --- src/valid/expression.rs | 17 +++++++++++++++-- src/valid/type.rs | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/valid/expression.rs b/src/valid/expression.rs index 765c8e2712..f77844b4b1 100644 --- a/src/valid/expression.rs +++ b/src/valid/expression.rs @@ -140,6 +140,8 @@ pub enum ConstExpressionError { Type(#[from] ResolveError), #[error(transparent)] Literal(#[from] LiteralError), + #[error(transparent)] + Width(#[from] super::r#type::WidthError), } #[derive(Clone, Debug, thiserror::Error)] @@ -149,6 +151,8 @@ pub enum LiteralError { NaN, #[error("Float literal is infinite")] Infinity, + #[error(transparent)] + Width(#[from] super::r#type::WidthError), } #[cfg(feature = "validate")] @@ -188,7 +192,7 @@ impl super::Validator { match gctx.const_expressions[handle] { E::Literal(literal) => { - check_literal_value(literal)?; + self.validate_literal(literal)?; } E::Constant(_) | E::ZeroValue(_) => {} E::Compose { ref components, ty } => { @@ -343,7 +347,7 @@ impl super::Validator { ShaderStages::all() } E::Literal(literal) => { - check_literal_value(literal)?; + self.validate_literal(literal)?; ShaderStages::all() } E::Constant(_) | E::ZeroValue(_) => ShaderStages::all(), @@ -1563,6 +1567,15 @@ impl super::Validator { _ => Err(ExpressionError::ExpectedGlobalVariable), } } + + pub fn validate_literal(&self, literal: crate::Literal) -> Result<(), LiteralError> { + let kind = literal.scalar_kind(); + let width = literal.width(); + self.check_width(kind, width)?; + check_literal_value(literal)?; + + Ok(()) + } } pub fn check_literal_value(literal: crate::Literal) -> Result<(), LiteralError> { diff --git a/src/valid/type.rs b/src/valid/type.rs index c93ae67d74..a5321178c1 100644 --- a/src/valid/type.rs +++ b/src/valid/type.rs @@ -129,6 +129,7 @@ pub enum TypeError { } #[derive(Clone, Debug, thiserror::Error)] +#[cfg_attr(test, derive(PartialEq))] pub enum WidthError { #[error("The {0:?} scalar width {1} is not supported")] Invalid(crate::ScalarKind, crate::Bytes),