Skip to content

BddUtils.valuationToStates may crash due to initializing a list with negative initial size

Stack trace:

java.lang.IllegalArgumentException: Illegal Capacity: -2147483648
	at java.base/java.util.ArrayList.<init>(ArrayList.java:160)
	at org.eclipse.escet.common.java.Lists.listc(Lists.java:220)
	at org.eclipse.escet.cif.bdd.utils.BddUtils.valuationToStates(BddUtils.java:473)

The problem is in the following code:

        // Get list to store resulting states, with a decently-approximated initial size.
        ListProductIterator<Object> stateIter = new ListProductIterator<>(valuesPerVar);
        Optional<Long> stateCountOrOverflow = stateIter.getResultSize();
        long cappedStateCount = stateCountOrOverflow.orElse(Long.MAX_VALUE);
        int approxStateCount = (int)cappedStateCount;
        List<Map<CifBddVariable, Object>> states = listc(Math.min(approxStateCount, maxStateCount));

The long to int cast may lead to negative values.