From 1b6267ff86348798a2138fc8f55f6c6639fe1512 Mon Sep 17 00:00:00 2001 From: SashaXser <24498484+SashaXser@users.noreply.github.com> Date: Sun, 2 Jun 2024 00:19:41 +0400 Subject: [PATCH] Update goodbyedpi.c --- src/goodbyedpi.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/goodbyedpi.c b/src/goodbyedpi.c index ade82080..c3f8def2 100644 --- a/src/goodbyedpi.c +++ b/src/goodbyedpi.c @@ -185,17 +185,14 @@ static void add_filter_str(int proto, int port) { const char *tcp = " or (tcp and !impostor and !loopback " MAXPAYLOADSIZE_TEMPLATE " and " \ "(tcp.SrcPort == %d or tcp.DstPort == %d))"; - char *current_filter = filter_string; - size_t current_filter_length = strlen(current_filter); - size_t new_filter_size = current_filter_length + - (proto == IPPROTO_UDP ? strlen(udp) : strlen(tcp)) + 16; - char *new_filter = malloc(new_filter_size); + const char *template = proto == IPPROTO_UDP ? udp : tcp; + size_t needed_size = strlen(filter_string) + strlen(template) + 16; - snprintf(new_filter, new_filter_size, "%s%s", current_filter, - proto == IPPROTO_UDP ? udp : tcp, port, port); + if (needed_size > strlen(filter_string)) { + filter_string = realloc(filter_string, needed_size); + } - filter_string = new_filter; - free(current_filter); + snprintf(filter_string + strlen(filter_string), needed_size, template, port, port); } static void add_ip_id_str(int id) { @@ -224,7 +221,6 @@ static void add_maxpayloadsize_str(unsigned short maxpayload) { free(addfilter); } - static void finalize_filter_strings() { char *newstr = repl_str(filter_string, IPID_TEMPLATE, ""); free(filter_string); @@ -238,17 +234,13 @@ static void finalize_filter_strings() { static const char* dumb_memmem(const char* haystack, unsigned int hlen, const char* needle, unsigned int nlen) { - // naive implementation + if (nlen == 0) return haystack; + if (nlen > hlen) return NULL; - size_t i; - for (i=0; i