From 63f41eb8e9bd01d7673a49de306a3311c1687694 Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Thu, 19 Nov 2026 16:54:33 +0000 Subject: [PATCH v1 2/4] ecpg: Fix placeholder handling in EXECUTE name(...) For EXECUTE name(:var, ...), ecpglib inserts each host variable value into the command text in place of its placeholder, and then searches for the next placeholder. Two things went wrong there: - The search started one character into the value it had just inserted, so a quote or $n in the value was read as part of the command (and so was ?, when ecpg is run with -r questionmarks; the new tests don't use that mode). A bytea value or a character value holding a quoted literal, followed by another value, made the statement fail with "too many arguments", and a $n in a value could also give "too few arguments" or a server error. - Every placeholder was taken to be two characters long, so for $10 and up only "$1" was replaced. The 10th and later values got a stray digit appended: a 7 in the 10th place was sent as 70, without any error. Continue the search after the inserted value, and replace the whole placeholder. Backpatch-through: 15 --- src/interfaces/ecpg/ecpglib/execute.c | 25 ++- .../ecpg/test/expected/sql-prepareas.c | 187 +++++++++++++++++- .../ecpg/test/expected/sql-prepareas.stdout | 3 + src/interfaces/ecpg/test/sql/prepareas.pgc | 40 ++++ 4 files changed, 250 insertions(+), 5 deletions(-) diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c index 2b33984005e..66ef0bdcc9d 100644 --- a/src/interfaces/ecpg/ecpglib/execute.c +++ b/src/interfaces/ecpg/ecpglib/execute.c @@ -1478,6 +1478,9 @@ ecpg_build_params(struct statement *stmt) } else if (stmt->statement_type == ECPGst_exec_with_exprlist) { + int ph_len; + int inserted_len; + if (tobeinserted == NULL) { /* A NULL value; insert the keyword NULL. */ @@ -1503,12 +1506,32 @@ ecpg_build_params(struct statement *stmt) tobeinserted = p; } - if (!insert_tobeinserted(position, 2, stmt, tobeinserted)) + /* + * Replace the whole placeholder: "?", or "$" followed by all of + * its digits ($10 and up have more than one). + */ + ph_len = 1; + if (stmt->command[position - 1] == '$') + { + while (isdigit((unsigned char) stmt->command[position - 1 + ph_len])) + ph_len++; + } + + inserted_len = (int) strlen(tobeinserted); + if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted)) { ecpg_free_params(stmt, false); return false; } tobeinserted = NULL; + + /* + * Continue the search for the next placeholder after the inserted + * value. The value can contain quotes, for example a bytea + * literal, and scanning those again would make next_insert() miss + * the next placeholder. + */ + position += inserted_len - 1; } else { diff --git a/src/interfaces/ecpg/test/expected/sql-prepareas.c b/src/interfaces/ecpg/test/expected/sql-prepareas.c index 057c232b70b..2467d67f198 100644 --- a/src/interfaces/ecpg/test/expected/sql-prepareas.c +++ b/src/interfaces/ecpg/test/expected/sql-prepareas.c @@ -699,17 +699,196 @@ if (sqlca.sqlcode < 0) sqlprint();} #line 206 "prepareas.pgc" + /* + * A value that contains a quote or $n (or ?, with -r questionmarks) must + * not hide the placeholder after it or be taken for one. A bytea value + * is inserted as a quoted literal, and so is a character value that + * already holds one. Placeholders from $10 up must be replaced whole. + * All rows must be stored as given. + */ + { + /* exec sql begin declare section */ + + + +#line 217 "prepareas.pgc" + struct bytea_1 { int len; char arr[ 4 ]; } v_bytea ; + +#line 218 "prepareas.pgc" + char v_literal [ 16 ] ; +/* exec sql end declare section */ +#line 219 "prepareas.pgc" + + + printf("+++++ Test for a quoted value before another value in EXECUTE(exprlist) +++++\n"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test_pos ( a text , b int )", ECPGt_EOIT, ECPGt_EORT); +#line 222 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 222 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( bytea , int ) as insert into test_pos values ( encode ( $1 , 'escape' ) , $2 )", + ECPGt_const,"stmt_pos_b",(long)10,(long)1,strlen("stmt_pos_b"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 223 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 223 "prepareas.pgc" + + v_bytea.len = 2; + memcpy(v_bytea.arr, "ab", 2); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", + ECPGt_const,"stmt_pos_b",(long)10,(long)1,strlen("stmt_pos_b"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_bytea,&(v_bytea),(long)4,(long)1,sizeof(struct bytea_1), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 226 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 226 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( text , int ) as insert into test_pos values ( $1 , $2 )", + ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 227 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 227 "prepareas.pgc" + + strcpy(v_literal, "'cd'"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", + ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_char,(v_literal),(long)16,(long)1,(16)*sizeof(char), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 229 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 229 "prepareas.pgc" + + strcpy(v_literal, "'x$2'"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", + ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_char,(v_literal),(long)16,(long)1,(16)*sizeof(char), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 231 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 231 "prepareas.pgc" + + strcpy(v_literal, "'a?b'"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", + ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_char,(v_literal),(long)16,(long)1,(16)*sizeof(char), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 233 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 233 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_pos where a in ( 'ab' , 'cd' , 'x$2' , 'a?b' ) and b = 2", ECPGt_EOIT, + ECPGt_int,&(ivar1),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT); +#line 234 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 234 "prepareas.pgc" + + printf("rows stored (must be 4) = %d\n", ivar1); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( int , int , int , int , int , int , int , int , int , int , int ) as insert into test_pos values ( concat_ws ( ',' , $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , $9 , $10 ) , $11 )", + ECPGt_const,"stmt_pos_11",(long)11,(long)1,strlen("stmt_pos_11"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 237 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 237 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , $9 , $10 , $11 )", + ECPGt_const,"stmt_pos_11",(long)11,(long)1,strlen("stmt_pos_11"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 239 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 239 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_pos where a = '2,2,2,2,2,2,2,2,2,2' and b = 2", ECPGt_EOIT, + ECPGt_int,&(ivar1),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT); +#line 240 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 240 "prepareas.pgc" + + printf("eleven values stored (must be 1) = %d\n", ivar1); + { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_b"); +#line 242 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 242 "prepareas.pgc" + + { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_t"); +#line 243 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 243 "prepareas.pgc" + + { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_11"); +#line 244 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 244 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_pos", ECPGt_EOIT, ECPGt_EORT); +#line 245 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 245 "prepareas.pgc" + + } + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test", ECPGt_EOIT, ECPGt_EORT); -#line 208 "prepareas.pgc" +#line 248 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 208 "prepareas.pgc" +#line 248 "prepareas.pgc" { ECPGtrans(__LINE__, NULL, "commit work"); -#line 209 "prepareas.pgc" +#line 249 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 209 "prepareas.pgc" +#line 249 "prepareas.pgc" return 0; diff --git a/src/interfaces/ecpg/test/expected/sql-prepareas.stdout b/src/interfaces/ecpg/test/expected/sql-prepareas.stdout index ed8d2c77e5b..ac912227a7c 100644 --- a/src/interfaces/ecpg/test/expected/sql-prepareas.stdout +++ b/src/interfaces/ecpg/test/expected/sql-prepareas.stdout @@ -66,3 +66,6 @@ execute "include_ _name"(:ivar1,:ivar2) 1 2 +++++ Test for a NULL value in EXECUTE(exprlist) +++++ rows with NULL c1 (must be 1) = 1 ++++++ Test for a quoted value before another value in EXECUTE(exprlist) +++++ +rows stored (must be 4) = 4 +eleven values stored (must be 1) = 1 diff --git a/src/interfaces/ecpg/test/sql/prepareas.pgc b/src/interfaces/ecpg/test/sql/prepareas.pgc index 33590f40cd2..2cf4526a65d 100644 --- a/src/interfaces/ecpg/test/sql/prepareas.pgc +++ b/src/interfaces/ecpg/test/sql/prepareas.pgc @@ -205,6 +205,46 @@ int main(void) printf("rows with NULL c1 (must be 1) = %d\n", ivar1); exec sql deallocate stmt_null; + /* + * A value that contains a quote or $n (or ?, with -r questionmarks) must + * not hide the placeholder after it or be taken for one. A bytea value + * is inserted as a quoted literal, and so is a character value that + * already holds one. Placeholders from $10 up must be replaced whole. + * All rows must be stored as given. + */ + { + exec sql begin declare section; + bytea v_bytea[4]; + char v_literal[16]; + exec sql end declare section; + + printf("+++++ Test for a quoted value before another value in EXECUTE(exprlist) +++++\n"); + exec sql create table test_pos (a text, b int); + exec sql prepare stmt_pos_b(bytea,int) as insert into test_pos values(encode($1, 'escape'),$2); + v_bytea.len = 2; + memcpy(v_bytea.arr, "ab", 2); + exec sql execute stmt_pos_b(:v_bytea, :ivar2); + exec sql prepare stmt_pos_t(text,int) as insert into test_pos values($1,$2); + strcpy(v_literal, "'cd'"); + exec sql execute stmt_pos_t(:v_literal, :ivar2); + strcpy(v_literal, "'x$2'"); + exec sql execute stmt_pos_t(:v_literal, :ivar2); + strcpy(v_literal, "'a?b'"); + exec sql execute stmt_pos_t(:v_literal, :ivar2); + exec sql select count(*) into :ivar1 from test_pos where a in ('ab', 'cd', 'x$2', 'a?b') and b = 2; + printf("rows stored (must be 4) = %d\n", ivar1); + exec sql prepare stmt_pos_11(int,int,int,int,int,int,int,int,int,int,int) as + insert into test_pos values(concat_ws(',',$1,$2,$3,$4,$5,$6,$7,$8,$9,$10),$11); + exec sql execute stmt_pos_11(:ivar2, :ivar2, :ivar2, :ivar2, :ivar2, :ivar2, + :ivar2, :ivar2, :ivar2, :ivar2, :ivar2); + exec sql select count(*) into :ivar1 from test_pos where a = '2,2,2,2,2,2,2,2,2,2' and b = 2; + printf("eleven values stored (must be 1) = %d\n", ivar1); + exec sql deallocate stmt_pos_b; + exec sql deallocate stmt_pos_t; + exec sql deallocate stmt_pos_11; + exec sql drop table test_pos; + } + exec sql drop table test; exec sql commit work; -- 2.43.0