google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Unclosed string literal error on regex string

Open diegomarquezp opened this issue 8 months ago • 1 comments

Hello, I found this issue when updating the formatter version in googleapis/java-spanner

package com.example;

final class Test {
  // Error: unclosed string literal
  String breaks = "'\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(READ_TIMESTAMP)[\\t";
  // Works
  String fix =    "'\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(READ_TIMESTAMP)[\u005Ct";
}

Produces the following error:

hi on] diegomarquezp:java-spanner$ java -jar ~/.library_generation/google-java-format.jar --version 
google-java-format: Version 1.26.0
[hi on] diegomarquezp:java-spanner$ java -jar ~/.library_generation/google-java-format.jar test.java 
test.java:7:13: error: unclosed string literal
  String fix = "'((STRONG)|(MIN_READ_TIMESTAMP)[\\t ]+((\\d{4})-(\\d{2})-(\\d{2})([Tt](\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(READ_TIMESTAMP)[\u005Ct ]+((\\d{4})-(\\d{2})-(\\d{2})([Tt](\\d{2}):(\\d{2}):(    \\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(MAX_STALENESS)[\u005Ct ]+((\\d{1,19})(s|ms|us|ns))|(EXACT_STALENESS)[\\t ]+((\\d{1,19})(s|ms|us|ns)))'";
            ^
[hi on] diegomarquezp:java-spanner$ 

Note that String fix doesn't have the error after replacing these two specific double backslashes \\ with the hexcode version \u0005C.

update: simplified reproducer code.

diegomarquezp avatar Apr 22 '25 18:04 diegomarquezp

Can you remove irrelevant characters in the example?

suztomo avatar May 09 '25 02:05 suztomo