Missing bounds check on upper bound with const ranges foo[1:3].

This commit is contained in:
Christoffer Lerno
2025-07-02 13:12:49 +02:00
parent 93ded9c1e0
commit 2151cd0929
3 changed files with 11 additions and 2 deletions

View File

@@ -15,6 +15,7 @@
- Correctly reject interface methods `type` and `ptr`.
- Comparing a null ZString with a non-null ZString would crash.
- Switch case with const non-int / enum would be treated as ints and crash. #2263
- Missing bounds check on upper bound with const ranges `foo[1:3]`.
### Stdlib changes

View File

@@ -4106,9 +4106,9 @@ INLINE bool sema_expr_analyse_range_internal(SemaContext *context, Range *range,
}
break;
case RANGE_CONST_RANGE:
if (range->len_index > len)
if (range->start_index + range->len_index > len)
{
RETURN_SEMA_ERROR(end ? end : start, "End index out of bounds, was %d, exceeding max index %d.", range->len_index - 1, len - 1);
RETURN_SEMA_ERROR(end ? end : start, "End index out of bounds, was %d, exceeding max index %d.", range->start_index + range->len_index - 1, len - 1);
}
break;
default:

View File

@@ -0,0 +1,8 @@
module ccc;
fn int main()
{
int[3] abc = {1, 2, 3};
abc[1:3]; // #error: End index out of bounds, was 3, exceeding max index 2.
return 0;
}