7777#endif
7878
7979#define BBF_ESC_CHAR_REPLC "\357\277\277"
80+ #define BBF_ESC_CHAR_REPLC_LEN (sizeof(BBF_ESC_CHAR_REPLC) - 1)
8081
8182static int
8283MatchText (const char * t , int tlen , const char * p , int plen , pg_locale_t locale )
@@ -100,12 +101,12 @@ MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
100101 {
101102 /* Default escape '\\' is disabled in babelfish */
102103 if ((* p == '\\' && sql_dialect == SQL_DIALECT_PG )||
103- (strncmp (p , BBF_ESC_CHAR_REPLC , strlen ( BBF_ESC_CHAR_REPLC )) == 0 && sql_dialect == SQL_DIALECT_TSQL ))
104+ (sql_dialect == SQL_DIALECT_TSQL && strncmp (p , BBF_ESC_CHAR_REPLC , BBF_ESC_CHAR_REPLC_LEN ) == 0 ))
104105 {
105106 /* Next pattern byte must match literally, whatever it is */
106107 if (sql_dialect == SQL_DIALECT_TSQL )
107108 {
108- for (int i = 0 ; i < strlen ( BBF_ESC_CHAR_REPLC ) ; i ++ )
109+ for (int i = 0 ; i < BBF_ESC_CHAR_REPLC_LEN ; i ++ )
109110 {
110111 NextByte (p , plen );
111112 }
@@ -182,6 +183,15 @@ MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
182183 errmsg ("LIKE pattern must not end with escape character" )));
183184 firstpat = GETCHAR (p [1 ], locale );
184185 }
186+ else if (sql_dialect == SQL_DIALECT_TSQL && strncmp (p , BBF_ESC_CHAR_REPLC , BBF_ESC_CHAR_REPLC_LEN ) == 0 )
187+ {
188+ int esc_len = BBF_ESC_CHAR_REPLC_LEN ;
189+ if (plen <= esc_len )
190+ ereport (ERROR ,
191+ (errcode (ERRCODE_INVALID_ESCAPE_SEQUENCE ),
192+ errmsg ("LIKE pattern must not end with escape character" )));
193+ firstpat = GETCHAR (p [esc_len ]);
194+ }
185195 else
186196 firstpat = GETCHAR (* p , locale );
187197
@@ -193,7 +203,7 @@ MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
193203
194204 if (matched != LIKE_FALSE )
195205 return matched ; /* TRUE or ABORT */
196- } else if (firstpat == '[' && sql_dialect == SQL_DIALECT_TSQL )
206+ } else if (sql_dialect == SQL_DIALECT_TSQL && firstpat == '[' )
197207 {
198208 int matched = MatchText (t , tlen , p , plen ,
199209 locale );
@@ -217,7 +227,7 @@ MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
217227 NextByte (p , plen );
218228 continue ;
219229 }
220- else if (* p == '[' && sql_dialect == SQL_DIALECT_TSQL )
230+ else if (sql_dialect == SQL_DIALECT_TSQL && * p == '[' )
221231 {
222232 /* Tsql deal with [ and ] wild character */
223233 Oid cid = InvalidOid ;
@@ -443,7 +453,7 @@ MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
443453 NextByte (p , plen );
444454 }
445455
446- if (tlen > 0 && sql_dialect == SQL_DIALECT_TSQL )
456+ if (sql_dialect == SQL_DIALECT_TSQL && tlen > 0 )
447457 {
448458 /* End of pattern, but not of text.
449459 *
@@ -511,7 +521,7 @@ do_like_escape(text *pat, text *esc)
511521 result = (text * ) palloc (plen * 2 + VARHDRSZ );
512522 r = VARDATA (result );
513523
514- if (elen == 0 && sql_dialect == SQL_DIALECT_TSQL )
524+ if (sql_dialect == SQL_DIALECT_TSQL && elen == 0 )
515525 {
516526 /*
517527 * Escape string is empty, just throw the error.
@@ -561,7 +571,7 @@ do_like_escape(text *pat, text *esc)
561571 {
562572 /* check if direct copy string (unicode char) is valid */
563573
564- for (int i = 0 ; i < strlen ( BBF_ESC_CHAR_REPLC ) ; i ++ ){
574+ for (int i = 0 ; i < BBF_ESC_CHAR_REPLC_LEN ; i ++ ){
565575 * r ++ = BBF_ESC_CHAR_REPLC [i ];
566576 }
567577 NextChar (p , plen );
0 commit comments