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);