diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-08-05 17:08:31 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-08-05 22:13:57 +0300 |
| commit | 5605f6e0e302cbf345a5229ea58aef6757fe139d (patch) | |
| tree | 962e8e809a5a116043698b9eda4366c27821466c /src/Sema.zig | |
| parent | 6aa438f0654569c1713a5c0c49b0bdda20d87c0f (diff) | |
| download | zig-5605f6e0e302cbf345a5229ea58aef6757fe139d.tar.gz zig-5605f6e0e302cbf345a5229ea58aef6757fe139d.zip | |
Sema: account for sentinel in bounds check
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index e964c8f76b..4bfd6ac1c4 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -25574,6 +25574,22 @@ fn analyzeSlice( const is_non_null = try sema.analyzeIsNull(block, ptr_src, ptr, true); try sema.addSafetyCheck(block, is_non_null, .unwrap_null); } + + if (slice_ty.isSlice()) { + const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice); + const actual_len = if (slice_ty.sentinel() == null) + slice_len_inst + else + try sema.analyzeArithmetic(block, .add, slice_len_inst, .one, src, end_src, end_src); + + const actual_end = if (slice_sentinel != null) + try sema.analyzeArithmetic(block, .add, end, .one, src, end_src, end_src) + else + end; + + try sema.panicIndexOutOfBounds(block, src, actual_end, actual_len, .cmp_lte); + } + // requirement: result[new_len] == slice_sentinel try sema.panicSentinelMismatch(block, src, slice_sentinel, elem_ty, result, new_len); } @@ -25635,7 +25651,11 @@ fn analyzeSlice( break :blk try sema.analyzeArithmetic(block, .add, slice_len_inst, .one, src, end_src, end_src); } else null; if (opt_len_inst) |len_inst| { - try sema.panicIndexOutOfBounds(block, src, end, len_inst, .cmp_lte); + const actual_end = if (slice_sentinel != null) + try sema.analyzeArithmetic(block, .add, end, .one, src, end_src, end_src) + else + end; + try sema.panicIndexOutOfBounds(block, src, actual_end, len_inst, .cmp_lte); } // requirement: start <= end |
