mirror of
https://github.com/opnsense/src.git
synced 2026-06-08 08:12:27 -04:00
lib/libc/amd64/string/memchr.S: fix behaviour with overly long buffers
When memchr(buf, c, len) is called with a phony len (say, SIZE_MAX), buf + len overflows and we have buf + len < buf. This confuses the implementation and makes it return incorrect results. Neverthless we must support this case as memchr() is guaranteed to work even with phony buffer lengths, as long as a match is found before the buffer actually ends. Sponsored by: The FreeBSD Foundation Reported by: yuri, des Tested by: des Approved by: mjg (blanket, via IRC) MFC after: 1 week MFC to: stable/14 PR: 273652
This commit is contained in:
parent
4fc08109fe
commit
b2618b651b
1 changed files with 6 additions and 3 deletions
|
|
@ -44,7 +44,9 @@ ARCHENTRY(__memchr, scalar)
|
||||||
je .Lnomatch
|
je .Lnomatch
|
||||||
|
|
||||||
lea (, %rdi, 8), %ecx
|
lea (, %rdi, 8), %ecx
|
||||||
add %rdi, %rdx # pointer to end of buffer
|
mov $-1, %rax
|
||||||
|
add %rdi, %rdx # pointer to end of buffer or to end of
|
||||||
|
cmovc %rax, %rdx # address space (whichever comes first)
|
||||||
and $~7, %rdi # align to 8 bytes
|
and $~7, %rdi # align to 8 bytes
|
||||||
mov (%rdi), %rax # load first word
|
mov (%rdi), %rax # load first word
|
||||||
movzbl %sil, %esi # clear stray high bits
|
movzbl %sil, %esi # clear stray high bits
|
||||||
|
|
@ -118,14 +120,15 @@ ARCHENTRY(__memchr, baseline)
|
||||||
|
|
||||||
movd %esi, %xmm2
|
movd %esi, %xmm2
|
||||||
mov %edi, %ecx
|
mov %edi, %ecx
|
||||||
add %rdi, %rdx # pointer to end of buffer
|
mov $-1, %r9
|
||||||
|
add %rdi, %rdx # pointer to end of buffer or to end of
|
||||||
|
cmovc %r9, %rdx # address space (whichever comes first)
|
||||||
and $~0x1f, %rdi # align to 32 bytes
|
and $~0x1f, %rdi # align to 32 bytes
|
||||||
movdqa (%rdi), %xmm0 # load first 32 bytes
|
movdqa (%rdi), %xmm0 # load first 32 bytes
|
||||||
movdqa 16(%rdi), %xmm1
|
movdqa 16(%rdi), %xmm1
|
||||||
|
|
||||||
punpcklbw %xmm2, %xmm2 # c -> cc
|
punpcklbw %xmm2, %xmm2 # c -> cc
|
||||||
|
|
||||||
mov $-1, %r9d
|
|
||||||
shl %cl, %r9d # mask with zeroes before the string
|
shl %cl, %r9d # mask with zeroes before the string
|
||||||
|
|
||||||
punpcklwd %xmm2, %xmm2 # cc -> cccc
|
punpcklwd %xmm2, %xmm2 # cc -> cccc
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue