From 6fa7d5d4ad02f9694bfabf0ef42f65236c02d1ef Mon Sep 17 00:00:00 2001 From: andrewkraevskii Date: Mon, 10 Feb 2025 02:26:52 +0300 Subject: [PATCH] std.mem: add missing check to lastIndexOfLinear --- lib/std/mem.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 0f56762c27..78cbe77c65 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1396,6 +1396,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize /// Consider using `lastIndexOf` instead of this, which will automatically use a /// more sophisticated algorithm on larger inputs. pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize { + if (needle.len > haystack.len) return null; var i: usize = haystack.len - needle.len; while (true) : (i -= 1) { if (mem.eql(T, haystack[i..][0..needle.len], needle)) return i;