Merge pull request #2015 from MarkLee131/fix/cstring-empty-input-guards

ZNCString: guard Replace/Split against empty-width arguments
This commit is contained in:
Alexey Sokolov
2026-05-04 22:05:49 +01:00
committed by GitHub
2 changed files with 41 additions and 2 deletions
+27
View File
@@ -14,9 +14,13 @@
* limitations under the License.
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <znc/ZNCString.h>
using ::testing::ElementsAre;
using ::testing::IsEmpty;
class EscapeTest : public ::testing::Test {
protected:
void testEncode(const CString& in, const CString& expectedOut,
@@ -128,6 +132,18 @@ TEST(StringTest, Replace) {
EXPECT_EQ(CString("(a()a)").Replace_n("a", "b"), "(b()b)");
EXPECT_EQ(CString("(a()a)").Replace_n("a", "b", "(", ")"), "(a()b)");
EXPECT_EQ(CString("(a()a)").Replace_n("a", "b", "(", ")", true), "a(b)");
// An empty needle must return the input unchanged with a 0 count
// instead of looping forever appending sWith (#2009).
CString sStr = "abc";
EXPECT_EQ(CString::Replace(sStr, "", "X"), 0u);
EXPECT_EQ(sStr, "abc");
sStr = "";
EXPECT_EQ(CString::Replace(sStr, "", "X"), 0u);
EXPECT_EQ(sStr, "");
EXPECT_EQ(CString("abc").Replace_n("", "X"), "abc");
}
TEST(StringTest, Misc) {
@@ -172,6 +188,17 @@ TEST(StringTest, Split) {
CS("a=x&c=d&a=b").URLSplit(mresult);
EXPECT_EQ(mexpected, mresult) << "URLSplit";
// Empty delimiter must not spin in the prefix-skip loop (#2009).
// With nothing to split on, the whole input is returned as a single
// element (or zero elements if the input itself is empty).
VCString vempty;
EXPECT_EQ(CS("abc").Split("", vempty, false), 1u);
EXPECT_THAT(vempty, ElementsAre("abc"));
EXPECT_EQ(CS("abc").Split("", vempty, true), 1u);
EXPECT_THAT(vempty, ElementsAre("abc"));
EXPECT_EQ(CS("").Split("", vempty, false), 0u);
EXPECT_THAT(vempty, IsEmpty());
}
TEST(StringTest, NamedFormat) {