Skip to content

Conversation

@fishcakez
Copy link
Contributor

@fishcakez fishcakez commented Feb 4, 2026

When trying to use regex patterns with quotes or escapes in there are syntax issues for py and java.

For py, the generated code for regex containing a " is not be escaped and fail at runtime with SyntaxError. Therefore reuse the python r usage handling so that Python converts to string to escape correctly. The test added will fail to compile without the fix:

[string - pattern (quote) - valid] (python harness) executor error: [tests/harness/python/python-harness tests/harness/python/python-harness] failed execution (exit status 1) - captured stderr:
Traceback (most recent call last):
  File "/tmp/bazel-working-directory/com_envoyproxy_protoc_gen_validate/bazel-out/aarch64-fastbuild/bin/tests/harness/executor/executor_python_test.runfiles/com_envoyproxy_protoc_gen_validate/tests/harness/python/harness.py", line 42, in <module>
    valid = validate(test_msg)
            ^^^^^^^^^^^^^^^^^^
  File "/tmp/bazel-working-directory/com_envoyproxy_protoc_gen_validate/bazel-out/aarch64-fastbuild/bin/tests/harness/executor/executor_python_test.runfiles/com_envoyproxy_protoc_gen_validate/python/protoc_gen_validate/validator.py", line 58, in validate
    return _validate_inner(ValidatingMessage(proto_message))(proto_message)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/bazel-working-directory/com_envoyproxy_protoc_gen_validate/bazel-out/aarch64-fastbuild/bin/tests/harness/executor/executor_python_test.runfiles/com_envoyproxy_protoc_gen_validate/python/protoc_gen_validate/validator.py", line 68, in _validate_inner
    exec(func, globals(), local_vars)
  File "<string>", line 7
    raise ValidationFailed("p.val pattern does not match ^[^"]*$")
                                                             ^
SyntaxError: closing parenthesis ']' does not match opening parenthesis '('
[string - pattern (quote) - invalid] (python harness) executor error: [tests/harness/python/python-harness tests/harness/python/python-harness] failed execution (exit status 1) - captured stderr:
Traceback (most recent call last):
  File "/tmp/bazel-working-directory/com_envoyproxy_protoc_gen_validate/bazel-out/aarch64-fastbuild/bin/tests/harness/executor/executor_python_test.runfiles/com_envoyproxy_protoc_gen_validate/tests/harness/python/harness.py", line 42, in <module>
    valid = validate(test_msg)
            ^^^^^^^^^^^^^^^^^^
  File "/tmp/bazel-working-directory/com_envoyproxy_protoc_gen_validate/bazel-out/aarch64-fastbuild/bin/tests/harness/executor/executor_python_test.runfiles/com_envoyproxy_protoc_gen_validate/python/protoc_gen_validate/validator.py", line 58, in validate
    return _validate_inner(ValidatingMessage(proto_message))(proto_message)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/bazel-working-directory/com_envoyproxy_protoc_gen_validate/bazel-out/aarch64-fastbuild/bin/tests/harness/executor/executor_python_test.runfiles/com_envoyproxy_protoc_gen_validate/python/protoc_gen_validate/validator.py", line 68, in _validate_inner
    exec(func, globals(), local_vars)
  File "<string>", line 7
    raise ValidationFailed("p.val pattern does not match ^[^"]*$")
                                                             ^
SyntaxError: closing parenthesis ']' does not match opening parenthesis '('

For java, the generated code for regex containing \ is not escaped and failed at compile. There is a commented out ReplaceAll for java to escape it. Removing the commented out line passes a simple text. Looking through the code history this commented out line has been there since original PR. The test added will fail to compile without the fix:

bazel-out/aarch64-fastbuild/bin/tests/harness/cases/_javac/java/libjava_tmp/tests/harness/cases/StringsValidator.java:320: error: ')' or ',' expected
		com.google.re2j.Pattern VAL__PATTERN = com.google.re2j.Pattern.compile("^[^\\"]*$");
		                                                                              ^
bazel-out/aarch64-fastbuild/bin/tests/harness/cases/_javac/java/libjava_tmp/tests/harness/cases/StringsValidator.java:320: error: unclosed string literal
		com.google.re2j.Pattern VAL__PATTERN = com.google.re2j.Pattern.compile("^[^\\"]*$");
		                                                                                 ^
bazel-out/aarch64-fastbuild/bin/tests/harness/cases/_javac/java/libjava_tmp/tests/harness/cases/StringsValidator.java:337: error: illegal escape character
		com.google.re2j.Pattern VAL__PATTERN = com.google.re2j.Pattern.compile("^\\\x41+$");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant