aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorVladimir Gavrilov <105977161+0xA50C1A1@users.noreply.github.com>2024-09-25 21:49:23 +0300
committerGitHub <noreply@github.com>2024-09-25 20:49:23 +0200
commit8dd9567a51a1a1e6ba8e55a2f371289792214a26 (patch)
tree42d1d5d8daf3709d08dd29b97d82b347cce2a896 /src/lib
parent276afa0d00a2aa219865db044954d2355b13b93b (diff)
Slightly better ndpi_strrstr implementation (#2570)
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ndpi_utils.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c
index 8282319f9..9b7cc387e 100644
--- a/src/lib/ndpi_utils.c
+++ b/src/lib/ndpi_utils.c
@@ -3465,20 +3465,28 @@ int64_t ndpi_strtonum(const char *numstr, int64_t minval,
/* ****************************************************** */
char* ndpi_strrstr(const char *haystack, const char *needle) {
- char *ret = NULL;
-
- while(true) {
- char *s = strstr(haystack, needle);
-
- if(s == NULL || s[0] == '\0')
+ if (!haystack || !needle) {
+ return NULL;
+ }
+
+ if (*needle == '\0') {
+ return (char*) haystack + strlen(haystack);
+ }
+
+ const char *last_occurrence = NULL;
+
+ while (true) {
+ const char *current_pos = strstr(haystack, needle);
+
+ if (!current_pos) {
break;
- else {
- ret = s;
- haystack = &s[1]; /* Skip the first char */
}
+
+ last_occurrence = current_pos;
+ haystack = current_pos + 1;
}
- return(ret);
+ return (char*) last_occurrence;
}
/* ******************************************* */