Skip to content

Commit 8dd9567

Browse files
authored
Slightly better ndpi_strrstr implementation (#2570)
1 parent 276afa0 commit 8dd9567

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/lib/ndpi_utils.c

+18-10
Original file line numberDiff line numberDiff line change
@@ -3465,20 +3465,28 @@ int64_t ndpi_strtonum(const char *numstr, int64_t minval,
34653465
/* ****************************************************** */
34663466

34673467
char* ndpi_strrstr(const char *haystack, const char *needle) {
3468-
char *ret = NULL;
3469-
3470-
while(true) {
3471-
char *s = strstr(haystack, needle);
3472-
3473-
if(s == NULL || s[0] == '\0')
3468+
if (!haystack || !needle) {
3469+
return NULL;
3470+
}
3471+
3472+
if (*needle == '\0') {
3473+
return (char*) haystack + strlen(haystack);
3474+
}
3475+
3476+
const char *last_occurrence = NULL;
3477+
3478+
while (true) {
3479+
const char *current_pos = strstr(haystack, needle);
3480+
3481+
if (!current_pos) {
34743482
break;
3475-
else {
3476-
ret = s;
3477-
haystack = &s[1]; /* Skip the first char */
34783483
}
3484+
3485+
last_occurrence = current_pos;
3486+
haystack = current_pos + 1;
34793487
}
34803488

3481-
return(ret);
3489+
return (char*) last_occurrence;
34823490
}
34833491

34843492
/* ******************************************* */

0 commit comments

Comments
 (0)