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

bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new bbd63e6ead avaScript lpad and rpad loop forever when the filler is an 
empty string, fixes #9301 (#9304)
bbd63e6ead is described below

commit bbd63e6ead482979e5d07b4e951416bdb8ed5016
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Fri Nov 6 07:09:05 2026 +0100

    avaScript lpad and rpad loop forever when the filler is an empty string, 
fixes #9301 (#9304)
---
 .../javascript/ScriptValuesAddedFunctions.java     |  6 ++++--
 .../javascript/ScriptValuesAddedFunctionsTest.java | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git 
a/plugins/transforms/javascript/src/main/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctions.java
 
b/plugins/transforms/javascript/src/main/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctions.java
index 4eaa1ef245..07ae041268 100644
--- 
a/plugins/transforms/javascript/src/main/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctions.java
+++ 
b/plugins/transforms/javascript/src/main/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctions.java
@@ -862,7 +862,8 @@ public class ScriptValuesAddedFunctions extends 
ScriptableObject {
         String filler = Context.toString(argList[1]);
         int size = (int) Context.toNumber(argList[2]);
 
-        while (valueToPad.length() < size) {
+        // an empty filler can never reach the size
+        while (!filler.isEmpty() && valueToPad.length() < size) {
           valueToPad = filler + valueToPad;
         }
         return valueToPad;
@@ -886,7 +887,8 @@ public class ScriptValuesAddedFunctions extends 
ScriptableObject {
         String filler = Context.toString(argList[1]);
         int size = (int) Context.toNumber(argList[2]);
 
-        while (valueToPad.length() < size) {
+        // an empty filler can never reach the size
+        while (!filler.isEmpty() && valueToPad.length() < size) {
           valueToPad = valueToPad + filler;
         }
         return valueToPad;
diff --git 
a/plugins/transforms/javascript/src/test/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctionsTest.java
 
b/plugins/transforms/javascript/src/test/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctionsTest.java
index 2f68ceabee..61fc306579 100644
--- 
a/plugins/transforms/javascript/src/test/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctionsTest.java
+++ 
b/plugins/transforms/javascript/src/test/java/org/apache/hop/pipeline/transforms/javascript/ScriptValuesAddedFunctionsTest.java
@@ -19,7 +19,9 @@ package org.apache.hop.pipeline.transforms.javascript;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
 
+import java.time.Duration;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.Calendar;
@@ -31,6 +33,26 @@ import org.mozilla.javascript.EvaluatorException;
 class ScriptValuesAddedFunctionsTest {
   private static final String FORMATTER = "yyyy-MM-dd HH:mm:ss";
 
+  @Test
+  void padWithAnEmptyFillerReturnsTheValue() {
+    Object[] args = new Object[] {"ab", "", 5.0};
+
+    assertTimeoutPreemptively(
+        Duration.ofSeconds(5),
+        () -> {
+          assertEquals("ab", ScriptValuesAddedFunctions.lpad(null, null, args, 
null));
+          assertEquals("ab", ScriptValuesAddedFunctions.rpad(null, null, args, 
null));
+        });
+  }
+
+  @Test
+  void padFillsUpToTheSize() {
+    Object[] args = new Object[] {"ab", "*", 4.0};
+
+    assertEquals("**ab", ScriptValuesAddedFunctions.lpad(null, null, args, 
null));
+    assertEquals("ab**", ScriptValuesAddedFunctions.rpad(null, null, args, 
null));
+  }
+
   @Test
   void testTruncDate() {
     LocalDateTime time = LocalDateTime.of(2025, 2, 15, 11, 11, 11, 123);

Reply via email to