aboutsummaryrefslogtreecommitdiff
path: root/contrib/expat/tests/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/expat/tests/common.c')
-rw-r--r--contrib/expat/tests/common.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/contrib/expat/tests/common.c b/contrib/expat/tests/common.c
index b158385f56a8..b2537d0deee1 100644
--- a/contrib/expat/tests/common.c
+++ b/contrib/expat/tests/common.c
@@ -303,7 +303,14 @@ duff_reallocator(void *ptr, size_t size) {
return realloc(ptr, size);
}
-// Portable remake of strndup(3) for C99; does not care about space efficiency
+// Portable remake of strnlen(3) for C99
+static size_t
+portable_strnlen(const char *s, size_t maxlen) {
+ const char *const end = (const char *)memchr(s, '\0', maxlen);
+ return (end == NULL) ? maxlen : (size_t)(end - s);
+}
+
+// Portable remake of strndup(3) for C99
char *
portable_strndup(const char *s, size_t n) {
if ((s == NULL) || (n == SIZE_MAX)) {
@@ -311,6 +318,8 @@ portable_strndup(const char *s, size_t n) {
return NULL;
}
+ n = portable_strnlen(s, n);
+
char *const buffer = (char *)malloc(n + 1);
if (buffer == NULL) {
errno = ENOMEM;