Skip to content

Commit f522219

Browse files
authored
fix(string): avoid empty-pattern loop (#141)
An empty search pattern never advances either strstr loop. Treat it as a successful no-op, matching the existing no-match behavior. Confidence: high Scope-risk: narrow Tested: official sc_str CMake test with ASan; GCC 13 ASan/UBSan; MinGW GCC 8 Not-tested: MSVC
1 parent d0f6a80 commit f522219

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

‎string/sc_str.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ bool sc_str_replace(char **str, const char *replace, const char *with)
367367
rep_len = strlen(replace);
368368
with_len = strlen(with);
369369

370+
if (rep_len == 0) {
371+
return true;
372+
}
373+
370374
if (rep_len >= UINT32_MAX || with_len >= UINT32_MAX) {
371375
return false;
372376
}

‎string/str_test.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ void test5(void)
459459
assert(strcmp(s1, "longeRlongeR") == 0);
460460
assert(sc_str_replace(&s1, "longeR", ""));
461461
assert(strcmp(s1, "") == 0);
462+
sc_str_set(&s1, "unchanged");
463+
assert(sc_str_replace(&s1, "", "x"));
464+
assert(strcmp(s1, "unchanged") == 0);
465+
assert(sc_str_replace(&s1, "", ""));
466+
assert(strcmp(s1, "unchanged") == 0);
462467
sc_str_destroy(&s1);
463468

464469
s1 = sc_str_create("*test * test*");

0 commit comments

Comments
 (0)