Skip to content

Commit

Permalink
fix(naga): Forbid negative indexing in const expressions (#7155)
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev authored Feb 17, 2025
1 parent d625d08 commit 7240c18
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions naga/src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,26 @@ impl super::Validator {
}
}

// If we know both the length and the index, we can do the
// bounds check now.
if let crate::proc::IndexableLength::Known(known_length) =
base_type.indexable_length(module)?
// If index is const we can do check for non-negative index
match module
.to_ctx()
.eval_expr_to_u32_from(index, &function.expressions)
{
match module
.to_ctx()
.eval_expr_to_u32_from(index, &function.expressions)
{
Ok(value) => {
Ok(value) => {
// If we know both the length and the index, we can do the
// bounds check now.
if let crate::proc::IndexableLength::Known(known_length) =
base_type.indexable_length(module)?
{
if value >= known_length {
return Err(ExpressionError::IndexOutOfBounds(base, value));
}
}
Err(crate::proc::U32EvalError::Negative) => {
return Err(ExpressionError::NegativeIndex(base))
}
Err(crate::proc::U32EvalError::NonConst) => {}
}
Err(crate::proc::U32EvalError::Negative) => {
return Err(ExpressionError::NegativeIndex(base))
}
Err(crate::proc::U32EvalError::NonConst) => {}
}

ShaderStages::all()
Expand Down

0 comments on commit 7240c18

Please sign in to comment.