Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/eu/autogex/core/AbstractAutomatonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
public abstract class AbstractAutomatonBuilder<
B extends AbstractAutomatonBuilder<B, A>, A extends Automaton> {

/** Maximum allowed states to prevent State Explosion (DoS) during manual construction */
protected static final int MAX_STATES = 10000;

protected final Map<String, State> states = new HashMap<>();
protected final Set<State> finalStates = new HashSet<>();
protected State initialState;
Expand All @@ -37,8 +40,15 @@ protected AbstractAutomatonBuilder() {
* @param name The name of the state.
* @param isFinal True if the state is an accepting state.
* @return The current builder instance.
* @throws IllegalStateException if adding the state would exceed MAX_STATES.
*/
public B addState(String name, boolean isFinal) {
if (states.size() >= MAX_STATES && !states.containsKey(name)) {
throw new IllegalStateException(
"Exceeded maximum allowed states of "
+ MAX_STATES
+ " (Security: DoS prevention).");
}
State state = new State(name, isFinal);
states.put(name, state);
if (isFinal) {
Expand Down