summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/memmem.c
diff options
context:
space:
mode:
authortim <>2026-06-23 13:09:11 +0000
committertim <>2026-06-23 13:09:11 +0000
commit2e2e484ec4991ae53dfe018edc48fdfedaf31ec2 (patch)
treeb9eed594c1a596dbd4c719207bd0b761f2da7e51 /src/lib/libc/string/memmem.c
parent75c891e616f0f052332793b0abc0bf917f15048e (diff)
downloadopenbsd-2e2e484ec4991ae53dfe018edc48fdfedaf31ec2.tar.gz
openbsd-2e2e484ec4991ae53dfe018edc48fdfedaf31ec2.tar.bz2
openbsd-2e2e484ec4991ae53dfe018edc48fdfedaf31ec2.zip
Avoid shift overflow in memmem(3) and strstr(3)
Fix from upstream musl: https://git.musl-libc.org/cgit/musl/commit?id=593caa456309714402ca4cb77c3770f4c24da9da OK tb@
Diffstat (limited to 'src/lib/libc/string/memmem.c')
-rw-r--r--src/lib/libc/string/memmem.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/lib/libc/string/memmem.c b/src/lib/libc/string/memmem.c
index 3b180b4a62..e87c7be37a 100644
--- a/src/lib/libc/string/memmem.c
+++ b/src/lib/libc/string/memmem.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */ 1/* $OpenBSD: memmem.c,v 1.6 2026/06/23 13:09:11 tim Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005-2020 Rich Felker, et al. 4 * Copyright (c) 2005-2020 Rich Felker, et al.
@@ -38,8 +38,8 @@ twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
38static char * 38static char *
39threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) 39threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
40{ 40{
41 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; 41 uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8;
42 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; 42 uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8;
43 for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8) 43 for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
44 if (hw == nw) return (char *)h-3; 44 if (hw == nw) return (char *)h-3;
45 return hw == nw ? (char *)h-3 : 0; 45 return hw == nw ? (char *)h-3 : 0;
@@ -48,8 +48,8 @@ threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
48static char * 48static char *
49fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) 49fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
50{ 50{
51 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; 51 uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
52 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; 52 uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
53 for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++) 53 for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
54 if (hw == nw) return (char *)h-4; 54 if (hw == nw) return (char *)h-4;
55 return hw == nw ? (char *)h-4 : 0; 55 return hw == nw ? (char *)h-4 : 0;