Skip to content

Commit

Permalink
WW-5451 Fixes NPE when iterator starts with null
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Jul 31, 2024
1 parent f977f0c commit 479a9d8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ public boolean start(Writer writer) {
if ((iterator != null) && iterator.hasNext()) {
Object currentValue = iterator.next();
stack.push(currentValue);
threadAllowlist.allowClass(currentValue.getClass());

if (currentValue != null) {
threadAllowlist.allowClass(currentValue.getClass());
}

String var = getVar();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ public List<String> getItems() {
assertEquals("1, 2, , 4, ", out.getBuffer().toString());
}

public void testIteratorWithNullsOnly() {
// given
stack.push(new FooAction() {
private final List<String> items = Arrays.asList(null, null, null);

public List<String> getItems() {
return items;
}
});

StringWriter out = new StringWriter();

ic.setValue("items");
ic.setVar("val");
Property prop = new Property(stack);

ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);

String body = ", ";

// when
assertTrue(ic.start(out));

for (int i = 0; i < 3; i++) {
prop.start(out);
prop.end(out, body);
ic.end(out, null);
}

// then
assertEquals(", , , ", out.getBuffer().toString());
}

public void testIteratorWithDifferentLocale() {
// given
ActionContext.getContext().withLocale(new Locale("fa_IR"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.mockobjects.servlet.MockBodyContent;
import com.mockobjects.servlet.MockJspWriter;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.collections.ListUtils;

import javax.servlet.jsp.JspException;
Expand Down Expand Up @@ -722,6 +721,41 @@ public void testCounterWithList() throws JspException {
validateCounter(new String[]{"a", "b", "c"});
}

public void testNullElements() throws JspException {
Foo foo = new Foo();
foo.setArray(new String[3]);

stack.push(foo);
tag.setValue("array");
tag.setVar("anId");

// one
int result = tag.doStartTag();
assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
assertNull(stack.peek());
assertNull(stack.getContext().get("anId"));

tag.doInitBody();

// two
result = tag.doAfterBody();
assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
assertNull(stack.peek());
assertNull(stack.getContext().get("anId"));

// three
result = tag.doAfterBody();
assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
assertNull(stack.peek());
assertNull(stack.getContext().get("anId"));

result = tag.doAfterBody();
assertEquals(TagSupport.SKIP_BODY, result);

result = tag.doEndTag();
assertEquals(TagSupport.EVAL_PAGE, result);
}

public void testCounterWithArray() throws JspException {
Foo foo = new Foo();
foo.setArray(new String[]{"a", "b", "c", "d"});
Expand Down

0 comments on commit 479a9d8

Please sign in to comment.