Fix array index access at negative index

Conversion from negative float to unsigned is saturating to 0 in rust
and undefined behavior in C++, we should therefore handle the case
properly

Fixes #8222
This commit is contained in:
Olivier Goffart 2025-04-22 11:28:09 +02:00 committed by GitHub
parent ff6065ace4
commit cd8ab8ce53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 24 additions and 10 deletions

View file

@ -3250,9 +3250,7 @@ fn compile_expression(expr: &llr::Expression, ctx: &EvaluationContext) -> String
let base_e = compile_expression(array, ctx);
let index_e = compile_expression(index, ctx);
let value_e = compile_expression(value, ctx);
format!(
"{base_e}->set_row_data({index_e}, {value_e})"
)
format!("[&](auto index, const auto &base) {{ if (index >= 0. && std::size_t(index) < base->row_count()) base->set_row_data(index, {value_e}); }}({index_e}, {base_e})")
}
Expression::BinaryExpression { lhs, rhs, op } => {
let lhs_str = compile_expression(lhs, ctx);

View file

@ -2381,7 +2381,7 @@ fn compile_expression(expr: &Expression, ctx: &EvaluationContext) -> TokenStream
let base_e = compile_expression(array, ctx);
let index_e = compile_expression(index, ctx);
let value_e = compile_expression(value, ctx);
quote!((#base_e).set_row_data(#index_e as usize, #value_e as _))
quote!((#base_e).set_row_data(#index_e as isize as usize, #value_e as _))
}
Expression::BinaryExpression { lhs, rhs, op } => {
let lhs_ty = lhs.ty(ctx);