vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / string / strlen.cpp
blob9c88a35e75d60a95975daa7c3a80997f4fc2db0d
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
6 #include <string.h>
7 #include <SupportDefs.h>
10 /* From Bit twiddling hacks:
11 http://graphics.stanford.edu/~seander/bithacks.html */
12 #define LACKS_ZERO_BYTE(value) \
13 (((value - 0x01010101) & ~value & 0x80808080) == 0)
16 size_t
17 strlen(const char* string)
19 size_t length = 0;
21 /* Align access for four byte reads */
22 for (; (((addr_t)string + length) & 3) != 0; length++) {
23 if (string[length] == '\0')
24 return length;
27 /* Check four bytes for zero char */
28 uint32* valuePointer = (uint32*)(string + length);
29 for (; LACKS_ZERO_BYTE(*valuePointer); valuePointer++)
32 /* Find the exact length */
33 for (length = ((char*)valuePointer) - string; string[length] != '\0';
34 length++)
37 return length;