aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-03-21 01:47:54 -0700
committerGitHub <noreply@github.com>2024-03-21 01:47:54 -0700
commitb5cef9e8b48e81fb1f3b56cb752bac95e6a2d44b (patch)
tree40bf026cca4014c3d7502cd70f832ea807a3d0e9 /src
parente831313b10c8c0f6c1984bb1704f3ca90105c416 (diff)
parent4d5e0a0434a69059f623e5c51862fb1a2c553abc (diff)
downloadzig-b5cef9e8b48e81fb1f3b56cb752bac95e6a2d44b.tar.gz
zig-b5cef9e8b48e81fb1f3b56cb752bac95e6a2d44b.zip
Merge pull request #19374 from ziglang/slice-by-len-safety
add OOB safety check for by-length slice of array
Diffstat (limited to 'src')
-rw-r--r--src/Sema.zig16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index be3fd3e4db..6194e6c1bc 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -33284,12 +33284,16 @@ fn analyzeSlice(
try sema.addSafetyCheck(block, src, is_non_null, .unwrap_null);
}
- if (slice_ty.isSlice(mod)) {
- const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice);
- const actual_len = if (slice_ty.sentinel(mod) == null)
- slice_len_inst
- else
- try sema.analyzeArithmetic(block, .add, slice_len_inst, .one, src, end_src, end_src, true);
+ bounds_check: {
+ const actual_len = if (array_ty.zigTypeTag(mod) == .Array)
+ try mod.intRef(Type.usize, array_ty.arrayLenIncludingSentinel(mod))
+ else if (slice_ty.isSlice(mod)) l: {
+ const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice);
+ break :l if (slice_ty.sentinel(mod) == null)
+ slice_len_inst
+ else
+ try sema.analyzeArithmetic(block, .add, slice_len_inst, .one, src, end_src, end_src, true);
+ } else break :bounds_check;
const actual_end = if (slice_sentinel != null)
try sema.analyzeArithmetic(block, .add, end, .one, src, end_src, end_src, true)