summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortim <>2026-06-23 13:09:11 +0000
committertim <>2026-06-23 13:09:11 +0000
commit2e2e484ec4991ae53dfe018edc48fdfedaf31ec2 (patch)
treeb9eed594c1a596dbd4c719207bd0b761f2da7e51
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@
-rw-r--r--src/lib/libc/string/memmem.c10
-rw-r--r--src/lib/libc/string/strstr.c10
2 files changed, 10 insertions, 10 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;
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c
index 241a080e7a..06795d9838 100644
--- a/src/lib/libc/string/strstr.c
+++ b/src/lib/libc/string/strstr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: strstr.c,v 1.9 2020/04/16 12:37:52 claudio Exp $ */ 1/* $OpenBSD: strstr.c,v 1.10 2026/06/23 13:09:11 tim Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005-2018 Rich Felker 4 * Copyright (c) 2005-2018 Rich Felker
@@ -37,8 +37,8 @@ twobyte_strstr(const unsigned char *h, const unsigned char *n)
37static char * 37static char *
38threebyte_strstr(const unsigned char *h, const unsigned char *n) 38threebyte_strstr(const unsigned char *h, const unsigned char *n)
39{ 39{
40 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; 40 uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8;
41 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; 41 uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8;
42 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); 42 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
43 return *h ? (char *)h-2 : 0; 43 return *h ? (char *)h-2 : 0;
44} 44}
@@ -46,8 +46,8 @@ threebyte_strstr(const unsigned char *h, const unsigned char *n)
46static char * 46static char *
47fourbyte_strstr(const unsigned char *h, const unsigned char *n) 47fourbyte_strstr(const unsigned char *h, const unsigned char *n)
48{ 48{
49 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; 49 uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
50 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; 50 uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
51 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); 51 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
52 return *h ? (char *)h-3 : 0; 52 return *h ? (char *)h-3 : 0;
53} 53}