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

centic9 pushed a commit to branch improve_fuzzing_and_avoid_some_npes
in repository https://gitbox.apache.org/repos/asf/poi.git

commit 8294eac0f4b7935eee25bc90afefdef9830dcb38
Author: Dominik Stadler <[email protected]>
AuthorDate: Fri Oct 30 13:46:36 2026 +0100

    Avoid NPE in text-join on blank or null boolean arg
---
 .../poi/ss/formula/atp/TextJoinFunction.java       |  14 +--
 .../poi/ss/formula/atp/TestTextJoinFunction.java   | 124 +++++++++++++++------
 2 files changed, 95 insertions(+), 43 deletions(-)

diff --git 
a/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java 
b/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java
index 90c1192ebe..b3a2fc2910 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java
@@ -42,19 +42,17 @@ import java.util.List;
  */
 final class TextJoinFunction implements FreeRefFunction {
 
-    public static final FreeRefFunction instance = new 
TextJoinFunction(ArgumentsEvaluator.instance);
+    public static final FreeRefFunction instance = new TextJoinFunction();
 
-    private ArgumentsEvaluator evaluator;
-
-    private TextJoinFunction(ArgumentsEvaluator anEvaluator) {
+    private TextJoinFunction() {
         // enforces singleton
-        this.evaluator = anEvaluator;
     }
 
+    @Override
     public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) 
{
         /*
          * Must be at least three arguments:
-         *  - delimiter    Delimiter for joining text arguments
+         *  - delimiter    The delimiter for joining text arguments
          *  - ignoreEmpty  If true, empty strings will be ignored in the join
          *  - text1        First value to be evaluated as text and joined
          *  - text2, etc.  Optional additional values to be evaluated and 
joined
@@ -75,7 +73,7 @@ final class TextJoinFunction implements FreeRefFunction {
 
             // Get the boolean ignoreEmpty argument
             ValueEval ignoreEmptyArg = OperandResolver.getSingleValue(args[1], 
srcRowIndex, srcColumnIndex);
-            boolean ignoreEmpty = 
OperandResolver.coerceValueToBoolean(ignoreEmptyArg, false);
+            Boolean ignoreEmpty = 
OperandResolver.coerceValueToBoolean(ignoreEmptyArg, false);
 
             // Get a list of string values for each text argument
             ArrayList<String> textValues = new ArrayList<>();
@@ -86,7 +84,7 @@ final class TextJoinFunction implements FreeRefFunction {
                     String textValue = 
OperandResolver.coerceValueToString(textArg);
 
                     // If we're not ignoring empty values or if our value is 
not empty, add it to the list
-                    if (!ignoreEmpty || (textValue != null && 
!textValue.isEmpty())) {
+                    if (ignoreEmpty == null || !ignoreEmpty || (textValue != 
null && !textValue.isEmpty())) {
                         textValues.add(textValue);
                     }
                 }
diff --git 
a/poi/src/test/java/org/apache/poi/ss/formula/atp/TestTextJoinFunction.java 
b/poi/src/test/java/org/apache/poi/ss/formula/atp/TestTextJoinFunction.java
index f8eb12a6c8..d0f2b943b2 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/atp/TestTextJoinFunction.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/atp/TestTextJoinFunction.java
@@ -21,57 +21,57 @@ import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.*;
 import org.apache.poi.ss.usermodel.*;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
 
 import static org.apache.poi.ss.util.Utils.addRow;
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.*;
 
 /**
  * Testcase for function TEXTJOIN()
  */
 public class TestTextJoinFunction {
 
-    private Workbook wb;
-    private Sheet sheet;
+    private final Workbook wb = new HSSFWorkbook();
     private FormulaEvaluator evaluator;
-    private Cell textCell1;
-    private Cell textCell2;
-    private Cell numericCell1;
-    private Cell numericCell2;
-    private Cell blankCell;
     private Cell formulaCell;
 
     @BeforeEach
-    public void setUp() throws Exception {
-        wb = new HSSFWorkbook();
+    public void setUp() {
         evaluator = wb.getCreationHelper().createFormulaEvaluator();
 
-        sheet = wb.createSheet("TextJoin");
+        Sheet sheet = wb.createSheet("TextJoin");
         Row row = sheet.createRow(0);
 
-        textCell1 = row.createCell(0);
+        Cell textCell1 = row.createCell(0);
         textCell1.setCellValue("One");
 
-        textCell2 = row.createCell(1);
+        Cell textCell2 = row.createCell(1);
         textCell2.setCellValue("Two");
 
-        blankCell = row.createCell(2);
+        Cell blankCell = row.createCell(2);
         blankCell.setBlank();
 
-        numericCell1 = row.createCell(3);
+        Cell numericCell1 = row.createCell(3);
         numericCell1.setCellValue(1);
 
-        numericCell2 = row.createCell(4);
+        Cell numericCell2 = row.createCell(4);
         numericCell2.setCellValue(2);
 
         formulaCell = row.createCell(100, CellType.FORMULA);
     }
 
+    @AfterEach
+    void tearDown() throws IOException {
+        wb.close();
+    }
+
     @Test
     public void testJoinSingleLiteralText() {
         evaluator.clearAllCachedResultValues();
@@ -172,9 +172,9 @@ public class TestTextJoinFunction {
     
//https://support.microsoft.com/en-us/office/textjoin-function-357b449a-ec91-49d0-80c3-0e8fc845691c
     @Test
     void testMicrosoftExample1() throws IOException {
-        try (HSSFWorkbook wb = initWorkbook1()) {
-            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-            HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
+        try (HSSFWorkbook wbLoc = initWorkbook1()) {
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wbLoc);
+            HSSFCell cell = wbLoc.getSheetAt(0).getRow(0).createCell(100);
             confirmResult(fe, cell, "TEXTJOIN(\", \", TRUE, A2:A8)",
                     "US Dollar, Australian Dollar, Chinese Yuan, Hong Kong 
Dollar, Israeli Shekel, South Korean Won, Russian Ruble");
         }
@@ -182,9 +182,9 @@ public class TestTextJoinFunction {
 
     @Test
     void testMicrosoftExample2() throws IOException {
-        try (HSSFWorkbook wb = initWorkbook2()) {
-            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-            HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
+        try (HSSFWorkbook wbLoc = initWorkbook2()) {
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wbLoc);
+            HSSFCell cell = wbLoc.getSheetAt(0).getRow(0).createCell(100);
             confirmResult(fe, cell, "TEXTJOIN(\", \", TRUE, A2:B8)",
                     "a1, b1, a2, b2, a4, b4, a5, b5, a6, b6, a7, b7");
             confirmResult(fe, cell, "TEXTJOIN(\", \", FALSE, A2:B8)",
@@ -194,9 +194,9 @@ public class TestTextJoinFunction {
 
     @Test
     void testMicrosoftExample3() throws IOException {
-        try (HSSFWorkbook wb = initWorkbook3()) {
-            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-            HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
+        try (HSSFWorkbook wbLoc = initWorkbook3()) {
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wbLoc);
+            HSSFCell cell = wbLoc.getSheetAt(0).getRow(0).createCell(100);
             confirmResult(fe, cell, "TEXTJOIN(A8:D8, TRUE, A2:D7)",
                     
"Tulsa,OK,74133,US;Seattle,WA,98109,US;Iselin,NJ,08830,US;Fort 
Lauderdale,FL,33309,US;Tempe,AZ,85285,US;end");
             confirmResult(fe, cell, "TEXTJOIN(, TRUE, A2:D7)",
@@ -205,8 +205,8 @@ public class TestTextJoinFunction {
     }
 
     private HSSFWorkbook initWorkbook1() {
-        HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFSheet sheet = wb.createSheet();
+        HSSFWorkbook wbLoc = new HSSFWorkbook();
+        HSSFSheet sheet = wbLoc.createSheet();
         addRow(sheet, 0, "Currency");
         addRow(sheet, 1, "US Dollar");
         addRow(sheet, 2, "Australian Dollar");
@@ -215,24 +215,24 @@ public class TestTextJoinFunction {
         addRow(sheet, 5, "Israeli Shekel");
         addRow(sheet, 6, "South Korean Won");
         addRow(sheet, 7, "Russian Ruble");
-        return wb;
+        return wbLoc;
     }
 
     private HSSFWorkbook initWorkbook2() {
-        HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFSheet sheet = wb.createSheet();
+        HSSFWorkbook wbLoc = new HSSFWorkbook();
+        HSSFSheet sheet = wbLoc.createSheet();
         addRow(sheet, 0, "A’s", "B’s");
         for (int i = 1; i <= 7; i++) {
             if (i != 3) {
                 addRow(sheet, i, "a" + i, "b" + i);
             }
         }
-        return wb;
+        return wbLoc;
     }
 
     private HSSFWorkbook initWorkbook3() {
-        HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFSheet sheet = wb.createSheet();
+        HSSFWorkbook wbLoc = new HSSFWorkbook();
+        HSSFSheet sheet = wbLoc.createSheet();
         addRow(sheet, 0, "City", "State", "Postcode", "Country");
         addRow(sheet, 1, "Tulsa", "OK", "74133", "US");
         addRow(sheet, 2, "Seattle", "WA", "98109", "US");
@@ -241,7 +241,7 @@ public class TestTextJoinFunction {
         addRow(sheet, 5, "Tempe", "AZ", "85285", "US");
         addRow(sheet, 6, "end");
         addRow(sheet, 7, ",", ",", ",", ";");
-        return wb;
+        return wbLoc;
     }
 
     private static void confirmResult(HSSFFormulaEvaluator fe, HSSFCell cell, 
String formulaText, String expectedResult) {
@@ -251,4 +251,58 @@ public class TestTextJoinFunction {
         assertEquals(CellType.STRING, result.getCellType());
         assertEquals(expectedResult, result.getStringValue());
     }
+
+    @Test
+    void testDirect() {
+        ValueEval result = TextJoinFunction.instance.evaluate(new ValueEval[]{
+                        new StringEval(","),
+                        BoolEval.TRUE,
+                        new StringEval("t1"),
+                        new StringEval("t2")
+                },
+                new OperationEvaluationContext(null, null, 0, 0, 0, null, 
false));
+
+        assertInstanceOf(StringEval.class, result, "Had: " + result);
+        assertEquals("t1,t2", ((StringEval) result).getStringValue());
+    }
+
+    @Test
+    void testTooLessArgs() {
+        ValueEval result = TextJoinFunction.instance.evaluate(new ValueEval[]{
+                        new StringEval(","),
+                        BoolEval.TRUE
+                },
+                new OperationEvaluationContext(null, null, 0, 0, 0, null, 
false));
+
+        assertInstanceOf(ErrorEval.class, result, "Had: " + result);
+        assertEquals(ErrorEval.VALUE_INVALID, result);
+    }
+
+    @Test
+    void testNPEWithNull() {
+        ValueEval result = TextJoinFunction.instance.evaluate(new ValueEval[]{
+                        new StringEval(","),
+                        null,
+                        new StringEval("t1"),
+                        new StringEval("t2")
+                },
+                new OperationEvaluationContext(null, null, 0, 0, 0, null, 
false));
+
+        assertInstanceOf(StringEval.class, result, "Had: " + result);
+        assertEquals("t1,t2", ((StringEval) result).getStringValue());
+    }
+
+    @Test
+    void testNPEWithBlank() {
+        ValueEval result = TextJoinFunction.instance.evaluate(new ValueEval[]{
+                        new StringEval(","),
+                        BlankEval.instance,
+                        new StringEval("t1"),
+                        new StringEval("t2")
+                },
+                new OperationEvaluationContext(null, null, 0, 0, 0, null, 
false));
+
+        assertInstanceOf(StringEval.class, result, "Had: " + result);
+        assertEquals("t1,t2", ((StringEval) result).getStringValue());
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to