diff --git a/CHANGELOG.md b/CHANGELOG.md index 547da8277..10ee97284 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ SPDX-License-Identifier: GPL-3.0-or-later ### Fixed +- api: fix infinite loop in function string_replace when the search string is empty - irc: limit size of data received from the server to prevent memory exhaustion - relay: limit size of received websocket frame and HTTP body to prevent memory exhaustion diff --git a/src/core/core-string.c b/src/core/core-string.c index 54eef63af..27644d7be 100644 --- a/src/core/core-string.c +++ b/src/core/core-string.c @@ -1965,6 +1965,9 @@ string_replace (const char *string, const char *search, const char *replace) if (!string || !search || !replace) return NULL; + if (!search[0]) + return strdup (string); + length1 = strlen (search); length2 = strlen (replace); diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index 8d633cfaa..6cc98d06c 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -1454,6 +1454,8 @@ TEST(CoreString, Replace) WEE_TEST_STR(NULL, string_replace ("string", NULL, "replace")); WEE_TEST_STR(NULL, string_replace (NULL, "search", "replace")); + WEE_TEST_STR("test abc def", string_replace("test abc def", "", "xxx")); + WEE_TEST_STR("test abc def", string_replace("test abc def", "xyz", "xxx")); WEE_TEST_STR("test xxx def", string_replace("test abc def", "abc", "xxx")); WEE_TEST_STR("xxx test xxx def xxx",