Fix memchr with a zero length.

The memchr implementation for 64 bit fails if these conditions occur:

- The buffer is 32 byte aligned.
- The buffer contains the character in the first byte.
- The count sent in is zero.

The function should return NULL, but it's not.

Bug: 16676625

(cherry picked from commit e03e1eac0b)

Change-Id: Ie4cca2c445127a0936ee2b96651a8e7204fbaffd
This commit is contained in:
Christopher Ferris
2014-07-30 16:06:56 -07:00
parent 4f76469e88
commit 61833de613
2 changed files with 13 additions and 0 deletions

View File

@@ -75,6 +75,7 @@ ENTRY(memchr)
* Magic constant 0x40100401 allows us to identify which lane matches
* the requested byte.
*/
cbz cntin, .Lzero_length
mov wtmp2, #0x0401
movk wtmp2, #0x4010, lsl #16
dup vrepchr.16b, chrin
@@ -157,4 +158,8 @@ ENTRY(memchr)
/* Select result or NULL */
csel result, xzr, result, eq
ret
.Lzero_length:
mov result, xzr
ret
END(memchr)

View File

@@ -763,6 +763,14 @@ TEST(string, memchr) {
}
}
TEST(string, memchr_zero) {
uint8_t* buffer;
ASSERT_EQ(0, posix_memalign(reinterpret_cast<void**>(&buffer), 64, 64));
memset(buffer, 10, 64);
ASSERT_TRUE(NULL == memchr(buffer, 5, 0));
ASSERT_TRUE(NULL == memchr(buffer, 10, 0));
}
TEST(string, memrchr) {
int seek_char = random() & 255;
StringTestState<char> state(SMALL);