JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

sql parse StackOverflowError

Open bgy675939778 opened this issue 5 years ago • 14 comments

Describe the bug image

I was using the pagehelper paging plug-in when the problem occurred, but I think this problem should not be related to pagehelper, because the problem occurred in the invoke to select.toString() of jsqlparser.

To Reproduce Steps to reproduce the behavior:

  1. Example SQL sql-error.txt

  2. Parsing this SQL using JSqlParser with this statements 1630484451

    image

    • this is its invoke stack: image
  3. Exception image

System

  • Database you are using
    • MySQL
  • Java Version
    • JDK 11
  • JSqlParser version
    • 1.2 (Dependent in pagehelper)
    <dependency>
       <groupId>com.github.jsqlparser</groupId>
       <artifactId>jsqlparser</artifactId>
       <version>1.2</version>
    </dependency>
    

bgy675939778 avatar Sep 01 '21 08:09 bgy675939778

Greetings. You are using an ancient version on JSQLParser and it will be difficult to provide assistance for that.Please try your particular query with latest 4.2 Snapshot from GIT.If this fails we would be able to look at it and render assistance. Sent from my Galaxy -------- Original message --------From: bgy675939778 @.> Date: 01/09/2021 15:36 (GMT+07:00) To: JSQLParser/JSqlParser @.> Cc: Subscribed @.***> Subject: [JSQLParser/JSqlParser] sql parse StackOverflowError (#1324) Describe the bug

I was using the pagehelper paging plug-in when the problem occurred, but I think this problem should not be related to pagehelper, because the problem occurred in the invoke to select.toString() of jsqlparser. To Reproduce Steps to reproduce the behavior:

Example SQL sql-error.txt

Parsing this SQL using JSqlParser with this statements

this is its invoke stack:

Exception

System

Database you are using

MySQL

Java Version

JDK 11

JSqlParser version

1.2 (Dependent in pagehelper)

com.github.jsqlparser jsqlparser 1.2

—You are receiving this because you are subscribed to this thread.Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android.

manticore-projects avatar Sep 01 '21 08:09 manticore-projects

First of all thank you for replying~

And I wrote a test, using jsqlparser 4.1 version.

but...

1630493357

image

When it reached select.toString(), the same error occurred...

bgy675939778 avatar Sep 01 '21 10:09 bgy675939778

Ok. Please share the query in plain text and I promise to look into this tomorrow. 

Never mind, I found the query in your original post. But I am puzzled: when I build a test case with JSQLParser 4.2-Snapshot I get a Java exception:

error: constant string too long

manticore-projects avatar Sep 01 '21 11:09 manticore-projects

I try to reproduce your problem with a more sophisticated test case

    @Test
    public void testIssue1324() throws JSQLParserException {
        for (int j=0; j<1000; j++){
            StringBuilder builder = new StringBuilder("SELECT s.ci_no, s.* FROM server AS s WHERE s.exclusive IN ( ?, ?, ?) AND ( s.hostname LIKE concat('%', ?, '%') ");
            for (int i = 0; i < j; i++) {
                builder.append("OR s.hostname LIKE concat('%', ?, '%') ");
            }
            builder.append(") ORDER BY hostname ASC");
            assertSqlCanBeParsedAndDeparsed(builder.toString(), true);

            System.out.println("Ok with " + j);
        }
    }

While it slows down with each iteration as expected, I have been able to run all 1000 iterations without any problem. Or in other words: I am not able to reproduce your challenge with JSQLParser 4.2-Snapshot.

Please try the test above by yourself and return with findings or a modified testcase.

manticore-projects avatar Sep 01 '21 12:09 manticore-projects

Since parsing uses the Stack very heavily, could it be, that your configuration provides only a very limited Stack? It would be quite unusual to find a Stack overflow by recalling the same method for eternity in JSqlParser. This should have surfaced somewhere in the thousands of tests every build runs. So I am quite curious, what the problem is.

wumpz avatar Sep 01 '21 22:09 wumpz

Ok. Please share the query in plain text and I promise to look into this tomorrow.  Never mind, I found the query in your original post. But I am puzzled: when I build a test case with JSQLParser 4.2-Snapshot I get a Java exception: error: constant string too long

Yes, it will happen this exception constant string too long, because the string is too long.

Here's how I did it:

  1. let String sql = "";
  2. change its value in memory by idea evaluate: 1630547347

bgy675939778 avatar Sep 02 '21 01:09 bgy675939778

And this my new test, it had the same problem when select.toString()...

@Test
public void test1() throws JSQLParserException {
	StringBuilder builder = new StringBuilder("SELECT s.ci_no, s.* FROM server AS s WHERE s.exclusive IN ( ?, ?, ?) AND ( s.hostname LIKE concat('%', ?, '%') ");
	for (int i = 0; i < 5000; i++) {
		builder.append("OR s.hostname LIKE concat('%', ?, '%') ");
	}
	builder.append(") ORDER BY hostname ASC");
	String sql = builder.toString();
	Statement statement = CCJSqlParserUtil.parse(sql);
	Select select = (Select)statement;
	String result = select.toString();
}

bgy675939778 avatar Sep 02 '21 02:09 bgy675939778

You are simply exhausting the Stack when holding too many Expressions. That is normal and expected. The test case I provided will tell you what maximum J your system can process. Beyond that you would need to add Memory or increase Stack Size.

Please also note: your example is quite theoretical and literally designed to exhaust stack and memory. I do not not see a practical relevance unless you want to operate on the tiniest possible VM with only 192 MByte memory allocated.

On my machine, maximum 'J' is well above 2600, but below 3000 (test is still running). I have not started to define JVM's memory settings. At this point we would also need to talk about which JVM and OS exactly to use and so on. I do not think that this was in scope.

manticore-projects avatar Sep 02 '21 03:09 manticore-projects

Since parsing uses the Stack very heavily, could it be, that your configuration provides only a very limited Stack? It would be quite unusual to find a Stack overflow by recalling the same method for eternity in JSqlParser. This should have surfaced somewhere in the thousands of tests every build runs. So I am quite curious, what the problem is.

Yes... you are right.

When I set -Xss2M, it works fine... but default -Xss1M can't.

bgy675939778 avatar Sep 02 '21 03:09 bgy675939778

You are simply exhausting the Stack when holding too many Expressions. That is normal and expected. The test case I provided will tell you what maximum J your system can process. Beyond that you would need to add Memory or increase Stack Size.

Please also note: your example is quite theoretical and literally designed to exhaust stack and memory. I do not not see a practical relevance unless you want to operate on the tiniest possible VM with only 192 MByte memory allocated.

On my machine, maximum 'J' is well above 2600, but below 3000 (test is still running). I have not started to define JVM's memory settings. At this point we would also need to talk about which JVM and OS exactly to use and so on. I do not think that this was in scope.

Yes... This sql appears during runtime in our application, but it is caused by user misoperation, which does not occur under normal circumstances.

It’s just that I tracked down the stack and found that there was a problem with select.toString(), so I filed an issue.

But this problem can be solved by setting -Xss2m

bgy675939778 avatar Sep 02 '21 03:09 bgy675939778

When I set -Xss2M, it works fine... but default -Xss1M can't.

The necessary Stack Size is more or less a direct function of your SQL Statement Length/Complexity. Short SQL Statements with few expressions will result in small necessary Stack Size but long SQL Statements with a lot of Expressions (like yours!) will need big stacks to hold all these expressions.

manticore-projects avatar Sep 02 '21 03:09 manticore-projects

It’s just that I tracked down the stack and found that there was a problem with select.toString(), so I filed an issue.

It is not about the toString() function in particular. It is about exhausting the Stack with too many Expressions. During my tests, the exception has been thrown at various places and it depends on the Memory Settings and the SQL and even the timing, where the Stack will exhaust eventually.

But this problem can be solved by setting -Xss2m

Increasing the Memory and Stack Size is a feasible workaround, but you will always find a length (in my example J) exhausting even that increased memory.

Can we close this issue?

manticore-projects avatar Sep 02 '21 03:09 manticore-projects

It’s just that I tracked down the stack and found that there was a problem with select.toString(), so I filed an issue.

It is not about the toString() function in particular. It is about exhausting the Stack with too many Expressions. During my tests, the exception has been thrown at various places and it depends on the Memory Settings and the SQL and even the timing, where the Stack will exhaust eventually.

But this problem can be solved by setting -Xss2m

Increasing the Memory and Stack Size is a feasible workaround, but you will always find a length (in my example J) exhausting even that increased memory.

Can we close this issue?

yes... thank you very much for your attention and help

bgy675939778 avatar Sep 02 '21 03:09 bgy675939778

You are most welcome!

manticore-projects avatar Sep 02 '21 03:09 manticore-projects