Skip to content

Commit f880d04

Browse files
committed
Fix StringIndexOutOfBoundsException when getValue() is called on Entry without value
1 parent 14f43c1 commit f880d04

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ public List<String> getValues() {
7373
}
7474

7575
public String getValue() {
76-
List<String> values = getValues();
77-
76+
Iterator<String> iterator = getValues().iterator();
7877
StringBuilder builder = new StringBuilder();
79-
for (String value : values) {
80-
builder.append(value).append(" ");
78+
while (iterator.hasNext()) {
79+
builder.append(iterator.next());
80+
if (iterator.hasNext()) {
81+
builder.append(' ');
82+
}
8183
}
82-
String s = builder.toString();
83-
return s.substring(0, s.length()-1);
84+
return builder.toString();
8485
}
8586

8687
}

src/test/java/com/github/odiszapc/nginxparser/NestedTest.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
import static com.github.odiszapc.nginxparser.TestUtils.assertBlock;
2626
import static com.github.odiszapc.nginxparser.TestUtils.assertParam;
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertFalse;
29+
import static org.junit.Assert.assertNotEquals;
30+
import static org.junit.Assert.assertTrue;
2731

2832
public class NestedTest extends ParseTestBase {
2933

@@ -47,17 +51,17 @@ public class NestedTest extends ParseTestBase {
4751
appIt = application.iterator();
4852
assertParam(appIt.next(), "live", "off");
4953

50-
Assert.assertFalse(it.hasNext());
54+
assertFalse(it.hasNext());
5155
}
5256

5357
@Test
5458
public void testC1find() throws Exception {
5559
NgxConfig conf = parse("nested/c1.conf");
5660
List<NgxEntry> result = conf.findAll(NgxConfig.PARAM, "rtmp", "server", "application", "live");
57-
Assert.assertEquals(result.size(), 2);
61+
assertEquals(result.size(), 2);
5862
assertParam(result.get(0), "live", "on");
5963
assertParam(result.get(1), "live", "off");
60-
Assert.assertNotEquals(result.get(0), result.get(1));
64+
assertNotEquals(result.get(0), result.get(1));
6165
}
6266

6367
@Test
@@ -72,23 +76,23 @@ public void testC1findEvery() throws Exception {
7276
assertBlock(s1, "server");
7377
NgxParam param = s1.findParam("application", "live");
7478
assertParam(param, "live");
75-
Assert.assertEquals(param.getValue(), "on");
79+
assertEquals(param.getValue(), "on");
7680

7781
NgxBlock s2 = (NgxBlock) it.next();
7882
assertBlock(s2, "server");
7983
NgxParam param2 = s2.findParam("application", "live");
8084
assertParam(param2, "live");
81-
Assert.assertEquals(param2.getValue(), "off");
85+
assertEquals(param2.getValue(), "off");
8286
}
8387

8488
@Test
8589
public void testC1findBlock() throws Exception {
8690
NgxConfig conf = parse("nested/c1.conf");
8791
List<NgxEntry> result = conf.findAll(NgxConfig.BLOCK, "rtmp", "server", "application");
88-
Assert.assertEquals(result.size(), 2);
92+
assertEquals(result.size(), 2);
8993
assertBlock(result.get(0), "application", "myapp");
9094
assertBlock(result.get(1), "application", "myapp2");
91-
Assert.assertNotEquals(result.get(0), result.get(1));
95+
assertNotEquals(result.get(0), result.get(1));
9296
}
9397

9498
@Test
@@ -101,4 +105,16 @@ public void testC1findBlock() throws Exception {
101105
assertParam(it.next(), "set_if_empty", "$name", "\"Anonymous\"");
102106
assertParam(it.next(), "memc_pass", "127.0.0.1:11211");
103107
}
108+
109+
@Test
110+
public void testС5() throws Exception {
111+
NgxConfig conf = parse("nested/c5.conf");
112+
NgxBlock loc = assertBlock(conf.findBlock("http", "server", "location"), "location", "/foo");
113+
Iterator<NgxEntry> it = loc.iterator();
114+
NgxEntry entry = it.next();
115+
assertTrue(entry instanceof NgxParam);
116+
assertEquals(((NgxParam) entry).getName(), "stub_status");
117+
assertEquals(0, ((NgxParam) entry).getValues().size());
118+
assertEquals("", ((NgxParam) entry).getValue());
119+
}
104120
}

src/test/resources/nested/c5.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
http {
2+
server {
3+
location /foo {
4+
stub_status;
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)