Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert ndpi_strnstr() optimization introduced in a813121e0 #2439

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5691,6 +5691,16 @@ void strnstrUnitTest(void) {

/* Test 10: substring equal to the string */
assert(strcmp(ndpi_strnstr("string", "string", 6), "string") == 0);

/* Test 11a,b: max_length bigger that string length */
assert(strcmp(ndpi_strnstr("string", "string", 66), "string") == 0);
assert(ndpi_strnstr("string", "a", 66) == NULL);

/* Test 12: substring longer than the string */
assert(ndpi_strnstr("string", "stringA", 6) == NULL);

/* Test 13 */
assert(ndpi_strnstr("abcdef", "abc", 2) == NULL);
}

/* *********************************************** */
Expand Down
51 changes: 18 additions & 33 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9687,42 +9687,27 @@ void ndpi_dump_risks_score(FILE *risk_out) {
* first slen characters of s.
*/
char *ndpi_strnstr(const char *s, const char *find, size_t slen) {
if (s == NULL || find == NULL || slen == 0) {
return NULL;
}

char c = *find;

if (c == '\0') {
return (char *)s;
}

if (*(find + 1) == '\0') {
return (char *)memchr(s, c, slen);
}

size_t find_len = strnlen(find, slen);
char c;
size_t len;

if (find_len > slen) {
if(s == NULL || find == NULL || slen == 0)
return NULL;
}

const char *end = s + slen - find_len;

while (s <= end) {
if (memcmp(s, find, find_len) == 0) {
return (char *)s;
}

size_t remaining_length = end - s;
s = (char *)memchr(s + 1, c, remaining_length);

if (s == NULL || s > end) {
return NULL;
}
}

return NULL;
if((c = *find++) != '\0') {
len = strnlen(find, slen);
do {
char sc;

do {
if(slen-- < 1 || (sc = *s++) == '\0')
return(NULL);
} while(sc != c);
if(len > slen)
return(NULL);
} while(strncmp(s, find, len) != 0);
s--;
}
return((char *) s);
}

/* ****************************************************** */
Expand Down
Loading