Skip to content

Commit d805222

Browse files
msuneclaude
andcommitted
str: reject empty substr in find_*()
Empty substring should be rejected in find_*() API families. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5133913 commit d805222

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Legend:
2424
- [B] set: fix `cdada_set_find()` returning `true` on internal error
2525
- [B] str: fix uint32_t overflow in `cdada_str_erase()` bounds check
2626
- [B] str: fix `cdada_str_find_count()`/`cdada_str_find_all()` not returning `CDADA_E_NOT_FOUND`
27+
- [B] str: reject empty `substr` in `cdada_str_find_count()`/`cdada_str_find_all()`
2728
- [O] tests: add coverage for `cdada_strerr()` valid, boundary and high invalid values
2829
- [O] tests: add `oom_create_test` coverage for create paths (`list`, `map`, `queue`, `set`, `stack`, `str`, `bbitmap`)
2930
- [O] tests: add coverage for `cdada_str_replace_all()` empty-match invalid input and self-overlap replacement

src/str.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int cdada_str_find_count(const cdada_str_t* str, const char* substr,
206206

207207
CDADA_CHECK_MAGIC(m);
208208

209-
if(!substr || !n)
209+
if(!substr || !n || !*substr)
210210
return CDADA_E_INVALID;
211211

212212
std::string& s = *m->str;
@@ -243,7 +243,7 @@ int cdada_str_find_all(const cdada_str_t* str, const char* substr,
243243

244244
CDADA_CHECK_MAGIC(m);
245245

246-
if(!substr || !poss || !size || !cnt)
246+
if(!substr || !poss || !size || !cnt || !*substr)
247247
return CDADA_E_INVALID;
248248

249249
std::string& s = *m->str;

test/str_test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ int test_basics(){
110110
int rv;
111111
cdada_str_t * s;
112112
const char* ptr;
113+
uint32_t count, poss[2];
113114

114115
void* ptr_not_null = (void*)0x123;
115116

@@ -168,6 +169,8 @@ int test_basics(){
168169
rv = cdada_str_find_count(s, (const char*)ptr_not_null,
169170
(uint32_t*)NULL);
170171
TEST_ASSERT(rv == CDADA_E_INVALID);
172+
rv = cdada_str_find_count(s, "", &count);
173+
TEST_ASSERT(rv == CDADA_E_INVALID);
171174

172175
rv = cdada_str_find_all(NULL, (const char*)ptr_not_null, 123,
173176
(uint32_t*)ptr_not_null,
@@ -189,6 +192,8 @@ int test_basics(){
189192
(uint32_t*)ptr_not_null,
190193
(uint32_t*)NULL);
191194
TEST_ASSERT(rv == CDADA_E_INVALID);
195+
rv = cdada_str_find_all(s, "", 2, poss, &count);
196+
TEST_ASSERT(rv == CDADA_E_INVALID);
192197

193198
rv = cdada_str_first_c(NULL, (char*)ptr_not_null);
194199
TEST_ASSERT(rv == CDADA_E_INVALID);

0 commit comments

Comments
 (0)