summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2017-04-22 15:15:54 +0200
committerSébastien Helleu <flashcode@flashtux.org>2017-04-22 15:15:54 +0200
commit94355e2e38a892e41d04f8405d9bd9bfed71e1f4 (patch)
tree96e5934462e4cc7ab7d6a43608139b64e0fff712
parent572678100b817878c78202b32819ea4597542e33 (diff)
downloadweechat-94355e2e38a892e41d04f8405d9bd9bfed71e1f4.zip
core: ensure length is not negative in function string_strndup
-rw-r--r--src/core/wee-string.c2
-rw-r--r--tests/unit/core/test-string.cpp2
2 files changed, 3 insertions, 1 deletions
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);