From bd998f6c32f94dd66b98f23866ede7ed2878e87b Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 7 Jan 2021 14:30:18 -0500 Subject: [PATCH] Validate constant array sizes --- src/front/glsl/mod.rs | 2 +- src/proc/validator.rs | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/front/glsl/mod.rs b/src/front/glsl/mod.rs index 9711aca90f..c5ecfbbfe3 100644 --- a/src/front/glsl/mod.rs +++ b/src/front/glsl/mod.rs @@ -14,13 +14,13 @@ use ast::Program; use lex::Lexer; mod error; use error::ParseError; +mod functions; mod parser; #[cfg(test)] mod parser_tests; mod token; mod types; mod variables; -mod functions; pub fn parse_str( source: &str, diff --git a/src/proc/validator.rs b/src/proc/validator.rs index 51ddc46f0e..3967766362 100644 --- a/src/proc/validator.rs +++ b/src/proc/validator.rs @@ -29,6 +29,8 @@ pub enum ConstantError { InvalidType, #[error("The component handle {0:?} can not be resolved")] UnresolvedComponent(Handle), + #[error("The array size handle {0:?} can not be resolved")] + UnresolvedSize(Handle), } #[derive(Clone, Debug, PartialEq, thiserror::Error)] @@ -312,7 +314,7 @@ impl Validator { &self, handle: Handle, constants: &Arena, - _types: &Arena, + types: &Arena, ) -> Result<(), ConstantError> { let con = &constants[handle]; match con.inner { @@ -321,14 +323,27 @@ impl Validator { return Err(ConstantError::InvalidType); } } - crate::ConstantInner::Composite { - ty: _, - ref components, - } => { + crate::ConstantInner::Composite { ty, ref components } => { + match types[ty].inner { + crate::TypeInner::Array { + size: crate::ArraySize::Dynamic, + .. + } => { + return Err(ConstantError::InvalidType); + } + crate::TypeInner::Array { + size: crate::ArraySize::Constant(size_handle), + .. + } => { + if handle <= size_handle { + return Err(ConstantError::UnresolvedSize(size_handle)); + } + } + _ => {} //TODO + } if let Some(&comp) = components.iter().find(|&&comp| handle <= comp) { return Err(ConstantError::UnresolvedComponent(comp)); } - //TODO } } Ok(())