Bug 1317001

Summary: replaceAll causes Unexpected internal error
Product: Red Hat Enterprise Linux 7 Reporter: Marko Myllynen <myllynen>
Component: java-1.8.0-openjdkAssignee: jiri vanek <jvanek>
Status: CLOSED NOTABUG QA Contact: BaseOS QE - Apps <qe-baseos-apps>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.2CC: sbaiduzh
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-03-14 13:05:05 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description Marko Myllynen 2016-03-11 16:43:58 UTC
Description of problem:
$ cat StrTest.java
public class StrTest {
    public static void main (String[] args) {
        String test = "abc\"def";
        System.out.println("Test string is  :" + test + ":");
        test = test.replaceAll("\\", "");
        System.out.println("Test string now :" + test + ":");
    }
}
$ javac StrTest.java
$ java StrTest
Test string is  :abc"def:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.compile(Pattern.java:1702)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.replaceAll(String.java:2223)
	at StrTest.main(StrTest.java:5)
zsh: exit 1     java StrTest
$ rpm -qf $(readlink -f $(which java))
java-1.8.0-openjdk-headless-1.8.0.71-2.b15.el7_2.x86_64

Comment 2 jiri vanek 2016-03-14 13:05:05 UTC
Yes, the pattern is wrong.
this is only java slash escape.
  test = test.replaceAll("\\", "");
Regeex is second round of escaping. So to escape SINGLE slash, you need
  test = test.replaceAll("\\\\", "");

However, slash is NOT in your original string of
   String test = "abc\"def";

So I'm not sure what was yours goal. 
Maybe;
   public static void main (String[] args) {
        String test = "abc\\\"def";
        System.out.println("Test string is  :" + test + ":");
        test = test.replaceAll("\\\\", "");
        System.out.println("Test string now :" + test + ":");
    }

: Test string is  :abc\"def:
: Test string now :abc"def:

So... this is not a bug. This is very correct behaviour.

Comment 3 Marko Myllynen 2016-03-15 07:08:33 UTC
The regex is wrong, yes, but the issue was "Unexpected internal error," which sounds like a JVM internal issue not a regex pattern issue. Thanks.

Comment 4 jiri vanek 2016-03-15 07:39:06 UTC
Still it is PatternSyntaxException. And although I agree that " Unexpected internal error near index 1\^" is not exactly est message. It is correclty pointing to position in your regex(^\) and also in java code where it happened	(at java.util.regex.Pattern.error(Pattern.java:1955)), So I would not claim it as bug. If you disagree, feel free to reopen.

Comment 5 Marko Myllynen 2016-03-15 08:17:22 UTC
I saw PatternSyntaxException earlier so when I see this "Unexpected internal error" I thought this is something different. I guess it would be better to have consistent error messages in every case possible but I'll leave it up to you to make the final call whether this is worth changing or not. Thanks.