From 197ac5608f0abbbaa7f17c13f8188df77b9c6834 Mon Sep 17 00:00:00 2001 From: Ashley Scopes <73482956+ascopes@users.noreply.github.com> Date: Mon, 23 Sep 2024 07:44:50 +0100 Subject: [PATCH] Simplify batch arg quoting by ignoring invalid characters --- .../plugins/JvmPluginResolver.java | 38 +++++-------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/plugins/JvmPluginResolver.java b/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/plugins/JvmPluginResolver.java index 9a173ee2..8aab027f 100644 --- a/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/plugins/JvmPluginResolver.java +++ b/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/plugins/JvmPluginResolver.java @@ -350,6 +350,8 @@ private Path writeArgLineFile( } private void quoteShellArg(StringBuilder sb, String arg) { + // POSIX file names can be a bit more complicated and may need escaping + // in certain edge cases to remain valid. sb.append('\''); for (var i = 0; i < arg.length(); ++i) { var c = arg.charAt(i); @@ -374,38 +376,16 @@ private void quoteShellArg(StringBuilder sb, String arg) { break; } } - sb.append('\''); } private void quoteBatchArg(StringBuilder sb, String arg) { - sb.append('"'); - - for (var i = 0; i < arg.length(); ++i) { - var c = arg.charAt(i); - - switch (c) { - case '"': - sb.append("\"\"\""); - break; - case '%': - sb.append("%%"); - break; - case '\r': - case '\t': - case '^': - case '&': - case '<': - case '>': - case '|': - sb.append('^').append(c); - break; - default: - sb.append(c); - break; - } - } - - sb.append('"'); + // All the escapable characters in batch files other than quotes + // are considered to be illegal characters in Windows file names, + // so we can make the assumption that we don't need to change much + // here. + sb.append('"') + .append(arg.replaceAll("\"", "\"\"\"")) + .append('"'); } }