From a52cae48f46f1418871334fdff2e1bf390fafa36 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 29 Nov 2023 22:05:10 +0100 Subject: [PATCH] Remove obsolete string escaping routines. They have been necessary for the Telnet API, however, this is gone and our JSON functions know how to deal with spaces Signed-off-by: DL6ER --- src/dnsmasq_interface.c | 2 +- src/shmem.c | 90 +++-------------------------------------- src/shmem.h | 11 ----- 3 files changed, 7 insertions(+), 96 deletions(-) diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 90aaf7c5..d77feba5 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -2084,7 +2084,7 @@ static void FTL_reply(const unsigned int flags, const char *name, const union al // else: This is a reply from upstream // Check if this domain matches exactly - const bool isExactMatch = strcmp_escaped(name, getstr(domain->domainpos)); + const bool isExactMatch = strcmp(name, getstr(domain->domainpos)); if((flags & F_CONFIG) && isExactMatch && !query->flags.complete) { diff --git a/src/shmem.c b/src/shmem.c index 415305f6..1ee6b1b4 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -206,73 +206,6 @@ static bool chown_shmem(SharedMemory *sharedMemory, struct passwd *ent_pw) return true; } -// A function that duplicates a string and replaces all characters "s" by "r" -static char *__attribute__ ((malloc)) str_replace(const char *input, - const char s, - const char r, - unsigned int *N) -{ - // Duplicate string - char *copy = strdup(input); - if(!copy) - return NULL; - - // Woring pointer - char *ix = copy; - // Loop over string until there are no further "s" chars in the string - while((ix = strchr(ix, s)) != NULL) - { - *ix++ = r; - (*N)++; - } - - return copy; -} - -char *__attribute__ ((malloc)) str_escape(const char *input, unsigned int *N) -{ - // If no escaping is done, this routine returns the original pointer - // and N stays 0 - *N = 0; - if(strchr(input, ' ') != NULL) - { - // Replace any spaces by ~ if we find them in the domain name - // This is necessary as our telnet API uses space delimiters - return str_replace(input, ' ', '~', N); - } - - return strdup(input); -} - -bool strcmp_escaped(const char *a, const char *b) -{ - unsigned int Na, Nb; - - // Input check - if(a == NULL || b == NULL) - return false; - - // Escape both inputs - char *aa = str_escape(a, &Na); - char *bb = str_escape(b, &Nb); - - // Check for memory errors - if(!aa || !bb) - { - if(aa) free(aa); - if(bb) free(bb); - return false; - } - - const char result = strcasecmp(aa, bb) == 0; - - free(aa); - free(bb); - - return result; -} - - size_t _addstr(const char *input, const char *func, const int line, const char *file) { if(input == NULL) @@ -304,22 +237,12 @@ size_t _addstr(const char *input, const char *func, const int line, const char * len = avail_mem; } - unsigned int N = 0; - char *str = str_escape(input, &N); - - if(N > 0) - log_info("FTL replaced %u invalid characters with ~ in the query \"%s\"", N, str); - // Search buffer for existence of exact same string - char *str_pos = memmem(shm_strings.ptr, shmSettings->next_str_pos, str, len); + char *str_pos = memmem(shm_strings.ptr, shmSettings->next_str_pos, input, len); if(str_pos != NULL) { - log_debug(DEBUG_SHMEM, "Reusing existing string \"%s\" at position %zd in %s() (%s:%i)", - str, str_pos - (char*)shm_strings.ptr, func, short_path(file), line); - - // If the string already exists, we can free the memory allocated - // for the escaped string - free(str); + log_debug(DEBUG_SHMEM, "Reusing existing string \"%s\" at %zd in %s() (%s:%i)", + input, str_pos - (char*)shm_strings.ptr, func, short_path(file), line); // Return position of existing string return (str_pos - (char*)shm_strings.ptr); @@ -327,11 +250,10 @@ size_t _addstr(const char *input, const char *func, const int line, const char * // Debugging output log_debug(DEBUG_SHMEM, "Adding \"%s\" (len %zu) to buffer in %s() (%s:%i), next_str_pos is %u", - str, len, func, short_path(file), line, shmSettings->next_str_pos); + input, len, func, short_path(file), line, shmSettings->next_str_pos); - // Copy the C string pointed by str into the shared string buffer - strncpy(&((char*)shm_strings.ptr)[shmSettings->next_str_pos], str, len); - free(str); + // Copy the C string pointed by input into the shared string buffer + strncpy(&((char*)shm_strings.ptr)[shmSettings->next_str_pos], input, len); // Increment string length counter shmSettings->next_str_pos += len; diff --git a/src/shmem.h b/src/shmem.h index f92de654..8a6ed627 100644 --- a/src/shmem.h +++ b/src/shmem.h @@ -116,17 +116,6 @@ size_t _addstr(const char *str, const char *func, const int line, const char *fi #define getstr(pos) _getstr(pos, __FUNCTION__, __LINE__, __FILE__) const char *_getstr(const size_t pos, const char *func, const int line, const char *file); -/** - * Escapes a string by replacing special characters, such as spaces - * The input string is always duplicated, ensure to free it after use - */ -char *str_escape(const char *input, unsigned int *N) __attribute__ ((malloc)); - -/** - * Compare two strings. Escape them if needed - */ -bool strcmp_escaped(const char *a, const char *b); - /** * Create a new overTime client shared memory block. * This also updates `overTimeClientData`.