mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
stage1: add error for slice.len incr beyond bounds
comptime direct slice.len increment dodges bounds checking but we can emit an error for it, at least in the simple case. - promote original assert to compile-error - add test case closes #7810
This commit is contained in:
parent
3d4eeafb47
commit
f9b85c6e50
2 changed files with 18 additions and 3 deletions
|
|
@ -22655,9 +22655,10 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
|
||||||
if (ptr_field->data.x_ptr.data.base_array.array_val->data.x_array.special !=
|
if (ptr_field->data.x_ptr.data.base_array.array_val->data.x_array.special !=
|
||||||
ConstArraySpecialBuf)
|
ConstArraySpecialBuf)
|
||||||
{
|
{
|
||||||
ir_assert(new_index <
|
if (new_index >= ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len) {
|
||||||
ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len,
|
ir_add_error(ira, &elem_ptr_instruction->base.base, buf_sprintf("out of bounds slice"));
|
||||||
&elem_ptr_instruction->base.base);
|
return ira->codegen->invalid_inst_gen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
|
out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
|
||||||
out_val->data.x_ptr.data.base_array.array_val =
|
out_val->data.x_ptr.data.base_array.array_val =
|
||||||
|
|
|
||||||
|
|
@ -7981,6 +7981,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||||
"tmp.zig:7:37: note: referenced here",
|
"tmp.zig:7:37: note: referenced here",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// issue #7810
|
||||||
|
cases.add("comptime slice-len increment beyond bounds",
|
||||||
|
\\export fn foo_slice_len_increment_beyond_bounds() void {
|
||||||
|
\\ comptime {
|
||||||
|
\\ var buf_storage: [8]u8 = undefined;
|
||||||
|
\\ var buf: []const u8 = buf_storage[0..];
|
||||||
|
\\ buf.len += 1;
|
||||||
|
\\ buf[8] = 42;
|
||||||
|
\\ }
|
||||||
|
\\}
|
||||||
|
, &[_][]const u8{
|
||||||
|
":6:12: error: out of bounds slice",
|
||||||
|
});
|
||||||
|
|
||||||
cases.add("comptime slice-sentinel is out of bounds (unterminated)",
|
cases.add("comptime slice-sentinel is out of bounds (unterminated)",
|
||||||
\\export fn foo_array() void {
|
\\export fn foo_array() void {
|
||||||
\\ comptime {
|
\\ comptime {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue