From d0aaa41790214db610d2274eb0f65613c513443f Mon Sep 17 00:00:00 2001 From: Pierre POMES Date: Tue, 27 Aug 2024 16:41:11 -0400 Subject: [PATCH] Handle escaped character for substring rule --- main/myanon.c | 30 +++++++++++++++++++++++++++++- main/myanon.h | 1 + tests/test_substring.sql | 10 +++++----- tests/test_substring_anon.sql | 10 +++++----- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/main/myanon.c b/main/myanon.c index db54754..6796050 100644 --- a/main/myanon.c +++ b/main/myanon.c @@ -77,6 +77,34 @@ char *mystrcpy(char *dest, const char *src, size_t size) return dest; } +char *myescapedstrcpy(char *dest, const char *src, size_t size) +{ + size_t srccount = 0; + size_t dstcount = 0; + short backslash = 0; + + memset(dest, 0, size); + while (src[srccount] != '\0' && dstcount < size - 1) + { + if (src[srccount] == '\\') + { + backslash++; + + if (backslash % 2 == 0) + { + backslash = 0; + dest[dstcount++] = src[srccount++]; + } + } + else + { + dest[dstcount++] = src[srccount++]; + } + } + + return dest; +} + unsigned long get_ts_in_ms() { struct timeval tv; @@ -203,7 +231,7 @@ anonymized_res_st anonymize_token(bool quoted, anon_base_st *config, char *token case AM_SUBSTRING: res_st.len = MIN(worktokenlen, config->len); DEBUG_MSG("%d, %d, %d", worktokenlen, config->len, res_st.len) - mystrcpy((char *)&(res_st.data[0]), worktoken, res_st.len + 1); + myescapedstrcpy((char *)&(res_st.data[0]), worktoken, res_st.len + 1); break; #ifdef HAVE_PYTHON diff --git a/main/myanon.h b/main/myanon.h index d479543..093fa52 100644 --- a/main/myanon.h +++ b/main/myanon.h @@ -201,6 +201,7 @@ EXTERN unsigned long anon_time; /* some safe malloc/strpcy wrappers */ void *mymalloc(size_t size); +char *myescapedstrcpy(char *dest, const char *src, size_t size); char *mystrcpy(char *dest, const char *src, size_t size); /* function to anonymize a single field 'token' which length is 'tokenlen' diff --git a/tests/test_substring.sql b/tests/test_substring.sql index 03d9473..a307531 100644 --- a/tests/test_substring.sql +++ b/tests/test_substring.sql @@ -1,8 +1,8 @@ --- MySQL dump 10.13 Distrib 8.3.0, for macos14.2 (arm64) +-- MySQL dump 10.13 Distrib 8.0.37, for Linux (x86_64) -- --- Host: 127.0.0.1 Database: minimal +-- Host: localhost Database: test_substring -- ------------------------------------------------------ --- Server version 8.3.0 +-- Server version 8.0.37-0ubuntu0.24.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -33,7 +33,7 @@ CREATE TABLE `minimal_table` ( LOCK TABLES `minimal_table` WRITE; /*!40000 ALTER TABLE `minimal_table` DISABLE KEYS */; -INSERT INTO `minimal_table` VALUES ('minimaal zo veel'),('🥳'); +INSERT INTO `minimal_table` VALUES ('minimaal zo veel'),('🥳'),(' test escape'); /*!40000 ALTER TABLE `minimal_table` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -46,4 +46,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2024-08-13 15:47:46 +-- Dump completed on 2024-08-27 14:58:38 diff --git a/tests/test_substring_anon.sql b/tests/test_substring_anon.sql index 3bff1cc..9f0d4de 100644 --- a/tests/test_substring_anon.sql +++ b/tests/test_substring_anon.sql @@ -1,8 +1,8 @@ --- MySQL dump 10.13 Distrib 8.3.0, for macos14.2 (arm64) +-- MySQL dump 10.13 Distrib 8.0.37, for Linux (x86_64) -- --- Host: 127.0.0.1 Database: minimal +-- Host: localhost Database: test_substring -- ------------------------------------------------------ --- Server version 8.3.0 +-- Server version 8.0.37-0ubuntu0.24.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -33,7 +33,7 @@ CREATE TABLE `minimal_table` ( LOCK TABLES `minimal_table` WRITE; /*!40000 ALTER TABLE `minimal_table` DISABLE KEYS */; -INSERT INTO `minimal_table` VALUES ('minim'),('🥳'); +INSERT INTO `minimal_table` VALUES ('minim'),('🥳'),(' test'); /*!40000 ALTER TABLE `minimal_table` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -46,4 +46,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2024-08-13 15:47:46 +-- Dump completed on 2024-08-27 14:58:38