aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetr <30545094+pasabanov@users.noreply.github.com>2024-07-15 09:35:10 +0300
committerGitHub <noreply@github.com>2024-07-15 08:35:10 +0200
commit2f66a6a3e1829b764d2acabf8745a525c68ccfb6 (patch)
treeb943e49fa5fff82c1d8208989e9d5fede757cfe2 /src
parente059daa0f16f73f27dbdb232ede037d1a43f1fee (diff)
ndpi_memmem: optimized, fixed bug, added tests (#2499)
Diffstat (limited to 'src')
-rw-r--r--src/lib/ndpi_main.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index fb8c113c9..5c1596ba2 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -11437,29 +11437,31 @@ char *ndpi_dump_config(struct ndpi_detection_module_struct *ndpi_str,
void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len)
{
- if (!haystack || !needle || haystack_len < needle_len || needle_len == 0) {
+ if (!haystack || !needle || haystack_len < needle_len) {
return NULL;
}
+ if (needle_len == 0) {
+ return (void *)haystack;
+ }
+
if (needle_len == 1) {
return (void *)memchr(haystack, *(const u_int8_t *)needle, haystack_len);
}
- const u_int8_t *current = (const u_int8_t *)haystack;
- const u_int8_t *haystack_end = (const u_int8_t *)haystack + haystack_len;
+ const u_int8_t *const end_of_search = (const u_int8_t *)haystack + haystack_len - needle_len + 1;
- while (current <= haystack_end - needle_len) {
+ const u_int8_t *current = (const u_int8_t *)haystack;
+ while (current < end_of_search) {
/* Find the first occurrence of the first character from the needle */
- current = (const u_int8_t *)memchr(current, *(const u_int8_t *)needle,
- haystack_end - current);
+ current = (const u_int8_t *)memchr(current, *(const u_int8_t *)needle, end_of_search - current);
if (!current) {
return NULL;
}
/* Check the rest of the needle for a match */
- if ((current + needle_len <= haystack_end) &&
- (memcmp(current, needle, needle_len) == 0)) {
+ if (memcmp(current, needle, needle_len) == 0) {
return (void *)current;
}