|
Lines 23-28
Link Here
|
| 23 |
#include <sys/types.h> |
23 |
#include <sys/types.h> |
| 24 |
|
24 |
|
| 25 |
#include <string.h> |
25 |
#include <string.h> |
|
|
26 |
#include <stdint.h> |
| 26 |
|
27 |
|
| 27 |
size_t |
28 |
size_t |
| 28 |
strnlen(const char *str, size_t maxlen) |
29 |
strnlen(const char *str, size_t maxlen) |
|
Lines 31-37
strnlen(const char *str, size_t maxlen)
Link Here
|
| 31 |
|
32 |
|
| 32 |
for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) |
33 |
for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) |
| 33 |
; |
34 |
; |
| 34 |
|
35 |
/* |
| 35 |
return (size_t)(cp - str); |
36 |
* Cast pointers to unsigned type before calculation, to avoid signed |
|
|
37 |
* overflow when the string ends where the MSB has changed. |
| 38 |
*/ |
| 39 |
return (size_t)((uintptr_t)cp - (uintptr_t)str); |
| 36 |
} |
40 |
} |
| 37 |
#endif |
41 |
#endif |
| 38 |
- |
|
|