From dbc26cacb5d1d7d7353b75a8b70fa09f5bcd5fee Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Wed, 15 Mar 2023 08:09:24 +0300 Subject: [PATCH] Add comment chars escaping --- src/backend/distributed/utils/attribute.c | 51 ++++++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 3a76c682d..69162e372 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -64,8 +64,8 @@ static void MultiTenantMonitorSMInit(void); static int CreateTenantStats(MultiTenantMonitor *monitor, time_t queryTime); static int FindTenantStats(MultiTenantMonitor *monitor); static size_t MultiTenantMonitorshmemSize(void); -static char * extractTopComment(const char *inputString); -static char * get_substring(const char *str, int start, int end); +static char * EscapeCommentChars(const char *str); +static char * UnescapeCommentChars(const char *str); int MultiTenantMonitoringLogLevel = CITUS_LOG_LEVEL_OFF; int CitusStatsTenantsPeriod = (time_t) 60; @@ -726,3 +726,50 @@ get_substring(const char *str, int start, int end) return substr; } + + +/* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */ +static char * +EscapeCommentChars(const char *str) +{ + int len = strlen(str); + char *new_str = (char *) malloc(len * 2 + 1); + int j = 0; + + for (int i = 0; i < len; i++) + { + if (str[i] == '*' || str[i] == '/') + { + new_str[j++] = '\\'; + } + new_str[j++] = str[i]; + } + new_str[j] = '\0'; + + return new_str; +} + + +/* UnescapeCommentChars removes the backslash that precedes '*' or '/' in the input string. */ +static char * +UnescapeCommentChars(const char *str) +{ + int len = strlen(str); + char *new_str = (char *) malloc(len + 1); + int j = 0; + + for (int i = 0; i < len; i++) + { + if (str[i] == '\\' && i < len - 1) + { + if (str[i + 1] == '*' || str[i + 1] == '/') + { + i++; + } + } + new_str[j++] = str[i]; + } + new_str[j] = '\0'; + + return new_str; +}