pg.ddx.io pgsql-odbc@postgresql.org mailing list archive
help / color / mirror / Atom feedSQL_CURSOR_TYPE prepare execute issue
10+ messages / 3 participants
[nested] [flat]
* SQL_CURSOR_TYPE prepare execute issue
@ 2015-01-13 17:57 Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
0 siblings, 1 reply; 10+ messages in thread
From: Faith, Jeremy @ 2015-01-13 17:57 UTC (permalink / raw)
To: pgsql-odbc
Hi,
With PosgreSQL 9.3.5 and pgsqlodbc 9.03.03
I have found an issue with setting cursor type to anything other than the default(SQL_CURSOR_FORWARD_ONLY) and the checking done on prepare/execute parameters.
In particular with regards to protection from SQL injection attacks.
Say for example there is a table
create table t1(c1 integer);
When following is run
SQLSMALLINT dec_digits;
SQLUINTEGER col_size;
SQLHSTMT stmt;
char buf[100];
//... not showing con setup
SQLAllocHandle(SQL_HANDLE_STMT,con->sqlcon,&stmt);
SQLSetStmtOption(stmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
SQLPrepare(stmt,(SQLCHAR *)"select * from t1 where c1=?",SQL_NTS);
sprintf(buf,"%s","1 and zzz='dummy'"); //i.e. injection attempt
//this could be much worse e.g. "1; drop table personnel;"
len=strlen(buf);
col_size=10;
dec_digits=-1;
SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_INTEGER,col_size,
dec_digits,buf,0,&len);
SQLExecute(stmt);
The SQLExecute will fail(as it should) but with the cursor set to SQL_CURSOR_STATIC then the error is:-
stmt native_err=7 [42703]ERROR: column "zzz" does not exist;
Whereas if the cursor type is not set(and is thus the default SQL_CURSOR_FORWARD_ONLY) then the error is much better(and safer):-
stmt native_err=7 [22P02]ERROR: invalid input syntax for integer: "1 and zzz='dummy' ";
Also if in the odbc.ini file
UseServerSidePrepare = 0
for the connection then error is always: column "zzz" does not exist, regardless of the cursor type.
I found this problem when testing with PHP odbc_prepare/odbc_execute functions as the odbc_prepare function sets the cursor type to SQL_CURSOR_STATIC(by default though it can be overridden by setting odbc.default_cursortype = SQL_CURSOR_FORWARD_ONLY in php.ini). PHP needs UseServerSidePrepare=1 as it also calls SQLDescribeParam() which fails if UseServerSidePrepare=0.
The question is why does changing the cursor type change the way the parameters are bound?
Can anything be done to fix this?
Regards,
Jeremy Faith
Tyco Safety Products/CEM Systems Ltd.
________________________________
This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments.
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
@ 2015-01-13 23:00 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
0 siblings, 1 reply; 10+ messages in thread
From: Heikki Linnakangas @ 2015-01-13 23:00 UTC (permalink / raw)
To: Faith, Jeremy <jfaith@tycoint.com>; pgsql-odbc
On 01/13/2015 07:57 PM, Faith, Jeremy wrote:
> Hi,
>
> With PosgreSQL 9.3.5 and pgsqlodbc 9.03.03
>
> I have found an issue with setting cursor type to anything other than the default(SQL_CURSOR_FORWARD_ONLY) and the checking done on prepare/execute parameters.
>
> In particular with regards to protection from SQL injection attacks.
>
> Say for example there is a table
> create table t1(c1 integer);
>
> When following is run
> SQLSMALLINT dec_digits;
> SQLUINTEGER col_size;
> SQLHSTMT stmt;
> char buf[100];
>
> //... not showing con setup
> SQLAllocHandle(SQL_HANDLE_STMT,con->sqlcon,&stmt);
>
> SQLSetStmtOption(stmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
> SQLPrepare(stmt,(SQLCHAR *)"select * from t1 where c1=?",SQL_NTS);
>
> sprintf(buf,"%s","1 and zzz='dummy'"); //i.e. injection attempt
> //this could be much worse e.g. "1; drop table personnel;"
> len=strlen(buf);
> col_size=10;
> dec_digits=-1;
> SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_INTEGER,col_size,
> dec_digits,buf,0,&len);
>
> SQLExecute(stmt);
>
> The SQLExecute will fail(as it should) but with the cursor set to SQL_CURSOR_STATIC then the error is:-
> stmt native_err=7 [42703]ERROR: column "zzz" does not exist;
>
> Whereas if the cursor type is not set(and is thus the default SQL_CURSOR_FORWARD_ONLY) then the error is much better(and safer):-
> stmt native_err=7 [22P02]ERROR: invalid input syntax for integer: "1 and zzz='dummy' ";
>
> Also if in the odbc.ini file
> UseServerSidePrepare = 0
> for the connection then error is always: column "zzz" does not exist, regardless of the cursor type.
Hmm. The driver blindly assumes that if parameter is bound as
SQL_INTEGER or SQL_SMALLINT, it doesn't require quoting. But clearly
that's not true, if the passed parameter string is not a valid integer.
> I found this problem when testing with PHP odbc_prepare/odbc_execute
> functions as the odbc_prepare function sets the cursor type to
> SQL_CURSOR_STATIC(by default though it can be overridden by setting
> odbc.default_cursortype = SQL_CURSOR_FORWARD_ONLY in php.ini). PHP
> needs UseServerSidePrepare=1 as it also calls SQLDescribeParam()
> which fails if UseServerSidePrepare=0.
>
> The question is why does changing the cursor type change the way the
> parameters are bound? Can anything be done to fix this?
I just pushed a fix and a regression test to the git repository. Thanks
for the report!
- Heikki
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-14 16:56 ` Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
0 siblings, 1 reply; 10+ messages in thread
From: Faith, Jeremy @ 2015-01-14 16:56 UTC (permalink / raw)
To: Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-odbc
Hi,
Thanks for the fix.
I think the test code should call
SQLSetStmtOption(hstmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
after SQLAllocHandle. As otherwise even with the 9.03.03 version of the ODBC driver the correct error occurs unless UseServerSidePrepare=0 in the odbc.ini file. That is I am not sure the changed code is being used by the test as it is.
I have had a quick look over the change and it looks ok to me. Something of a clean up and simplification as well.
If I understand it correctly, the only things that don't get quoted are SQL_INTEGER and SQL_SMALLINT that pass the new valid_int_literal() test.
The only thing I can see that could pass that test and not be a valid integer would be a single minus char i.e. "-"
not sure if there is anyway that could be vulnerable though.
Any idea when a new release of the driver is likely?
I know a big change to use libpq has occurred recently so I guess it could be a while.
Regards,
Jeremy Faith
________________________________________
From: Heikki Linnakangas [hlinnakangas@vmware.com]
Sent: 13 January 2015 23:00
To: Faith, Jeremy; pgsql-odbc@postgresql.org
Subject: Re: [ODBC] SQL_CURSOR_TYPE prepare execute issue
On 01/13/2015 07:57 PM, Faith, Jeremy wrote:
> Hi,
>
> With PosgreSQL 9.3.5 and pgsqlodbc 9.03.03
>
> I have found an issue with setting cursor type to anything other than the default(SQL_CURSOR_FORWARD_ONLY) and the checking done on prepare/execute parameters.
>
> In particular with regards to protection from SQL injection attacks.
>
> Say for example there is a table
> create table t1(c1 integer);
>
> When following is run
> SQLSMALLINT dec_digits;
> SQLUINTEGER col_size;
> SQLHSTMT stmt;
> char buf[100];
>
> //... not showing con setup
> SQLAllocHandle(SQL_HANDLE_STMT,con->sqlcon,&stmt);
>
> SQLSetStmtOption(stmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
> SQLPrepare(stmt,(SQLCHAR *)"select * from t1 where c1=?",SQL_NTS);
>
> sprintf(buf,"%s","1 and zzz='dummy'"); //i.e. injection attempt
> //this could be much worse e.g. "1; drop table personnel;"
> len=strlen(buf);
> col_size=10;
> dec_digits=-1;
> SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_INTEGER,col_size,
> dec_digits,buf,0,&len);
>
> SQLExecute(stmt);
>
> The SQLExecute will fail(as it should) but with the cursor set to SQL_CURSOR_STATIC then the error is:-
> stmt native_err=7 [42703]ERROR: column "zzz" does not exist;
>
> Whereas if the cursor type is not set(and is thus the default SQL_CURSOR_FORWARD_ONLY) then the error is much better(and safer):-
> stmt native_err=7 [22P02]ERROR: invalid input syntax for integer: "1 and zzz='dummy' ";
>
> Also if in the odbc.ini file
> UseServerSidePrepare = 0
> for the connection then error is always: column "zzz" does not exist, regardless of the cursor type.
Hmm. The driver blindly assumes that if parameter is bound as
SQL_INTEGER or SQL_SMALLINT, it doesn't require quoting. But clearly
that's not true, if the passed parameter string is not a valid integer.
> I found this problem when testing with PHP odbc_prepare/odbc_execute
> functions as the odbc_prepare function sets the cursor type to
> SQL_CURSOR_STATIC(by default though it can be overridden by setting
> odbc.default_cursortype = SQL_CURSOR_FORWARD_ONLY in php.ini). PHP
> needs UseServerSidePrepare=1 as it also calls SQLDescribeParam()
> which fails if UseServerSidePrepare=0.
>
> The question is why does changing the cursor type change the way the
> parameters are bound? Can anything be done to fix this?
I just pushed a fix and a regression test to the git repository. Thanks
for the report!
- Heikki
Tyco Safety Products/CEM Systems Ltd.
________________________________
This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments.
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
@ 2015-01-15 10:52 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 12:22 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 16:02 ` Re: SQL_CURSOR_TYPE prepare execute issue Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 2 replies; 10+ messages in thread
From: Heikki Linnakangas @ 2015-01-15 10:52 UTC (permalink / raw)
To: Faith, Jeremy <jfaith@tycoint.com>; pgsql-odbc
On 01/14/2015 06:56 PM, Faith, Jeremy wrote:
> Hi,
>
> Thanks for the fix.
>
> I think the test code should call
> SQLSetStmtOption(hstmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
> after SQLAllocHandle. As otherwise even with the 9.03.03 version of the ODBC driver the correct error occurs unless UseServerSidePrepare=0 in the odbc.ini file. That is I am not sure the changed code is being used by the test as it is.
It is common practice to run the test suite with different settings. At
least I do it, I'm not sure of others, though :-). I created a makefile
target "installcheck-all" specifically for that.
There are some subtle differences in the behaviour with
UserServerSidePrepare=0/1 in what datatype the server chooses for the
parameters, so I'd like to continue testing both and not force the
non-server-side codepath with SQLSetStmtOption.
> I have had a quick look over the change and it looks ok to me. Something of a clean up and simplification as well.
> If I understand it correctly, the only things that don't get quoted are SQL_INTEGER and SQL_SMALLINT that pass the new valid_int_literal() test.
> The only thing I can see that could pass that test and not be a valid integer would be a single minus char i.e. "-"
> not sure if there is anyway that could be vulnerable though.
Ah, good catch. That is definitely a problem. Consider:
SELECT * FROM foo WHERE 1-? > 0
If you replace ? with -, it becomes "--", which comments out the rest of
the query. That's actually a problem with any negative number.
It would be tempting to just always quote the value, but that again
would lead to subtle changes in the datatype that the server chooses.
There is a difference between:
SELECT '555' > '6';
and
SELECT '555' > 6;
We'd have to force the datatype with something like:
SELECT '555' > '6'::int4;
Perhaps that's the safest thing to do. It might cause subtle changes in
behaviour too, if e.g. you pass a number larger than what fits in an
int4. The server will upgrade it to an int8 or numeric constant if it's
just a numeric literal, but if you force it to int4, you'll get an
error. Now, you can certainly argue that that's a good thing, as you
shouldn't have specified SQL_INTEGER for an over-sized value in the
first place. So perhaps we should just bite the bullet and do that.
Another argument is that '6'::int4 looks more ugly than just 6. Not a
big deal, but still.
Another option is to use parens around a negative number. So it would
become:
SELECT '555' > (-6)
or
SELECT * FROM foo WHERE 1-(-6) > 0
That would be closest to the current behaviour. Does anyone see a
problem with that?
- Heikki
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-15 12:22 ` Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 15:58 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
1 sibling, 1 reply; 10+ messages in thread
From: Faith, Jeremy @ 2015-01-15 12:22 UTC (permalink / raw)
To: Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-odbc
Hi,
As long as you are confident that the test suite is checking this code path that is fine, I just wanted to be sure that was happening.
I hadn't thought of
select x-?
You are right that is a problem.
What does the server prepare code path do?
I did a quick check, sever side prepare, with the log turned on and it shows the following
LOG: execute <unnamed>: select 1-$1
DETAIL: parameters: $1 = '-1'
LOG: execute <unnamed>: select 1-$1
DETAIL: parameters: $1 = '1'
But if the bound string is '-1' then I get
ERROR: invalid input syntax for integer: "'-1'" i.e. double quote,single quote,-,1,single quote,double quote
STATEMENT: select 'wibble',1-$1
Also get invalid syntax error if it is just a minus sign.
ok, server side prepare log when string is just 6 shows
LOG: execute <unnamed>: select '555'>$1
DETAIL: parameters: $1 = '6'
and this return one row with a value of 0
so this is doing select '555'>'6';
So to be consistent with what the server side prepare does, I think it should quote the value.
You should check this as I may have been getting confused.
I realise this may have some implications for existing programs but given that sometimes it will be server side prepare and sometimes client side it is likely to cause more issues if the results are different.
PS. Sorry about top posting. Outlook Web App provides no other option.
Regards,
Jeremy
________________________________________
From: Heikki Linnakangas [hlinnakangas@vmware.com]
Sent: 15 January 2015 10:52
To: Faith, Jeremy; pgsql-odbc@postgresql.org
Subject: Re: [ODBC] SQL_CURSOR_TYPE prepare execute issue
On 01/14/2015 06:56 PM, Faith, Jeremy wrote:
> Hi,
>
> Thanks for the fix.
>
> I think the test code should call
> SQLSetStmtOption(hstmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
> after SQLAllocHandle. As otherwise even with the 9.03.03 version of the ODBC driver the correct error occurs unless UseServerSidePrepare=0 in the odbc.ini file. That is I am not sure the changed code is being used by the test as it is.
It is common practice to run the test suite with different settings. At
least I do it, I'm not sure of others, though :-). I created a makefile
target "installcheck-all" specifically for that.
There are some subtle differences in the behaviour with
UserServerSidePrepare=0/1 in what datatype the server chooses for the
parameters, so I'd like to continue testing both and not force the
non-server-side codepath with SQLSetStmtOption.
> I have had a quick look over the change and it looks ok to me. Something of a clean up and simplification as well.
> If I understand it correctly, the only things that don't get quoted are SQL_INTEGER and SQL_SMALLINT that pass the new valid_int_literal() test.
> The only thing I can see that could pass that test and not be a valid integer would be a single minus char i.e. "-"
> not sure if there is anyway that could be vulnerable though.
Ah, good catch. That is definitely a problem. Consider:
SELECT * FROM foo WHERE 1-? > 0
If you replace ? with -, it becomes "--", which comments out the rest of
the query. That's actually a problem with any negative number.
It would be tempting to just always quote the value, but that again
would lead to subtle changes in the datatype that the server chooses.
There is a difference between:
SELECT '555' > '6';
and
SELECT '555' > 6;
We'd have to force the datatype with something like:
SELECT '555' > '6'::int4;
Perhaps that's the safest thing to do. It might cause subtle changes in
behaviour too, if e.g. you pass a number larger than what fits in an
int4. The server will upgrade it to an int8 or numeric constant if it's
just a numeric literal, but if you force it to int4, you'll get an
error. Now, you can certainly argue that that's a good thing, as you
shouldn't have specified SQL_INTEGER for an over-sized value in the
first place. So perhaps we should just bite the bullet and do that.
Another argument is that '6'::int4 looks more ugly than just 6. Not a
big deal, but still.
Another option is to use parens around a negative number. So it would
become:
SELECT '555' > (-6)
or
SELECT * FROM foo WHERE 1-(-6) > 0
That would be closest to the current behaviour. Does anyone see a
problem with that?
- Heikki
Tyco Safety Products/CEM Systems Ltd.
________________________________
This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments.
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 12:22 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
@ 2015-01-15 15:58 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-16 19:21 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
0 siblings, 1 reply; 10+ messages in thread
From: Heikki Linnakangas @ 2015-01-15 15:58 UTC (permalink / raw)
To: Faith, Jeremy <jfaith@tycoint.com>; pgsql-odbc
On 01/15/2015 02:22 PM, Faith, Jeremy wrote:
> As long as you are confident that the test suite is checking this code path that is fine, I just wanted to be sure that was happening.
Sure, thanks!
> I hadn't thought of
> select x-?
> You are right that is a problem.
> What does the server prepare code path do?
> I did a quick check, sever side prepare, with the log turned on and it shows the following
> LOG: execute <unnamed>: select 1-$1
> DETAIL: parameters: $1 = '-1'
> LOG: execute <unnamed>: select 1-$1
> DETAIL: parameters: $1 = '1'
> But if the bound string is '-1' then I get
> ERROR: invalid input syntax for integer: "'-1'" i.e. double quote,single quote,-,1,single quote,double quote
> STATEMENT: select 'wibble',1-$1
Huh. I cannot reproduce that. Can you modify the
test/src/param-conversions-test.c test case to demonstrate it?
> Also get invalid syntax error if it is just a minus sign.
>
> ok, server side prepare log when string is just 6 shows
> LOG: execute <unnamed>: select '555'>$1
> DETAIL: parameters: $1 = '6'
> and this return one row with a value of 0
> so this is doing select '555'>'6';
>
> So to be consistent with what the server side prepare does, I think it should quote the value.
> You should check this as I may have been getting confused.
> I realise this may have some implications for existing programs but given that sometimes it will be server side prepare and sometimes client side it is likely to cause more issues if the results are different.
Ugh, I didn't realize that happens with UseServerSidePrepare=1. But
looking at the code, it's quite obvious that it does; we never send the
datatypes with server-side parameters, so the server always treats them
as "unknown", that is, the same as a plain string literal. That usually
works fine, as the server deduces the right datatype from the context,
but for ambiguous cases like above, it goes wrong.
I think it's pretty clear that the UseServerSidePrepare=1 behaviour is
wrong in this case. In particular, if you bind a parameter as
SQL_INTEGER, and run query "SELECT '555' > ?", the argument should be
treated as an integer and the answer should be "true".
I propose the attached two patches. The first one fixes the "-" case and
negative integers, with UseServerSidePrepare=0. The second one changes
the UseServerSidePrepare=1 behaviour so that a datatype is sent along
with each parameter. String-like datatypes, SQL_CHAR, SQL_WCHAR,
SQL_VARCHAR etc. are still sent as "unknown". It makes sense for string
types IMHO.
Thoughts?
- Heikki
h
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
Attachments: download all (2 files, .tar.gz)
0001-More-fixes-to-building-query-with-SQL_INTEGER-params.patch (text/x-diff, 8.5K) download
From c45ea69fd3841c4cb223be57587e055b878e16e5 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 15 Jan 2015 13:25:17 +0200
Subject: [PATCH 1/2] More fixes to building query with SQL_INTEGER params.
Jeremy Faith pointed out that the code still incorrectly accepted '-' as
a valid integer. But a bigger issue with negative numbers is that even a
valid negative integer can lead to bogus queries. Consider "SELECT 0-?",
where the parameter is -123. It was expanded to "SELECT 0--123".
To fix, don't accept '-' as a valid integer, forcing it to be quoted. And
if the value is negative, put parens around it, so that the previous
example becomes "SELECT 0-(-123)".
Add test cases for these issues.
---
convert.c | 127 +++++++++++++++++++++-------------
test/expected/param-conversions.out | 9 +++
test/expected/param-conversions_1.out | 9 +++
test/src/param-conversions-test.c | 22 ++++--
4 files changed, 113 insertions(+), 54 deletions(-)
diff --git a/convert.c b/convert.c
index cf8a16e..7cdf135 100644
--- a/convert.c
+++ b/convert.c
@@ -707,25 +707,34 @@ copy_and_convert_field_bindinfo(StatementClass *stmt, OID field_type, int atttyp
* Is 'str' a valid integer literal, consisting only of ASCII characters
* 0-9 ?
*
+ * Also, *negative is set to TRUE if the value was negative.
+ *
* We don't check for overflow here. This is just to decide if we need to
* quote the value.
*/
static BOOL
-valid_int_literal(const char *str, SQLLEN len)
+valid_int_literal(const char *str, SQLLEN len, BOOL *negative)
{
SQLLEN i;
i = 0;
if (str[0] == '-')
+ {
i++;
+ *negative = TRUE;
+ }
+ else
+ *negative = FALSE;
+
+ /* Must begin with a digit. This rejects empty strings, too */
+ if (!(str[i] >= '0' && str[i] <= '9'))
+ return FALSE;
for (; str[i] && (len == SQL_NTS || i < len); i++)
{
- if (str[i] >= '0' && str[i] <= '9')
- continue;
-
- return FALSE;
+ if (!(str[i] >= '0' && str[i] <= '9'))
+ return FALSE;
}
return TRUE;
@@ -3885,7 +3894,10 @@ ResolveOneParam(QueryBuild *qb, QueryParse *qp, BOOL *isnull, BOOL *isbinary)
int lobj_fd;
SQLULEN offset = apdopts->param_offset_ptr ? *apdopts->param_offset_ptr : 0;
size_t current_row = qb->current_row;
- BOOL handling_large_object = FALSE, req_bind, add_quote = FALSE;
+ BOOL handling_large_object = FALSE, req_bind;
+ BOOL need_quotes = TRUE;
+ BOOL add_parens = FALSE;
+ BOOL negative;
ParameterInfoClass *apara;
ParameterImplClass *ipara;
BOOL outputDiscard,
@@ -4634,63 +4646,84 @@ mylog("cvt_null_date_string=%d pgtype=%d buf=%p\n", conn->connInfo.cvt_null_date
break;
case SQL_NUMERIC:
break;
+ /*
+ * If it looks like a valid integer, we can pass it without quotes
+ * and let the server interpret it. Arguably, it would always be
+ * better to explicitly pass it as 'xxx'::integer or 'xxx'::smallint,
+ * but historically we haven't done that, so let's avoid changing the
+ * behaviour.
+ *
+ * If it's a negative number, we have to wrap it in parens. Otherwise
+ * a query like "SELECT 0-?" would turn into "SELECT 0--123".
+ */
+ case SQL_INTEGER:
+ if (valid_int_literal(buf, used, &negative))
+ {
+ need_quotes = FALSE;
+ add_parens = negative;
+ }
+ else
+ {
+ /*
+ * Doesn't look like a valid integer. The server will most
+ * likely throw an error, unless it's in some format we don't
+ * recognize but the server does.
+ */
+ lastadd = "::int4";
+ }
+ break;
+ case SQL_SMALLINT:
+ if (valid_int_literal(buf, used, &negative))
+ {
+ need_quotes = FALSE;
+ add_parens = negative;
+ }
+ else
+ lastadd = "::smallint";
+ break;
default: /* a numeric type or SQL_BIT */
break;
}
+ if (used == SQL_NTS)
+ used = strlen(buf);
+
/*
* Ok, we now have the final string representation in 'buf', length 'used'.
+ * We're ready to output the final string, with quotes and other
+ * embellishments if necessary.
*
- * Do we need to escape it?
+ * In bind-mode, we don't need to do any quoting.
*/
- if (!req_bind)
+ if (req_bind)
+ CVT_APPEND_DATA(qb, buf, used);
+ else
{
- switch (param_sqltype)
+ if (add_parens)
+ CVT_APPEND_CHAR(qb, '(');
+
+ if (need_quotes)
{
- case SQL_INTEGER:
- case SQL_SMALLINT:
- if (valid_int_literal(buf, used))
- break;
- /* fall through */
- case SQL_CHAR:
- case SQL_VARCHAR:
- case SQL_LONGVARCHAR:
- case SQL_BINARY:
- case SQL_VARBINARY:
- case SQL_LONGVARBINARY:
-#ifdef UNICODE_SUPPORT
- case SQL_WCHAR:
- case SQL_WVARCHAR:
- case SQL_WLONGVARCHAR:
-#endif /* UNICODE_SUPPORT */
-mylog("buf=%p flag=%d\n", buf, qb->flags);
- default:
- if ((qb->flags & FLGB_LITERAL_EXTENSION) != 0)
- CVT_APPEND_CHAR(qb, LITERAL_EXT);
- CVT_APPEND_CHAR(qb, LITERAL_QUOTE);
- add_quote = TRUE;
- break;
- }
- }
+ if ((qb->flags & FLGB_LITERAL_EXTENSION) != 0)
+ CVT_APPEND_CHAR(qb, LITERAL_EXT);
+ CVT_APPEND_CHAR(qb, LITERAL_QUOTE);
- if (used == SQL_NTS)
- used = strlen(buf);
- if (add_quote)
- {
- if (final_binary_convert)
- CVT_APPEND_BINARY(qb, buf, used);
+ if (final_binary_convert)
+ CVT_APPEND_BINARY(qb, buf, used);
+ else
+ CVT_SPECIAL_CHARS(qb, buf, used);
+
+ CVT_APPEND_CHAR(qb, LITERAL_QUOTE);
+ }
else
- CVT_SPECIAL_CHARS(qb, buf, used);
- }
- else
- CVT_APPEND_DATA(qb, buf, used);
+ CVT_APPEND_DATA(qb, buf, used);
- if (add_quote)
- {
- CVT_APPEND_CHAR(qb, LITERAL_QUOTE);
+ if (add_parens)
+ CVT_APPEND_CHAR(qb, ')');
if (lastadd)
CVT_APPEND_STR(qb, lastadd);
}
+
retval = SQL_SUCCESS;
cleanup:
if (allocbuf)
diff --git a/test/expected/param-conversions.out b/test/expected/param-conversions.out
index f282396..2359e39 100644
--- a/test/expected/param-conversions.out
+++ b/test/expected/param-conversions.out
@@ -27,4 +27,13 @@ Error while executing the query
SQLExecDirect failed
22003=ERROR: value "99999999999999999999999" is out of range for type integer;
Error while executing the query
+Result set:
+2
+SQLExecDirect failed
+22P02=ERROR: invalid input syntax for integer: "-";
+Error while executing the query
+Result set:
+-1234
+Result set:
+1234
disconnecting
diff --git a/test/expected/param-conversions_1.out b/test/expected/param-conversions_1.out
index c75aee9..f3d1194 100644
--- a/test/expected/param-conversions_1.out
+++ b/test/expected/param-conversions_1.out
@@ -26,4 +26,13 @@ SQLExecDirect failed
Error while executing the query
Result set:
0
+Result set:
+2
+SQLExecDirect failed
+22P02=ERROR: invalid input syntax for integer: "-";
+Error while executing the query
+Result set:
+-1234
+Result set:
+1234
disconnecting
diff --git a/test/src/param-conversions-test.c b/test/src/param-conversions-test.c
index 47caee4..2603d03 100644
--- a/test/src/param-conversions-test.c
+++ b/test/src/param-conversions-test.c
@@ -7,13 +7,14 @@
#include "common.h"
static void test_convert(const char *sql, SQLUSMALLINT c_type,
- SQLSMALLINT sql_type, char *value);
+ SQLSMALLINT sql_type, SQLPOINTER value);
static HSTMT hstmt = SQL_NULL_HSTMT;
int main(int argc, char **argv)
{
SQLRETURN rc;
+ SQLINTEGER intparam;
test_connect();
@@ -24,7 +25,7 @@ int main(int argc, char **argv)
exit(1);
}
- /*** Test proper escaping of parameters ***/
+ /*** Test proper escaping of integer parameters ***/
printf("\nTesting conversions...\n");
test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "2");
@@ -39,6 +40,13 @@ int main(int argc, char **argv)
test_convert("SELECT 1.3 > ?", SQL_C_CHAR, SQL_FLOAT, "3', 'injected, BAD!', '1");
test_convert("SELECT 1.4 > ?", SQL_C_CHAR, SQL_FLOAT, "4 \\'bad', '1");
test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "99999999999999999999999");
+ test_convert("SELECT 1-?", SQL_C_CHAR, SQL_INTEGER, "-1");
+ test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "-");
+
+ intparam = 1234;
+ test_convert("SELECT 0-?", SQL_C_SLONG, SQL_INTEGER, &intparam);
+ intparam = -1234;
+ test_convert("SELECT 0-?", SQL_C_SLONG, SQL_INTEGER, &intparam);
/* Clean up */
test_disconnect();
@@ -52,13 +60,13 @@ int main(int argc, char **argv)
*/
static void
test_convert(const char *sql, SQLUSMALLINT c_type, SQLSMALLINT sql_type,
- char *value)
+ SQLPOINTER value)
{
- SQLRETURN rc;
- SQLLEN cbParam = SQL_NTS;
- int failed = 0;
+ SQLRETURN rc;
+ SQLLEN cbParam = SQL_NTS;
+ int failed = 0;
- /* a query with an SQL_INTEGER param. */
+ cbParam = SQL_NTS; /* ignored for non-character data */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
c_type, /* value type */
sql_type, /* param type */
--
2.1.4
0002-Send-proper-datatypes-to-the-server-when-binding-par.patch (text/x-diff, 20.5K) download
From 5211901c9853570940a9129fe207f957154e95c8 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 15 Jan 2015 17:39:49 +0200
Subject: [PATCH 2/2] Send proper datatypes to the server when binding
parameters.
We used to always send 0 as the parameter type, which means that the server
should deduce the datatype from the context. That usually works well, but
we can do better. Sometimes there is ambiguity, like in "SELECT '555' > ?".
If the parameter is bound as a string, '6' would return true, but if it's
bound as an integer, it returns false.
When the application calls SQLBindParameter, it specifies the datatype, so
we should make use of that.
Add a test case for the "SELECT '555' > ?" case. While we're at it, improve
the output of the param-conversions-test to be more readable.
---
convert.c | 27 ++++++--
convert.h | 3 +-
pgtypes.c | 124 ++++++++++++++++++++++++++++++++++
pgtypes.h | 3 +-
statement.c | 18 +++--
test/expected/param-conversions.out | 41 ++++++++++-
test/expected/param-conversions_1.out | 37 ++++++++++
test/src/param-conversions-test.c | 69 ++++++++++++++-----
8 files changed, 293 insertions(+), 29 deletions(-)
diff --git a/convert.c b/convert.c
index 7cdf135..ba3f807 100644
--- a/convert.c
+++ b/convert.c
@@ -2143,7 +2143,8 @@ convert_escape(QueryParse *qp, QueryBuild *qb);
static int
inner_process_tokens(QueryParse *qp, QueryBuild *qb);
static int
-ResolveOneParam(QueryBuild *qb, QueryParse *qp, BOOL *isnull, BOOL *usebinary);
+ResolveOneParam(QueryBuild *qb, QueryParse *qp, BOOL *isnull, BOOL *usebinary,
+ Oid *pgType);
static int
processParameters(QueryParse *qp, QueryBuild *qb,
size_t *output_count, SQLLEN param_pos[][2]);
@@ -3167,6 +3168,7 @@ inner_process_tokens(QueryParse *qp, QueryBuild *qb)
char literal_quote = LITERAL_QUOTE, dollar_quote = DOLLAR_QUOTE, escape_in_literal = '\0';
BOOL isnull;
BOOL isbinary;
+ Oid dummy;
if (stmt && stmt->ntab > 0)
bestitem = GET_NAME(stmt->ti[0]->bestitem);
@@ -3522,7 +3524,7 @@ inner_process_tokens(QueryParse *qp, QueryBuild *qb)
/*
* It's a '?' parameter alright
*/
- retval = ResolveOneParam(qb, qp, &isnull, &isbinary);
+ retval = ResolveOneParam(qb, qp, &isnull, &isbinary, &dummy);
if (retval < 0)
return retval;
@@ -3542,7 +3544,9 @@ cleanup:
*/
BOOL
build_libpq_bind_params(StatementClass *stmt, const char *plan_name,
- int *nParams, char ***paramValues,
+ int *nParams,
+ OID **paramTypes,
+ char ***paramValues,
int **paramLengths,
int **paramFormats,
int *resultFormat)
@@ -3556,6 +3560,7 @@ build_libpq_bind_params(StatementClass *stmt, const char *plan_name,
RETCODE retval;
const IPDFields *ipdopts = SC_get_IPDF(stmt);
+ *paramTypes = NULL;
*paramValues = NULL;
*paramLengths = NULL;
*paramFormats = NULL;
@@ -3575,6 +3580,9 @@ build_libpq_bind_params(StatementClass *stmt, const char *plan_name,
if (QB_initialize(&qb, MIN_ALC_SIZE, stmt, NULL) < 0)
return FALSE;
+ *paramTypes = malloc(sizeof(OID) * num_params);
+ if (*paramTypes == NULL)
+ goto cleanup;
*paramValues = malloc(sizeof(char *) * num_params);
if (*paramValues == NULL)
goto cleanup;
@@ -3606,6 +3614,7 @@ inolog("num_p=%d\n", num_p);
BOOL isnull;
BOOL isbinary;
char *val_copy;
+ OID pgType;
inolog("%dth parameter type oid is %u\n", i, PIC_dsp_pgtype(conn, parameters[i]));
@@ -3613,7 +3622,7 @@ inolog("num_p=%d\n", num_p);
continue;
qb.npos = 0;
- retval = ResolveOneParam(&qb, NULL, &isnull, &isbinary);
+ retval = ResolveOneParam(&qb, NULL, &isnull, &isbinary, &pgType);
if (SQL_ERROR == retval)
{
QB_replace_SC_error(stmt, &qb, func);
@@ -3629,11 +3638,13 @@ inolog("num_p=%d\n", num_p);
memcpy(val_copy, qb.query_statement, qb.npos);
val_copy[qb.npos] = '\0';
+ (*paramTypes)[i] = pgType;
(*paramValues)[i] = val_copy;
(*paramLengths)[i] = qb.npos;
}
else
{
+ (*paramTypes)[i] = pgType;
(*paramValues)[i] = NULL;
(*paramLengths)[i] = 0;
}
@@ -3865,9 +3876,12 @@ parse_to_numeric_struct(const char *wv, SQL_NUMERIC_STRUCT *ns, BOOL *overflow)
* *isnull is set to TRUE if it was NULL.
* *isbinary is set to TRUE, if the binary output format was used. (binary
* output is only produced if the FLGB_BINARY_AS_POSSIBLE flag is set)
+ * *pgType is set to the PostgreSQL type OID that should be used when binding
+ * (or 0, to let the server decide)
*/
static int
-ResolveOneParam(QueryBuild *qb, QueryParse *qp, BOOL *isnull, BOOL *isbinary)
+ResolveOneParam(QueryBuild *qb, QueryParse *qp, BOOL *isnull, BOOL *isbinary,
+ OID *pgType)
{
CSTR func = "ResolveOneParam";
@@ -4095,6 +4109,9 @@ inolog("ipara=%p paramType=%d %d proc_return=%d\n", ipara, ipara ? ipara->paramT
param_sqltype = ipara->SQLType;
param_pgtype = PIC_dsp_pgtype(qb->conn, *ipara);
+ /* XXX: should we use param_pgtype here instead? */
+ *pgType = sqltype_to_bind_pgtype(conn, param_sqltype);
+
mylog("%s: from(fcType)=%d, to(fSqlType)=%d(%u)\n", func,
param_ctype, param_sqltype, param_pgtype);
diff --git a/convert.h b/convert.h
index acf11b7..5d6103a 100644
--- a/convert.h
+++ b/convert.h
@@ -59,7 +59,8 @@ int convert_lo(StatementClass *stmt, const void *value, SQLSMALLINT fCType,
size_t findTag(const char *str, char dollar_quote, int ccsc);
BOOL build_libpq_bind_params(StatementClass *stmt, const char *plan_name,
- int *nParams, char ***paramValues,
+ int *nParams, OID **paramTypes,
+ char ***paramValues,
int **paramLengths,
int **paramFormats,
int *resultFormat);
diff --git a/pgtypes.c b/pgtypes.c
index 37889e5..4a6ba57 100644
--- a/pgtypes.c
+++ b/pgtypes.c
@@ -1232,6 +1232,130 @@ pgtype_attr_transfer_octet_length(const ConnectionClass *conn, OID type, int att
}
+
+/*
+ * This is used when binding a query parameter, to decide which PostgreSQL
+ * datatype to send to the server, depending on the SQL datatype that was
+ * used in the SQLBindParameter call.
+ *
+ * For most types, this returns 0, which means that the server should treat
+ * the parameter the same as an untyped literal string, and deduce the type
+ * based on the context.
+ *
+ * This is different from sqltype_to_pgtype(), which doesn't return 0 but
+ * tries to give a closer match to the actual datatype.
+ */
+OID
+sqltype_to_bind_pgtype(const ConnectionClass *conn, SQLSMALLINT fSqlType)
+{
+ OID pgType;
+ const ConnInfo *ci = &(conn->connInfo);
+
+ pgType = 0; /* ??? */
+ switch (fSqlType)
+ {
+ case SQL_BINARY:
+ case SQL_VARBINARY:
+ pgType = PG_TYPE_BYTEA;
+ break;
+
+ case SQL_BIT:
+ pgType = ci->drivers.bools_as_char ? PG_TYPE_CHAR : PG_TYPE_BOOL;
+ break;
+
+ case SQL_TYPE_DATE:
+ case SQL_DATE:
+ pgType = PG_TYPE_DATE;
+ break;
+
+ case SQL_DOUBLE:
+ case SQL_FLOAT:
+ pgType = PG_TYPE_FLOAT8;
+ break;
+
+ case SQL_DECIMAL:
+ case SQL_NUMERIC:
+ pgType = PG_TYPE_NUMERIC;
+ break;
+
+ case SQL_BIGINT:
+ pgType = PG_TYPE_INT8;
+ break;
+
+ case SQL_INTEGER:
+ pgType = PG_TYPE_INT4;
+ break;
+
+ case SQL_LONGVARBINARY:
+ if (ci->bytea_as_longvarbinary)
+ pgType = PG_TYPE_BYTEA;
+ else
+ pgType = conn->lobj_type;
+ break;
+
+ case SQL_REAL:
+ pgType = PG_TYPE_FLOAT4;
+ break;
+
+ case SQL_SMALLINT:
+ case SQL_TINYINT:
+ pgType = PG_TYPE_INT2;
+ break;
+
+ case SQL_TIME:
+ case SQL_TYPE_TIME:
+ pgType = PG_TYPE_TIME;
+ break;
+
+ case SQL_TIMESTAMP:
+ case SQL_TYPE_TIMESTAMP:
+ pgType = PG_TYPE_DATETIME;
+ break;
+
+ case SQL_GUID:
+ if (PG_VERSION_GE(conn, 8.3))
+ pgType = PG_TYPE_UUID;
+ else
+ pgType = 0;
+ break;
+
+ case SQL_INTERVAL_MONTH:
+ case SQL_INTERVAL_YEAR:
+ case SQL_INTERVAL_YEAR_TO_MONTH:
+ case SQL_INTERVAL_DAY:
+ case SQL_INTERVAL_HOUR:
+ case SQL_INTERVAL_MINUTE:
+ case SQL_INTERVAL_SECOND:
+ case SQL_INTERVAL_DAY_TO_HOUR:
+ case SQL_INTERVAL_DAY_TO_MINUTE:
+ case SQL_INTERVAL_DAY_TO_SECOND:
+ case SQL_INTERVAL_HOUR_TO_MINUTE:
+ case SQL_INTERVAL_HOUR_TO_SECOND:
+ case SQL_INTERVAL_MINUTE_TO_SECOND:
+ pgType = PG_TYPE_INTERVAL;
+ break;
+
+ case SQL_CHAR:
+#ifdef UNICODE_SUPPORT
+ case SQL_WCHAR:
+#endif /* UNICODE_SUPPORT */
+ case SQL_LONGVARCHAR:
+#ifdef UNICODE_SUPPORT
+ case SQL_WLONGVARCHAR:
+#endif /* UNICODE_SUPPORT */
+ case SQL_VARCHAR:
+#if UNICODE_SUPPORT
+ case SQL_WVARCHAR:
+#endif /* UNICODE_SUPPORT */
+ default:
+ /* The default is to let the server choose */
+ pgType = 0;
+ break;
+ }
+
+ return pgType;
+}
+
OID
sqltype_to_pgtype(const ConnectionClass *conn, SQLSMALLINT fSqlType)
{
diff --git a/pgtypes.h b/pgtypes.h
index 8d5b33f..34931d4 100644
--- a/pgtypes.h
+++ b/pgtypes.h
@@ -53,7 +53,7 @@
#define PG_TYPE_DATE 1082
#define PG_TYPE_TIME 1083
#define PG_TYPE_TIMESTAMP_NO_TMZONE 1114 /* since 7.2 */
-#define PG_TYPE_DATETIME 1184
+#define PG_TYPE_DATETIME 1184 /* timestamptz */
#define PG_TYPE_INTERVAL 1186
#define PG_TYPE_TIME_WITH_TMZONE 1266 /* since 7.1 */
#define PG_TYPE_TIMESTAMP 1296 /* deprecated since 7.0 */
@@ -83,6 +83,7 @@ extern SQLSMALLINT sqlTypes[];
OID pg_true_type(const ConnectionClass *, OID, OID);
OID sqltype_to_pgtype(const ConnectionClass *conn, SQLSMALLINT fSqlType);
+OID sqltype_to_bind_pgtype(const ConnectionClass *conn, SQLSMALLINT fSqlType);
SQLSMALLINT pgtype_to_concise_type(const StatementClass *stmt, OID type, int col);
SQLSMALLINT pgtype_to_sqldesctype(const StatementClass *stmt, OID type, int col);
diff --git a/statement.c b/statement.c
index 106e968..803d5e6 100644
--- a/statement.c
+++ b/statement.c
@@ -2460,6 +2460,7 @@ libpq_bind_and_exec(StatementClass *stmt, const char *plan_name,
CSTR func = "libpq_bind_and_exec";
ConnectionClass *conn = SC_get_conn(stmt);
int nParams;
+ Oid *paramTypes = NULL;
char **paramValues = NULL;
int *paramLengths = NULL;
int *paramFormats = NULL;
@@ -2487,7 +2488,9 @@ libpq_bind_and_exec(StatementClass *stmt, const char *plan_name,
/* 1. Bind */
mylog("%s: bind plan_name=%s\n", func, plan_name);
if (!build_libpq_bind_params(stmt, plan_name,
- &nParams, ¶mValues,
+ &nParams,
+ ¶mTypes,
+ ¶mValues,
¶mLengths, ¶mFormats,
&resultFormat))
{
@@ -2522,8 +2525,10 @@ libpq_bind_and_exec(StatementClass *stmt, const char *plan_name,
pgres = PQexecParams(conn->pqconn,
pstmt->query,
pstmt->num_params,
- NULL,
- (const char **) paramValues, paramLengths, paramFormats,
+ paramTypes,
+ (const char **) paramValues,
+ paramLengths,
+ paramFormats,
resultFormat);
}
else
@@ -2621,6 +2626,8 @@ cleanup:
}
free(paramValues);
}
+ if (paramTypes)
+ free(paramTypes);
if (paramLengths)
free(paramLengths);
if (paramFormats)
@@ -2709,7 +2716,10 @@ mylog("sta_pidx=%d end_pidx=%d num_p=%d\n", sta_pidx, end_pidx, num_params);
SQL_PARAM_OUTPUT == ipdopts->parameters[i].paramType)
paramTypes[j++] = PG_TYPE_VOID;
else
- paramTypes[j++] = 0;
+ {
+ paramTypes[j++] = sqltype_to_bind_pgtype(conn,
+ ipdopts->parameters[i].SQLType);
+ }
}
}
diff --git a/test/expected/param-conversions.out b/test/expected/param-conversions.out
index 2359e39..00869b0 100644
--- a/test/expected/param-conversions.out
+++ b/test/expected/param-conversions.out
@@ -2,38 +2,75 @@
connected
Testing conversions...
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "2"...
Result set:
0
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "-2"...
Result set:
1
+
+Testing "SELECT 2.2 > ?" with SQL_C_CHAR -> SQL_FLOAT param "2.3"...
Result set:
0
+
+Testing "SELECT 3.3 > ?" with SQL_C_CHAR -> SQL_DOUBLE param "3.01"...
Result set:
1
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_CHAR param "5 escapes: \ and '"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for integer: "5 escapes: \ and '";
Error while executing the query
+
+Testing conversions whose result depend on whether the
+parameter is treated as a string or an integer...
+Testing "SELECT '555' > ?" with SQL_C_CHAR -> SQL_INTEGER param "6"...
+Result set:
+1
+
+Testing "SELECT '555' > ?" with SQL_C_CHAR -> SQL_CHAR param "6"...
+Result set:
+0
+
+
Testing conversions with invalid values...
+Testing "SELECT 2 > ?" with SQL_C_CHAR -> SQL_INTEGER param "2, 'injected, BAD!'"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for integer: "2, 'injected, BAD!'";
Error while executing the query
+
+Testing "SELECT 1.3 > ?" with SQL_C_CHAR -> SQL_FLOAT param "3', 'injected, BAD!', '1"...
SQLExecDirect failed
-22P02=ERROR: invalid input syntax for type numeric: "3', 'injected, BAD!', '1";
+22P02=ERROR: invalid input syntax for type double precision: "3', 'injected, BAD!', '1";
Error while executing the query
+
+Testing "SELECT 1.4 > ?" with SQL_C_CHAR -> SQL_FLOAT param "4 \'bad', '1"...
SQLExecDirect failed
-22P02=ERROR: invalid input syntax for type numeric: "4 \'bad', '1";
+22P02=ERROR: invalid input syntax for type double precision: "4 \'bad', '1";
Error while executing the query
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "99999999999999999999999"...
SQLExecDirect failed
22003=ERROR: value "99999999999999999999999" is out of range for type integer;
Error while executing the query
+
+Testing "SELECT 1-?" with SQL_C_CHAR -> SQL_INTEGER param "-1"...
Result set:
2
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "-"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for integer: "-";
Error while executing the query
+
+Testing "SELECT 0-?" with SQL_C_SLONG -> SQL_INTEGER param 1234...
Result set:
-1234
+
+Testing "SELECT 0-?" with SQL_C_SLONG -> SQL_INTEGER param -1234...
Result set:
1234
+
disconnecting
diff --git a/test/expected/param-conversions_1.out b/test/expected/param-conversions_1.out
index f3d1194..3633a0f 100644
--- a/test/expected/param-conversions_1.out
+++ b/test/expected/param-conversions_1.out
@@ -2,37 +2,74 @@
connected
Testing conversions...
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "2"...
Result set:
0
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "-2"...
Result set:
1
+
+Testing "SELECT 2.2 > ?" with SQL_C_CHAR -> SQL_FLOAT param "2.3"...
Result set:
0
+
+Testing "SELECT 3.3 > ?" with SQL_C_CHAR -> SQL_DOUBLE param "3.01"...
Result set:
1
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_CHAR param "5 escapes: \ and '"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for integer: "5 escapes: \ and '";
Error while executing the query
+
+Testing conversions whose result depend on whether the
+parameter is treated as a string or an integer...
+Testing "SELECT '555' > ?" with SQL_C_CHAR -> SQL_INTEGER param "6"...
+Result set:
+1
+
+Testing "SELECT '555' > ?" with SQL_C_CHAR -> SQL_CHAR param "6"...
+Result set:
+0
+
+
Testing conversions with invalid values...
+Testing "SELECT 2 > ?" with SQL_C_CHAR -> SQL_INTEGER param "2, 'injected, BAD!'"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for integer: "2, 'injected, BAD!'";
Error while executing the query
+
+Testing "SELECT 1.3 > ?" with SQL_C_CHAR -> SQL_FLOAT param "3', 'injected, BAD!', '1"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for type double precision: "3', 'injected, BAD!', '1";
Error while executing the query
+
+Testing "SELECT 1.4 > ?" with SQL_C_CHAR -> SQL_FLOAT param "4 \'bad', '1"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for type double precision: "4 \'bad', '1";
Error while executing the query
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "99999999999999999999999"...
Result set:
0
+
+Testing "SELECT 1-?" with SQL_C_CHAR -> SQL_INTEGER param "-1"...
Result set:
2
+
+Testing "SELECT 1 > ?" with SQL_C_CHAR -> SQL_INTEGER param "-"...
SQLExecDirect failed
22P02=ERROR: invalid input syntax for integer: "-";
Error while executing the query
+
+Testing "SELECT 0-?" with SQL_C_SLONG -> SQL_INTEGER param 1234...
Result set:
-1234
+
+Testing "SELECT 0-?" with SQL_C_SLONG -> SQL_INTEGER param -1234...
Result set:
1234
+
disconnecting
diff --git a/test/src/param-conversions-test.c b/test/src/param-conversions-test.c
index 2603d03..f6d9713 100644
--- a/test/src/param-conversions-test.c
+++ b/test/src/param-conversions-test.c
@@ -6,8 +6,13 @@
#include "common.h"
-static void test_convert(const char *sql, SQLUSMALLINT c_type,
- SQLSMALLINT sql_type, SQLPOINTER value);
+#define TEST_CONVERT(sql, c_type, sql_type, value) \
+ test_convert(sql, c_type, #c_type, sql_type, #sql_type, value)
+
+static void test_convert(const char *sql,
+ SQLSMALLINT c_type, const char *c_type_str,
+ SQLSMALLINT sql_type, const char *sql_type_str,
+ SQLPOINTER value);
static HSTMT hstmt = SQL_NULL_HSTMT;
@@ -28,25 +33,34 @@ int main(int argc, char **argv)
/*** Test proper escaping of integer parameters ***/
printf("\nTesting conversions...\n");
- test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "2");
- test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "-2");
- test_convert("SELECT 2.2 > ?", SQL_C_CHAR, SQL_FLOAT, "2.3");
- test_convert("SELECT 3.3 > ?", SQL_C_CHAR, SQL_DOUBLE, "3.01");
- test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_CHAR, "5 escapes: \\ and '");
+ TEST_CONVERT("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "2");
+ TEST_CONVERT("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "-2");
+ TEST_CONVERT("SELECT 2.2 > ?", SQL_C_CHAR, SQL_FLOAT, "2.3");
+ TEST_CONVERT("SELECT 3.3 > ?", SQL_C_CHAR, SQL_DOUBLE, "3.01");
+ TEST_CONVERT("SELECT 1 > ?", SQL_C_CHAR, SQL_CHAR, "5 escapes: \\ and '");
+
+ /*
+ * The result of these depend on whether the server treats the parameters
+ * as a string or an integer.
+ */
+ printf("\nTesting conversions whose result depend on whether the\n");
+ printf("parameter is treated as a string or an integer...\n");
+ TEST_CONVERT("SELECT '555' > ?", SQL_C_CHAR, SQL_INTEGER, "6");
+ TEST_CONVERT("SELECT '555' > ?", SQL_C_CHAR, SQL_CHAR, "6");
printf("\nTesting conversions with invalid values...\n");
- test_convert("SELECT 2 > ?", SQL_C_CHAR, SQL_INTEGER, "2, 'injected, BAD!'");
- test_convert("SELECT 1.3 > ?", SQL_C_CHAR, SQL_FLOAT, "3', 'injected, BAD!', '1");
- test_convert("SELECT 1.4 > ?", SQL_C_CHAR, SQL_FLOAT, "4 \\'bad', '1");
- test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "99999999999999999999999");
- test_convert("SELECT 1-?", SQL_C_CHAR, SQL_INTEGER, "-1");
- test_convert("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "-");
+ TEST_CONVERT("SELECT 2 > ?", SQL_C_CHAR, SQL_INTEGER, "2, 'injected, BAD!'");
+ TEST_CONVERT("SELECT 1.3 > ?", SQL_C_CHAR, SQL_FLOAT, "3', 'injected, BAD!', '1");
+ TEST_CONVERT("SELECT 1.4 > ?", SQL_C_CHAR, SQL_FLOAT, "4 \\'bad', '1");
+ TEST_CONVERT("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "99999999999999999999999");
+ TEST_CONVERT("SELECT 1-?", SQL_C_CHAR, SQL_INTEGER, "-1");
+ TEST_CONVERT("SELECT 1 > ?", SQL_C_CHAR, SQL_INTEGER, "-");
intparam = 1234;
- test_convert("SELECT 0-?", SQL_C_SLONG, SQL_INTEGER, &intparam);
+ TEST_CONVERT("SELECT 0-?", SQL_C_SLONG, SQL_INTEGER, &intparam);
intparam = -1234;
- test_convert("SELECT 0-?", SQL_C_SLONG, SQL_INTEGER, &intparam);
+ TEST_CONVERT("SELECT 0-?", SQL_C_SLONG, SQL_INTEGER, &intparam);
/* Clean up */
test_disconnect();
@@ -59,13 +73,34 @@ int main(int argc, char **argv)
* error or result.
*/
static void
-test_convert(const char *sql, SQLUSMALLINT c_type, SQLSMALLINT sql_type,
+test_convert(const char *sql,
+ SQLSMALLINT c_type, const char *c_type_str,
+ SQLSMALLINT sql_type, const char *sql_type_str,
SQLPOINTER value)
{
SQLRETURN rc;
SQLLEN cbParam = SQL_NTS;
int failed = 0;
+ /* Print what we're doing */
+ switch (c_type)
+ {
+ case SQL_C_SLONG:
+ printf("Testing \"%s\" with %s -> %s param %d...\n",
+ sql, c_type_str, sql_type_str, *((SQLINTEGER *) value));
+ break;
+
+ case SQL_C_CHAR:
+ printf("Testing \"%s\" with %s -> %s param \"%s\"...\n",
+ sql, c_type_str, sql_type_str, (char *) value);
+ break;
+
+ default:
+ printf("Testing \"%s\" with %s -> %s param...\n",
+ sql, c_type_str, sql_type_str);
+ break;
+ }
+
cbParam = SQL_NTS; /* ignored for non-character data */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
c_type, /* value type */
@@ -99,5 +134,7 @@ test_convert(const char *sql, SQLUSMALLINT c_type, SQLSMALLINT sql_type,
rc = SQLExecDirect(hstmt, (SQLCHAR *) "ROLLBACK /* clean up after failed test */", SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect(ROLLBACK) failed", hstmt);
}
+
+ printf("\n");
}
--
2.1.4
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 12:22 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 15:58 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-16 19:21 ` Faith, Jeremy <jfaith@tycoint.com>
2015-01-16 21:24 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
0 siblings, 1 reply; 10+ messages in thread
From: Faith, Jeremy @ 2015-01-16 19:21 UTC (permalink / raw)
To: Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-odbc
Don't worry about the '-1' test I was just trying to determine, in the server side prepare case, if it was adding quotes i.e. the string I was supplying already had single quotes and was not accepted.
I've had a quick look at the new patches.
patch 0001
To be extra extra careful maybe valid_int_literal() should check 'len' before accessing the first byte and second bytes of 'str' in case len is 0 or 1. This may not be necessary it depends on where 'str' comes from.
Otherwise the patch looks fine but maybe you should add a couple of test cases for SQL_SMALLINT.
patch 0002
In convert.c you have added the comment and line
/* XXX: should we use param_pgtype here instead? */
*pgType = sqltype_to_bind_pgtype(conn, param_sqltype);
this really requires someone with more in depth knowledge than I have.
Again maybe a couple of test cases for SQL_SMALLINT.
I have done a few very quick tests myself and have not found anything wrong so far.
Looks like I opened up a can of worms here, oops, but the end result should be more secure and more consistent with regards to the server side and client side prepare behaviour, which is good. Also your helping people out by forcing integer comparison more often.
I will try to do some more testing next week.
Regards,
Jeremy Faith
________________________________________
From: Heikki Linnakangas [hlinnakangas@vmware.com]
Sent: 15 January 2015 15:58
To: Faith, Jeremy; pgsql-odbc@postgresql.org
Subject: Re: [ODBC] SQL_CURSOR_TYPE prepare execute issue
On 01/15/2015 02:22 PM, Faith, Jeremy wrote:
> As long as you are confident that the test suite is checking this code path that is fine, I just wanted to be sure that was happening.
Sure, thanks!
> I hadn't thought of
> select x-?
> You are right that is a problem.
> What does the server prepare code path do?
> I did a quick check, sever side prepare, with the log turned on and it shows the following
> LOG: execute <unnamed>: select 1-$1
> DETAIL: parameters: $1 = '-1'
> LOG: execute <unnamed>: select 1-$1
> DETAIL: parameters: $1 = '1'
> But if the bound string is '-1' then I get
> ERROR: invalid input syntax for integer: "'-1'" i.e. double quote,single quote,-,1,single quote,double quote
> STATEMENT: select 'wibble',1-$1
Huh. I cannot reproduce that. Can you modify the
test/src/param-conversions-test.c test case to demonstrate it?
> Also get invalid syntax error if it is just a minus sign.
>
> ok, server side prepare log when string is just 6 shows
> LOG: execute <unnamed>: select '555'>$1
> DETAIL: parameters: $1 = '6'
> and this return one row with a value of 0
> so this is doing select '555'>'6';
>
> So to be consistent with what the server side prepare does, I think it should quote the value.
> You should check this as I may have been getting confused.
> I realise this may have some implications for existing programs but given that sometimes it will be server side prepare and sometimes client side it is likely to cause more issues if the results are different.
Ugh, I didn't realize that happens with UseServerSidePrepare=1. But
looking at the code, it's quite obvious that it does; we never send the
datatypes with server-side parameters, so the server always treats them
as "unknown", that is, the same as a plain string literal. That usually
works fine, as the server deduces the right datatype from the context,
but for ambiguous cases like above, it goes wrong.
I think it's pretty clear that the UseServerSidePrepare=1 behaviour is
wrong in this case. In particular, if you bind a parameter as
SQL_INTEGER, and run query "SELECT '555' > ?", the argument should be
treated as an integer and the answer should be "true".
I propose the attached two patches. The first one fixes the "-" case and
negative integers, with UseServerSidePrepare=0. The second one changes
the UseServerSidePrepare=1 behaviour so that a datatype is sent along
with each parameter. String-like datatypes, SQL_CHAR, SQL_WCHAR,
SQL_VARCHAR etc. are still sent as "unknown". It makes sense for string
types IMHO.
Thoughts?
- Heikki
h
Tyco Safety Products/CEM Systems Ltd.
________________________________
This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments.
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 12:22 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 15:58 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-16 19:21 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
@ 2015-01-16 21:24 ` Heikki Linnakangas <hlinnakangas@vmware.com>
0 siblings, 0 replies; 10+ messages in thread
From: Heikki Linnakangas @ 2015-01-16 21:24 UTC (permalink / raw)
To: Faith, Jeremy <jfaith@tycoint.com>; pgsql-odbc
On 01/16/2015 09:21 PM, Faith, Jeremy wrote:
> Don't worry about the '-1' test I was just trying to determine, in the server side prepare case, if it was adding quotes i.e. the string I was supplying already had single quotes and was not accepted.
>
> I've had a quick look at the new patches.
>
> patch 0001
> To be extra extra careful maybe valid_int_literal() should check 'len' before accessing the first byte and second bytes of 'str' in case len is 0 or 1. This may not be necessary it depends on where 'str' comes from.
Ah, you're right. Fixed.
> Otherwise the patch looks fine but maybe you should add a couple of test cases for SQL_SMALLINT.
Added.
> patch 0002
> In convert.c you have added the comment and line
> /* XXX: should we use param_pgtype here instead? */
> *pgType = sqltype_to_bind_pgtype(conn, param_sqltype);
> this really requires someone with more in depth knowledge than I have.
I don't fully understand that either. It's not clear to me when
param_pgtype is set, and where the value comes from. I decided to ignore
it for now, which makes on which datatype is chosen simpler, and I think
is closer to the old behaviour.
> Again maybe a couple of test cases for SQL_SMALLINT.
Added.
> I have done a few very quick tests myself and have not found anything wrong so far.
>
> Looks like I opened up a can of worms here, oops, but the end result should be more secure and more consistent with regards to the server side and client side prepare behaviour, which is good. Also your helping people out by forcing integer comparison more often.
> I will try to do some more testing next week.
Thanks, I've pushed these changes now.
- Heikki
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-15 16:02 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-01-15 18:17 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
1 sibling, 1 reply; 10+ messages in thread
From: Alvaro Herrera @ 2015-01-15 16:02 UTC (permalink / raw)
To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: Faith, Jeremy <jfaith@tycoint.com>; pgsql-odbc
Heikki Linnakangas wrote:
> >I have had a quick look over the change and it looks ok to me. Something of a clean up and simplification as well.
> >If I understand it correctly, the only things that don't get quoted are SQL_INTEGER and SQL_SMALLINT that pass the new valid_int_literal() test.
> >The only thing I can see that could pass that test and not be a valid integer would be a single minus char i.e. "-"
> >not sure if there is anyway that could be vulnerable though.
>
> Ah, good catch. That is definitely a problem. Consider:
>
> SELECT * FROM foo WHERE 1-? > 0
>
> If you replace ? with -, it becomes "--", which comments out the rest of the
> query. That's actually a problem with any negative number.
>
> It would be tempting to just always quote the value, but that again would
> lead to subtle changes in the datatype that the server chooses.
Maybe you can "quote" it with whitespace, so that it becomes
SELECT * FROM foo WHERE 1- -1 > 0
which is no longer a comment and has no other side effect.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: SQL_CURSOR_TYPE prepare execute issue
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Re: SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Re: SQL_CURSOR_TYPE prepare execute issue Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 16:02 ` Re: SQL_CURSOR_TYPE prepare execute issue Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2015-01-15 18:17 ` Heikki Linnakangas <hlinnakangas@vmware.com>
0 siblings, 0 replies; 10+ messages in thread
From: Heikki Linnakangas @ 2015-01-15 18:17 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Faith, Jeremy <jfaith@tycoint.com>; pgsql-odbc
On 01/15/2015 06:02 PM, Alvaro Herrera wrote:
> Heikki Linnakangas wrote:
>
>>> I have had a quick look over the change and it looks ok to me. Something of a clean up and simplification as well.
>>> If I understand it correctly, the only things that don't get quoted are SQL_INTEGER and SQL_SMALLINT that pass the new valid_int_literal() test.
>>> The only thing I can see that could pass that test and not be a valid integer would be a single minus char i.e. "-"
>>> not sure if there is anyway that could be vulnerable though.
>>
>> Ah, good catch. That is definitely a problem. Consider:
>>
>> SELECT * FROM foo WHERE 1-? > 0
>>
>> If you replace ? with -, it becomes "--", which comments out the rest of the
>> query. That's actually a problem with any negative number.
>>
>> It would be tempting to just always quote the value, but that again would
>> lead to subtle changes in the datatype that the server chooses.
>
> Maybe you can "quote" it with whitespace, so that it becomes
>
> SELECT * FROM foo WHERE 1- -1 > 0
>
> which is no longer a comment and has no other side effect.
Hmm. Strictly speaking, -1 is interpreted as -(1) by the server. Usually
it doesn't make any difference, but see:
postgres=# select -32768::smallint;
ERROR: smallint out of range
postgres=# select (-32768)::smallint;
int2
--------
-32768
(1 row)
It also affects the automatically chosen column name:
postgres=# select -1::int4;
?column?
----------
-1
(1 row)
postgres=# select (-1)::int4;
int4
------
-1
(1 row)
On the whole, using parens seems better.
- Heikki
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 10+ messages in thread
end of thread, other threads:[~2015-01-16 21:24 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-01-13 17:57 SQL_CURSOR_TYPE prepare execute issue Faith, Jeremy <jfaith@tycoint.com>
2015-01-13 23:00 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-14 16:56 ` Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 10:52 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 12:22 ` Faith, Jeremy <jfaith@tycoint.com>
2015-01-15 15:58 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-16 19:21 ` Faith, Jeremy <jfaith@tycoint.com>
2015-01-16 21:24 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-15 16:02 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-01-15 18:17 ` Heikki Linnakangas <hlinnakangas@vmware.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox