You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2023/03/02 06:44:12 UTC

[ant] branch master updated: 66468: Fix regexmapper to not swallow backslashes from output

This is an automated email from the ASF dual-hosted git repository.

jaikiran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new 4dbe7a2a6 66468: Fix regexmapper to not swallow backslashes from output
4dbe7a2a6 is described below

commit 4dbe7a2a600fde2cf4cc525f4985a81f9616fab8
Author: Jaikiran Pai <ja...@apache.org>
AuthorDate: Thu Mar 2 12:13:41 2023 +0530

    66468: Fix regexmapper to not swallow backslashes from output
---
 WHATSNEW                                                       |  5 +++++
 src/etc/testcases/types/mappers/regexpmapper.xml               |  6 ++++++
 src/main/org/apache/tools/ant/util/RegexpPatternMapper.java    |  8 +++++---
 .../tools/ant/types/mappers/RegexpPatternMapperTest.java       | 10 ++++++++++
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/WHATSNEW b/WHATSNEW
index 288d1581a..216d69c41 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -13,6 +13,11 @@ Fixed bugs:
    basedir. This has now been fixed.
    Bugzilla Report 66504  
 
+ * regexmapper would, in some cases, incorrectly consume backslash characters
+   from the "to" attribute, resulting in missing backslashes in the output.
+   This is now fixed.
+   Bugzilla Report 66468
+
 Changes from Ant 1.10.12 TO Ant 1.10.13
 =======================================
 
diff --git a/src/etc/testcases/types/mappers/regexpmapper.xml b/src/etc/testcases/types/mappers/regexpmapper.xml
index 08f0dedc6..4cca53a96 100644
--- a/src/etc/testcases/types/mappers/regexpmapper.xml
+++ b/src/etc/testcases/types/mappers/regexpmapper.xml
@@ -29,4 +29,10 @@
       <regexpmapper from="d/e/(.*)" to="\1" handledirsep="yes"/>
     </mapperresult>
   </target>
+
+  <target name="to-with-backslash-for-non-groups">
+    <mapperresult input="a/j.java" output="foo\bar=j.java">
+      <regexpmapper from="a/(.*)" to="foo\bar=\1" />
+    </mapperresult>
+  </target>
 </project>
diff --git a/src/main/org/apache/tools/ant/util/RegexpPatternMapper.java b/src/main/org/apache/tools/ant/util/RegexpPatternMapper.java
index 144722ab6..ec767f7a7 100644
--- a/src/main/org/apache/tools/ant/util/RegexpPatternMapper.java
+++ b/src/main/org/apache/tools/ant/util/RegexpPatternMapper.java
@@ -142,12 +142,14 @@ public class RegexpPatternMapper implements FileNameMapper {
         result.setLength(0);
         for (int i = 0; i < to.length; i++) {
             if (to[i] == '\\') {
-                if (++i < to.length) {
-                    int value = Character.digit(to[i], DECIMAL);
+                final int nextCharIndex = i + 1;
+                if (nextCharIndex < to.length) {
+                    int value = Character.digit(to[nextCharIndex], DECIMAL);
                     if (value > -1) {
+                        i++; // mark that the next digit (after the backslash) has been consumed
                         result.append(v.get(value));
                     } else {
-                        result.append(to[i]);
+                        result.append(to[i]); // append the backslash character
                     }
                 } else {
                     // TODO - should throw an exception instead?
diff --git a/src/tests/junit/org/apache/tools/ant/types/mappers/RegexpPatternMapperTest.java b/src/tests/junit/org/apache/tools/ant/types/mappers/RegexpPatternMapperTest.java
index 8f95d63d1..9def8bae5 100644
--- a/src/tests/junit/org/apache/tools/ant/types/mappers/RegexpPatternMapperTest.java
+++ b/src/tests/junit/org/apache/tools/ant/types/mappers/RegexpPatternMapperTest.java
@@ -46,4 +46,14 @@ public class RegexpPatternMapperTest {
     public void testHandleDirSep() {
         buildRule.executeTarget("handle.dirsep");
     }
+
+    /**
+     * Test that if the {@code to} attribute of {@code regexpmapper} contains a backslash
+     * character which isn't followed by a digit (representing regex group) then the backslash
+     * doesn't disappear from the output. See bug 66468 for details
+     */
+    @Test
+    public void testBackslashInTo() {
+        buildRule.executeTarget("to-with-backslash-for-non-groups");
+    }
 }