2017-12-01 11 views
-3

私は間違いなく、真偽結果を表示する代わりに永遠に実行される以下のプログラムを考え出しました。それはバグですか?私は実行時間が(マッチングアルゴリズムの貪欲/不本意/所有性のために)長すぎる可能性があることを認めている(無限ではない)。とにかく、私はこの事件を通知に持ってきたかった。JDK 1.8のjava.util.regex.Patternのバグ?

String str = "[EXCEPTION] org.jfree.chart.annotations.junit.CategoryTextAnnotationTests.testEquals(org.jfree.chart.annotations.junit.CategoryTextAnnotationTests) false java.lang.NullPointerException [STACKTRACE] org.jfree.chart.annotations.AbstractAnnotation.notifyListeners(AbstractAnnotation.java:145) org.jfree.chart.annotations.AbstractAnnotation.fireAnnotationChanged(AbstractAnnotation.java:129) org.jfree.chart.annotations.CategoryTextAnnotation.setCategoryAnchor(CategoryTextAnnotation.java:157) org.jfree.chart.annotations.junit.CategoryTextAnnotationTests.testEquals(CategoryTextAnnotationTests.java:101) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) junit.framework.TestCase.runTest(TestCase.java:176) junit.framework.TestCase.runBare(TestCase.java:141) junit.framework.TestResult$1.protect(TestResult.java:122) junit.framework.TestResult.runProtected(TestResult.java:142) junit.framework.TestResult.run(TestResult.java:125) junit.framework.TestCase.run(TestCase.java:129) junit.framework.TestSuite.runTest(TestSuite.java:252) junit.framework.TestSuite.run(TestSuite.java:247) org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) org.pitest.junit.adapter.CustomRunnerExecutor.run(CustomRunnerExecutor.java:42) org.pitest.junit.adapter.AdaptedJUnitTestUnit.execute(AdaptedJUnitTestUnit.java:85) org.pitest.mutationtest.execute.MutationTimeoutDecorator$1.run(MutationTimeoutDecorator.java:88) java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) java.util.concurrent.FutureTask.run(FutureTask.java:262) java.lang.Thread.run(Thread.java:745)\nsdas"; 
System.out.println(str.matches("\\[EXCEPTION\\] (.)* \\[STACKTRACE\\]((.)*\\((.)*\\))*")); 

なく、少なくとも最後に、接頭辞「\ nsdas」は、この動作をトリガーすることに注意してください。

+0

String :: matchesはjava.util.regex.Patternに関して実装されています。 – RealNamesOrHandles

+1

あなたの正規表現は実際に動作していますが、正規表現が与えられた文字列にマッチする多数の方法のために、_catastrophicバックトラッキングと呼ばれるものがあります。あなたの正規表現はあまりにも長い時間をかけて仕上げています。 'str'の値を半分にすることでこれを確認することができます。しかし、文字列は正規表現ではなく、問題ではありません。 –

+2

@Aominè、「大惨事のバックトラッキング」という概念を導入していただきありがとうございます。私はあなたから何かを学んだ: – RealNamesOrHandles

答えて

0

これはバグですか?

私はそうは思わない。私はそれが、あまり設計されていない正規表現による指数関数的なバックトラッキングの例であると考えています。

このパターンは問題です。

((.)*\\((.)*\\))* 

特に、(.)*である。それらはと')'' 'を含む任意の文字列と一致します。それらを[^() ]*に変更すると、パフォーマンスが大幅に向上するはずです。

+0

説明をありがとう。私はまだJavaを学んでいます:D – RealNamesOrHandles

関連する問題