Skip to content

Commit eba3e39

Browse files
committed
Fix an IOOB when HTML root cleared and then attributes added
Fixes #1611
1 parent 9d538e6 commit eba3e39

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ jsoup changelog
7474
* Bugfix [Fuzz]: Fix a potential stack-overflow in the parser given crafted HTML, when the parser looped in the
7575
InSelectInTable state.
7676

77+
* Bugfix [Fuzz]: Fix an IOOB when the HTML root was cleared from the stack and then attributes were merged onto it.
78+
<https://github.com/jhy/jsoup/issues/1611>
79+
7780
*** Release 1.14.1 [2021-Jul-10]
7881
* Change: updated the minimum supported Java version from Java 7 to Java 8.
7982

src/main/java/org/jsoup/parser/HtmlTreeBuilderState.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,15 @@ private boolean inBodyStartTag(Token t, HtmlTreeBuilder tb) {
340340
break;
341341
case "html":
342342
tb.error(this);
343-
// merge attributes onto real html
344-
Element html = tb.getStack().get(0);
345-
if (startTag.hasAttributes()) {
346-
for (Attribute attribute : startTag.attributes) {
347-
if (!html.hasAttr(attribute.getKey()))
348-
html.attributes().put(attribute);
343+
// merge attributes onto real html (if present)
344+
stack = tb.getStack();
345+
if (stack.size() > 0) {
346+
Element html = tb.getStack().get(0);
347+
if (startTag.hasAttributes()) {
348+
for (Attribute attribute : startTag.attributes) {
349+
if (!html.hasAttr(attribute.getKey()))
350+
html.attributes().put(attribute);
351+
}
349352
}
350353
}
351354
break;

src/test/java/org/jsoup/integration/FuzzFixesTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,16 @@ public void overflow1607() throws IOException {
193193
Document docXml = Jsoup.parse(new FileInputStream(in), "UTF-8", "https://example.com", Parser.xmlParser());
194194
assertNotNull(docXml);
195195
}
196+
197+
@Test
198+
public void oob() throws IOException {
199+
// https://github.com/jhy/jsoup/issues/1611
200+
File in = ParseTest.getFile("/fuzztests/1611.html.gz");
201+
202+
Document doc = Jsoup.parse(in, "UTF-8");
203+
assertNotNull(doc);
204+
205+
Document docXml = Jsoup.parse(new FileInputStream(in), "UTF-8", "https://example.com", Parser.xmlParser());
206+
assertNotNull(docXml);
207+
}
196208
}
817 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)