When `maxlen` is large (such as SIZE_MAX), the `end` pointer will overflow, causing strnlen() to incorrectly return 0. To not make the implementation over-complicated, fix the problem by using a counter. BUG=b:359951393 TEST=make unit-tests -j BRANCH=none Change-Id: Ic9d983b11391f5e05c2bceb262682aced5206f94 Signed-off-by: Yu-Ping Wu <yupingso@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/83914 Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-by: Karthik Ramasubramanian <kramasub@google.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Mario Scheithauer <mario.scheithauer@siemens.com>
57 lines
899 B
C
57 lines
899 B
C
/* SPDX-License-Identifier: BSD-3-Clause */
|
|
|
|
#include <commonlib/bsd/string.h>
|
|
#include <ctype.h>
|
|
#include <stddef.h>
|
|
|
|
size_t strlen(const char *str)
|
|
{
|
|
const char *ptr = str;
|
|
|
|
while (*ptr++)
|
|
;
|
|
return ptr - str - 1;
|
|
}
|
|
|
|
size_t strnlen(const char *str, size_t maxlen)
|
|
{
|
|
size_t len = 0;
|
|
while (*str++ && len < maxlen)
|
|
len++;
|
|
return len;
|
|
}
|
|
|
|
char *strcat(char *dst, const char *src)
|
|
{
|
|
char *ptr = dst + strlen(dst);
|
|
|
|
while (*src)
|
|
*ptr++ = *src++;
|
|
|
|
*ptr = '\0';
|
|
return dst;
|
|
}
|
|
|
|
char *strncat(char *dst, const char *src, size_t n)
|
|
{
|
|
char *ptr = dst + strlen(dst);
|
|
|
|
/* Not using strncpy() because '\0' may not be appended. */
|
|
while (n-- > 0 && *src)
|
|
*ptr++ = *src++;
|
|
|
|
*ptr = '\0';
|
|
return dst;
|
|
}
|
|
|
|
unsigned int skip_atoi(char **ptr)
|
|
{
|
|
unsigned int result = 0;
|
|
char *str;
|
|
|
|
for (str = *ptr; isdigit(str[0]); str++)
|
|
result = result * 10 + (str[0] - '0');
|
|
*ptr = str;
|
|
return result;
|
|
}
|