Skip to content

Commit ffdafed

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

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ 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+
60+
/**
61+
* the maximum limit of references to remember
62+
*/
63+
public static final int MAX_REFS = 9999;
64+
4565
/**
4666
* Remembers when a merge has been started in order to skip the corresponding
4767
* sequence end which needs to be excluded
@@ -73,9 +93,12 @@ public YAMLAnchorReplayingParser(IOContext ctxt, int parserFeatures, int formatF
7393
}
7494

7595
private void finishContext(AnchorContext context) {
96+
if (referencedObjects.size() + 1 > MAX_REFS) throw new IllegalStateException("too many references in the document");
7697
referencedObjects.put(context.anchor, context.events);
7798
if (!tokenStack.isEmpty()) {
78-
tokenStack.peek().events.addAll(context.events);
99+
List<Event> events = tokenStack.peek().events;
100+
if (events.size() + context.events.size() > MAX_EVENTS) throw new IllegalStateException("too many events to replay");
101+
events.addAll(context.events);
79102
}
80103
}
81104

@@ -118,6 +141,7 @@ protected Event getEvent() {
118141
AliasEvent alias = (AliasEvent) event;
119142
List<Event> events = referencedObjects.get(alias.getAnchor());
120143
if (events != null) {
144+
if (refEvents.size() + events.size() > MAX_EVENTS) throw new IllegalStateException("too many events to replay");
121145
refEvents.addAll(events);
122146
return refEvents.removeFirst();
123147
}
@@ -130,6 +154,7 @@ protected Event getEvent() {
130154
AnchorContext context = new AnchorContext(anchor);
131155
context.events.add(event);
132156
if (event instanceof CollectionStartEvent) {
157+
if (tokenStack.size() + 1 > MAX_ANCHORS) throw new IllegalStateException("too many anchors in the document");
133158
tokenStack.push(context);
134159
} else {
135160
// directly store it
@@ -145,6 +170,7 @@ protected Event getEvent() {
145170
// expect next node to be a map
146171
Event next = getEvent();
147172
if (next instanceof MappingStartEvent) {
173+
if (mergeStack.size() + 1 > MAX_MERGES) throw new IllegalStateException("too many merges in the document");
148174
mergeStack.push(globalDepth);
149175
return getEvent();
150176
}
@@ -154,6 +180,7 @@ protected Event getEvent() {
154180

155181
if (!tokenStack.isEmpty()) {
156182
AnchorContext context = tokenStack.peek();
183+
if (context.events.size() + 1 > MAX_EVENTS) throw new IllegalStateException("too many events to replay");
157184
context.events.add(event);
158185
if (event instanceof CollectionStartEvent) {
159186
++context.depth;

0 commit comments

Comments
 (0)