From 94355e2e38a892e41d04f8405d9bd9bfed71e1f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 22 Apr 2017 15:15:54 +0200 Subject: [PATCH] core: ensure length is not negative in function string_strndup --- src/core/wee-string.c | 2 +- tests/unit/core/test-string.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 70f595553..2593ed279 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -76,7 +76,7 @@ string_strndup (const char *string, int length) { char *result; - if (!string) + if (!string || (length < 0)) return NULL; if ((int)strlen (string) < length) diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp index 19e7c89c4..a7bc807de 100644 --- a/tests/unit/core/test-string.cpp +++ b/tests/unit/core/test-string.cpp @@ -123,6 +123,8 @@ TEST(String, Duplicate) POINTERS_EQUAL(NULL, string_strndup (NULL, 0)); + POINTERS_EQUAL(NULL, string_strndup (str_test, -1)); + str = string_strndup (str_test, 0); CHECK(str); CHECK(str != str_test);