[libpostgresql-jdbc-java] 11/14: When performing replace processing we must continue processing until we hit the end of a user supplied query, not just once we've detected the end of a valid query. Consider the example: SELECT a FROM t WHERE (1>0)) ORDER BY a; We must send the whole query to the backend, not just the section before the last closing parenthesis.

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:19:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL8_1_405
in repository libpostgresql-jdbc-java.

commit 32fe0737b791915efcfb22f2f56c676d3141edcb
Author: Kris Jurka <books at ejurka.com>
Date:   Wed Feb 1 18:52:30 2006 +0000

    When performing replace processing we must continue processing
    until we hit the end of a user supplied query, not just once
    we've detected the end of a valid query.  Consider the example:
    SELECT a FROM t WHERE (1>0)) ORDER BY a;
    We must send the whole query to the backend, not just the section
    before the last closing parenthesis.
    
    Reported by Senden Kris
---
 org/postgresql/jdbc2/AbstractJdbc2Statement.java | 20 ++++++++++++++++----
 org/postgresql/test/jdbc2/StatementTest.java     | 11 ++++++++++-
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java
index 365786a..bacb941 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v 1.84.2.1 2005/12/04 21:41:21 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v 1.84.2.2 2006/01/30 20:10:41 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -747,8 +747,21 @@ public abstract class AbstractJdbc2Statement implements BaseStatement
         {
             // Since escape codes can only appear in SQL CODE, we keep track
             // of if we enter a string or not.
-            StringBuffer newsql = new StringBuffer(p_sql.length());
-            parseSql(p_sql,0,newsql,false);
+            int len = p_sql.length();
+            StringBuffer newsql = new StringBuffer(len);
+            int i=0;
+            while (i<len){
+                i=parseSql(p_sql,i,newsql,false);
+                // We need to loop here in case we encounter invalid
+                // SQL, consider: SELECT a FROM t WHERE (1 > 0)) ORDER BY a
+                // We can't ending replacing after the extra closing paren
+                // because that changes a syntax error to a valid query
+                // that isn't what the user specified.
+                if (i < len) {
+                    newsql.append(p_sql.charAt(i));
+                    i++;
+                }
+            }
             return newsql.toString();
         }
         else
@@ -914,7 +927,6 @@ public abstract class AbstractJdbc2Statement implements BaseStatement
             StringBuffer arg = new StringBuffer();
             int lastPos=i;
             i=parseSql(args,i,arg,true);
-            int nestedCount=0;
             if (lastPos!=i){
                 parsedArgs.add(arg);
             }
diff --git a/org/postgresql/test/jdbc2/StatementTest.java b/org/postgresql/test/jdbc2/StatementTest.java
index 5f4e775..7684e07 100644
--- a/org/postgresql/test/jdbc2/StatementTest.java
+++ b/org/postgresql/test/jdbc2/StatementTest.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/StatementTest.java,v 1.17 2005/11/05 09:27:13 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/StatementTest.java,v 1.17.2.1 2005/12/15 23:28:56 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -345,4 +345,13 @@ public class StatementTest extends TestCase
         assertTrue(!rs.next());
     }
 
+    public void testUnbalancedParensParseError() throws SQLException
+    {
+        Statement stmt = con.createStatement();
+        try {
+            stmt.executeQuery("SELECT i FROM test_statement WHERE (1 > 0)) ORDER BY i");
+            fail("Should have thrown a parse error.");
+        } catch (SQLException sqle) { }
+    }
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list