Skip to content

Commit

Permalink
Handle escaped character for substring rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ppomes committed Aug 27, 2024
1 parent 55b587a commit d0aaa41
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
30 changes: 29 additions & 1 deletion main/myanon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions main/myanon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 5 additions & 5 deletions tests/test_substring.sql
Original file line number Diff line number Diff line change
@@ -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 */;
Expand Down Expand Up @@ -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 */;
Expand All @@ -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
10 changes: 5 additions & 5 deletions tests/test_substring_anon.sql
Original file line number Diff line number Diff line change
@@ -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 */;
Expand Down Expand Up @@ -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 */;
Expand All @@ -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

0 comments on commit d0aaa41

Please sign in to comment.