Skip to content

Commit d6686c9

Browse files
Added: introduced constants for defining the upper limit
1 parent dd36d1d commit d6686c9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLAnchorReplayingParser.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public AnchorContext(String anchor) {
4242
}
4343
}
4444

45+
/**
46+
* the maximum number of events that can be replayed
47+
*/
48+
public static final int MAX_EVENTS = 9999;
49+
50+
/**
51+
* the maximum limit of anchors to remember
52+
*/
53+
public static final int MAX_ANCHORS = 9999;
54+
55+
/**
56+
* the maximum limit of merges to follow
57+
*/
58+
public static final int MAX_MERGES = 9999;
59+
4560
/**
4661
* Remembers when a merge has been started in order to skip the corresponding
4762
* sequence end which needs to be excluded
@@ -73,9 +88,12 @@ public YAMLAnchorReplayingParser(IOContext ctxt, int parserFeatures, int formatF
7388
}
7489

7590
private void finishContext(AnchorContext context) {
91+
if (referencedObjects.size() + 1 > MAX_REFS) throw new IllegalStateException("too many references in the document");
7692
referencedObjects.put(context.anchor, context.events);
7793
if (!tokenStack.isEmpty()) {
78-
tokenStack.peek().events.addAll(context.events);
94+
List<Event> events = tokenStack.peek().events;
95+
if (events.size() + context.events.size() > MAX_EVENTS) throw new IllegalStateException("too many events to replay");
96+
events.addAll(context.events);
7997
}
8098
}
8199

@@ -118,6 +136,7 @@ protected Event getEvent() {
118136
AliasEvent alias = (AliasEvent) event;
119137
List<Event> events = referencedObjects.get(alias.getAnchor());
120138
if (events != null) {
139+
if (refEvents.size() + events.size() > MAX_EVENTS) throw new IllegalStateException("too many events to replay");
121140
refEvents.addAll(events);
122141
return refEvents.removeFirst();
123142
}
@@ -130,6 +149,7 @@ protected Event getEvent() {
130149
AnchorContext context = new AnchorContext(anchor);
131150
context.events.add(event);
132151
if (event instanceof CollectionStartEvent) {
152+
if (tokenStack.size() + 1 > MAX_ANCHORS) throw new IllegalStateException("too many anchors in the document");
133153
tokenStack.push(context);
134154
} else {
135155
// directly store it
@@ -145,6 +165,7 @@ protected Event getEvent() {
145165
// expect next node to be a map
146166
Event next = getEvent();
147167
if (next instanceof MappingStartEvent) {
168+
if (mergeStack.size() + 1 > MAX_MERGES) throw new IllegalStateException("too many merges in the document");
148169
mergeStack.push(globalDepth);
149170
return getEvent();
150171
}
@@ -154,6 +175,7 @@ protected Event getEvent() {
154175

155176
if (!tokenStack.isEmpty()) {
156177
AnchorContext context = tokenStack.peek();
178+
if (context.events.size() + 1 > MAX_EVENTS) throw new IllegalStateException("too many events to replay");
157179
context.events.add(event);
158180
if (event instanceof CollectionStartEvent) {
159181
++context.depth;

0 commit comments

Comments
 (0)