Skip to content

Conversation

@dyrpsf
Copy link
Contributor

@dyrpsf dyrpsf commented Feb 1, 2026

Fixes #248.

Deriving units for a lambda expression while its child list is still empty
(e.g., immediately after the lambda node is constructed, before addChild()
has been called) could cause an IndexOutOfBoundsException in
UnitsCompiler.lambda(List<ASTNode2>).

Previously, lambda(...) always did:

values.get(values.size() - 1)

without checking whether values was empty, so an empty child list led to a
call to values.get(-1).

Changes

  1. Updated UnitsCompiler.lambda(List values) to guard against an empty list:

    • If values is null or empty, the method now returns invalid(), i.e., an ASTNode2Value whose UnitDefinition is marked as INVALID. This avoids out-of-bounds access while signalling that units for a bare lambda expression (without arguments/body yet) are unknown at that time.
  2. For non-empty values, behaviour is unchanged:

    • All but the last child are treated as arguments and stored in namesToUnits.

    • The units of the last child (the body) are used as the lambda’s result units.

Tests

  1. Added UnitsCompilerLambdaTest under core/test/org/sbml/jsbml/math/compiler:

    • testLambdaWithNoChildrenDoesNotThrow():

      • Calls new UnitsCompiler(3, 1).lambda(Collections.<ASTNode2>emptyList()).

      • Asserts that no IndexOutOfBoundsException is thrown.

      • Asserts that the returned units are marked as invalid.

  2. Ran:

mvn -pl core -Dtest=UnitsCompilerLambdaTest test
mvn -pl core test

Both complete with BUILD SUCCESS on Java 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deriving a unit for a lambda expression may cause an out of bounds exception

1 participant