authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-13 23:20:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-13 23:21:44-07:00
logbd46410419086acd274e33b47d9ae5dc1f678a1b
tree7899c29d06764ad2b64dea83137847b4d3f2710e
parent0f9345ea9a138a23937321cde3f83a76838ee36a

Revert "Merge pull request #18410 from dweiller/by-length-slice-bug"

This reverts commit d9d840a33ac8abb0e616de862f592821a7f4a35e, reversing changes made to a04d4330945565b8d6f298ace993f6954c42d0f3. This is not an adequate implementation of the missing safety check, as evidenced by the changes to std.json that are reverted in this commit. Reopens #18382 Closes #18510

4 files changed, 0 insertions(+), 76 deletions(-)

lib/std/json/static.zig-12
......@@ -402,33 +402,21 @@ pub fn innerParse(
402402 },
403403 .partial_string_escaped_1 => |arr| {
404404 if (i + arr.len > r.len) return error.LengthMismatch;
405 // tell the compiler that the by-length slice below is valid;
406 // this assert is required for the inequality to be comptime-known
407 if (arr.len > r.len) unreachable;
408405 @memcpy(r[i..][0..arr.len], arr[0..]);
409406 i += arr.len;
410407 },
411408 .partial_string_escaped_2 => |arr| {
412409 if (i + arr.len > r.len) return error.LengthMismatch;
413 // tell the compiler that the by-length slice below is valid;
414 // this assert is required for the inequality to be comptime-known
415 if (arr.len > r.len) unreachable;
416410 @memcpy(r[i..][0..arr.len], arr[0..]);
417411 i += arr.len;
418412 },
419413 .partial_string_escaped_3 => |arr| {
420414 if (i + arr.len > r.len) return error.LengthMismatch;
421 // tell the compiler that the by-length slice below is valid;
422 // this assert is required for the inequality to be comptime-known
423 if (arr.len > r.len) unreachable;
424415 @memcpy(r[i..][0..arr.len], arr[0..]);
425416 i += arr.len;
426417 },
427418 .partial_string_escaped_4 => |arr| {
428419 if (i + arr.len > r.len) return error.LengthMismatch;
429 // tell the compiler that the by-length slice below is valid;
430 // this assert is required for the inequality to be comptime-known
431 if (arr.len > r.len) unreachable;
432420 @memcpy(r[i..][0..arr.len], arr[0..]);
433421 i += arr.len;
434422 },
src/Sema.zig-24
......@@ -32635,30 +32635,6 @@ fn analyzeSlice(
3263532635 if (!end_is_len) {
3263632636 const end = if (by_length) end: {
3263732637 const len = try sema.coerce(block, Type.usize, uncasted_end_opt, end_src);
32638 if (try sema.resolveValue(len)) |slice_len_val| {
32639 const len_s_val = try mod.intValue(
32640 Type.usize,
32641 array_ty.arrayLenIncludingSentinel(mod),
32642 );
32643 if (!(try sema.compareScalar(slice_len_val, .lte, len_s_val, Type.usize))) {
32644 const sentinel_label: []const u8 = if (array_ty.sentinel(mod) != null)
32645 " +1 (sentinel)"
32646 else
32647 "";
32648
32649 return sema.fail(
32650 block,
32651 end_src,
32652 "length {} out of bounds for array of length {}{s}",
32653 .{
32654 slice_len_val.fmtValue(Type.usize, mod),
32655 len_val.fmtValue(Type.usize, mod),
32656 sentinel_label,
32657 },
32658 );
32659 }
32660 }
32661 // check len is less than array size if comptime known
3266232638 const uncasted_end = try sema.analyzeArithmetic(block, .add, start, len, src, start_src, end_src, false);
3266332639 break :end try sema.coerce(block, Type.usize, uncasted_end, end_src);
3266432640 } else try sema.coerce(block, Type.usize, uncasted_end_opt, end_src);
test/cases/compile_errors/slice_of_array_by-length_oversized.zig deleted-19
......@@ -1,19 +0,0 @@
1export fn entry1() void {
2 var buf: [5]u8 = undefined;
3 var a: u32 = 6;
4 _ = &a;
5 _ = buf[a..][0..10];
6}
7
8export fn entry2() void {
9 var buf: [5]u8 = undefined;
10 const a: u32 = 6;
11 _ = buf[a..][0..10];
12}
13
14// error
15// backend=stage2
16// target=native
17//
18// :5:21: error: length 10 out of bounds for array of length 5
19// :11:21: error: length 10 out of bounds for array of length 5
test/cases/safety/array slice by-length oversized.zig deleted-21
......@@ -1,21 +0,0 @@
1const std = @import("std");
2
3pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
4 _ = stack_trace;
5 if (std.mem.eql(u8, message, "index out of bounds: index 12, len 5")) {
6 std.process.exit(0);
7 }
8 std.process.exit(1);
9}
10
11pub fn main() !void {
12 var buf: [5]u8 = undefined;
13 var a: u32 = 6;
14 _ = &a;
15 _ = buf[a..][0..a];
16 return error.TestFailed;
17}
18
19// run
20// backend=llvm
21// target=native