From 781c3a985c2c6e31c57165c02582aa79c286e431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Tue, 19 Dec 2023 21:14:48 +0100 Subject: [PATCH] Prevent reading over a page boundary in `mem.indexOfSentinel` The size of the slice element was not correctly taken into account when determining whether a read would cross a page boundary. --- lib/std/mem.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 8e325e0b8e..a1f23668e5 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -967,14 +967,15 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co // as we don't read into a new page. This should be the case for most architectures // which use paged memory, however should be confirmed before adding a new arch below. .aarch64, .x86, .x86_64 => if (std.simd.suggestVectorSize(T)) |block_len| { - comptime std.debug.assert(std.mem.page_size % block_len == 0); const Block = @Vector(block_len, T); const mask: Block = @splat(sentinel); + comptime std.debug.assert(std.mem.page_size % @sizeOf(Block) == 0); + // First block may be unaligned const start_addr = @intFromPtr(&p[i]); const offset_in_page = start_addr & (std.mem.page_size - 1); - if (offset_in_page < std.mem.page_size - block_len) { + if (offset_in_page < std.mem.page_size - @sizeOf(Block)) { // Will not read past the end of a page, full block. const block: Block = p[i..][0..block_len].*; const matches = block == mask;