Skip to content

Commit

Permalink
Make validation reject 64-bit floating-point literals.
Browse files Browse the repository at this point in the history
Make expression validation and constant expression validation reject
`Literal` expressions containing `F64` literals unless the `FLOAT64`
capability is enabled.
  • Loading branch information
jimblandy authored and teoxoy committed Oct 19, 2023
1 parent 1bb84ae commit 334f745
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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")]
Expand Down Expand Up @@ -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 } => {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions src/valid/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 334f745

Please sign in to comment.