diff options
| author | Mitchell Hashimoto <mitchell.hashimoto@gmail.com> | 2022-03-20 20:04:18 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-03-21 22:10:34 -0400 |
| commit | 91fd0f42c88f4bea424b5a5c58435a2a98b57a58 (patch) | |
| tree | 37645cd0c83902451b59492dea2ad75db77cb06e /src | |
| parent | 0fb005d1d05acb680389ce8be51bab6020309922 (diff) | |
| download | zig-91fd0f42c88f4bea424b5a5c58435a2a98b57a58.tar.gz zig-91fd0f42c88f4bea424b5a5c58435a2a98b57a58.zip | |
stage2: out of bounds error for slicing
Diffstat (limited to 'src')
| -rw-r--r-- | src/Sema.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 61c46bcce7..2a3eff928b 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -19587,6 +19587,14 @@ fn analyzeSlice( if (!end_is_len) { const end = try sema.coerce(block, Type.usize, uncasted_end_opt, end_src); if (try sema.resolveMaybeUndefVal(block, end_src, end)) |end_val| { + if (end_val.compare(.gt, len_val, Type.usize)) { + return sema.fail( + block, + end_src, + "end index {} out of bounds for array of length {}", + .{ end_val.fmtValue(Type.usize), len_val.fmtValue(Type.usize) }, + ); + } if (end_val.eql(len_val, Type.usize)) { end_is_len = true; } @@ -19605,6 +19613,14 @@ fn analyzeSlice( .data = slice_val.sliceLen(), }; const slice_len_val = Value.initPayload(&int_payload.base); + if (end_val.compare(.gt, slice_len_val, Type.usize)) { + return sema.fail( + block, + end_src, + "end index {} out of bounds for slice of length {}", + .{ end_val.fmtValue(Type.usize), slice_len_val.fmtValue(Type.usize) }, + ); + } if (end_val.eql(slice_len_val, Type.usize)) { end_is_len = true; } @@ -19635,6 +19651,20 @@ fn analyzeSlice( break :s null; }; + // requirement: start <= end + if (try sema.resolveDefinedValue(block, src, end)) |end_val| { + if (try sema.resolveDefinedValue(block, src, start)) |start_val| { + if (start_val.compare(.gt, end_val, Type.usize)) { + return sema.fail( + block, + start_src, + "start index {} is larger than end index {}", + .{ start_val.fmtValue(Type.usize), end_val.fmtValue(Type.usize) }, + ); + } + } + } + const new_len = try sema.analyzeArithmetic(block, .sub, end, start, src, end_src, start_src); const opt_new_len_val = try sema.resolveDefinedValue(block, src, new_len); |
