From 42ac53ca7b02a1eb3a9e58b5578ae14dc55b8710 Mon Sep 17 00:00:00 2001 From: Zhibai Song Date: Mon, 22 Jul 2024 14:21:27 -0700 Subject: [PATCH] Fix crash issue in pre transforming column name (#2754) Fix the crash happen when column ref is followed by a comment without space like : 'SELECT table1.c2--table1.REPGETTEXT('. Previously, the column transform didn't consider '--' will directly followed with column ref and will wrongly regard the comment part as the column ref, thus will cause crash later in finding the column name. This fix add the check for '--' at the end of column def in the sql string to avoid this crash. Task: BABEL-5070 Signed-off-by: Zhibai Song --- contrib/babelfishpg_tsql/src/hooks.c | 12 ++++++- test/JDBC/expected/BABEL-4484-vu-verify.out | 40 +++++++++++++++++++++ test/JDBC/input/BABEL-4484-vu-verify.sql | 20 +++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/contrib/babelfishpg_tsql/src/hooks.c b/contrib/babelfishpg_tsql/src/hooks.c index e016f1d971..859e2038e0 100644 --- a/contrib/babelfishpg_tsql/src/hooks.c +++ b/contrib/babelfishpg_tsql/src/hooks.c @@ -1388,7 +1388,17 @@ pre_transform_target_entry(ResTarget *res, ParseState *pstate, colname_start = pstate->p_sourcetext + res->location; last_dot = colname_start; while(*colname_start != '\0') - { + { + /* + * comment follow up with column like : + * + * 'SELECT table1.c2--table1.REPGETTEXT(' + * + * will cause crash if we don't break the searching + * for the last_dot position + */ + if (*colname_start == '-' && *(colname_start+1) == '-') + break; if(open_square_bracket == 0 && *colname_start == '"') { double_quotes++; diff --git a/test/JDBC/expected/BABEL-4484-vu-verify.out b/test/JDBC/expected/BABEL-4484-vu-verify.out index b3961349aa..4ef1b538d3 100644 --- a/test/JDBC/expected/BABEL-4484-vu-verify.out +++ b/test/JDBC/expected/BABEL-4484-vu-verify.out @@ -26,3 +26,43 @@ GO varchar ~~END~~ + +SELECT test_babel_4484_t1.ced--table1.REPGETTEXT( +FROM test_babel_4484_t1 +GO +~~START~~ +varchar +~~END~~ + + +select test_babel_4484_t1.您您--table1.a.b.c +from test_babel_4484_t1 +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: column test_babel_4484_t1.您您 does not exist)~~ + + +select test_babel_4484_t2.您您--table1.a.b.c +from test_babel_4484_t2 +GO +~~START~~ +varchar +~~END~~ + + +select test_babel_4484_t1.您您 as kk--table1.a.b.c +from test_babel_4484_t1 +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: column test_babel_4484_t1.您您 does not exist)~~ + + +select test_babel_4484_t2.您您 as kk--table1.a.b.c +from test_babel_4484_t2 +GO +~~START~~ +varchar +~~END~~ + diff --git a/test/JDBC/input/BABEL-4484-vu-verify.sql b/test/JDBC/input/BABEL-4484-vu-verify.sql index 5c5f6a2e63..474ae42cd3 100644 --- a/test/JDBC/input/BABEL-4484-vu-verify.sql +++ b/test/JDBC/input/BABEL-4484-vu-verify.sql @@ -9,3 +9,23 @@ GO SELECT test_babel_4484_t1.ced FROM test_babel_4484_t1 INNER JOIN test_babel_4484_t2 ON test_babel_4484_t1.ABC = test_babel_4484_t2.ABC WHERE test_babel_4484_t1.ABC = 1; GO + +SELECT test_babel_4484_t1.ced--table1.REPGETTEXT( +FROM test_babel_4484_t1 +GO + +select test_babel_4484_t1.您您--table1.a.b.c +from test_babel_4484_t1 +GO + +select test_babel_4484_t2.您您--table1.a.b.c +from test_babel_4484_t2 +GO + +select test_babel_4484_t1.您您 as kk--table1.a.b.c +from test_babel_4484_t1 +GO + +select test_babel_4484_t2.您您 as kk--table1.a.b.c +from test_babel_4484_t2 +GO \ No newline at end of file