Skip to content

SURF-1408: Bound user-controlled allocation sizes in apoc.text, apoc.number.exact and apoc.coll - #974

Merged
JoelBergstrand merged 2 commits into
devfrom
surf-1408-user-controlled-sizes-drive-untracked-multi-gigabyte
Sep 29, 2026
Merged

JoelBergstrand merged 2 commits into
devfrom
surf-1408-user-controlled-sizes-drive-untracked-multi-gigabyte

Conversation

@JoelBergstrand

@JoelBergstrand JoelBergstrand commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

These functions fed caller-supplied sizes straight into heap allocations and loop bounds, with no upper bound and no overflow-checked narrowing. Neo4j's transaction memory tracker does not see plain Java objects allocated inside a procedure body, so the query was never aborted and the JVM hit OutOfMemoryError - which, with the recommended -XX:+ExitOnOutOfMemoryError, takes down the whole DBMS process.

Each site now estimates its result size with overflow-checked arithmetic and charges it to the transaction memory budget through the preview org.neo4j.procedure.memory.ProcedureMemory API before allocating anything, so dbms.memory.transaction.* decides what is allowed and the cost is visible in SHOW TRANSACTIONS. Degenerate arguments are rejected up front with a message naming the argument.

Shared helper:

  • common/.../apoc/util/ProcedureMemoryUtil - one home for the String size estimate and for the charge-then-allocate sequence. Closes the tracker when a charge is refused, which otherwise leaks it for the rest of the transaction, and saturates rather than overflowing so an unrepresentable size reports its size instead of an arithmetic overflow.

Per function:

  • apoc.text.repeat - short-circuits an empty item, which also removes an infinite, zero-allocation loop that no query timeout or dbms.killQuery could stop: a count above 2^31 wrapped the int counter while still comparing less than the long bound. Replaces the loop with String.repeat, and rejects a negative count, a count no String could hold, and a count whose product with the item length exceeds the maximum string length.
  • apoc.text.lpad / rpad - rejects an empty delimiter, which used to surface as StringIndexOutOfBoundsException, and builds the result once instead of allocating both a pre-sized StringBuilder and a separate char array, which together asked for roughly twice the final size.
  • apoc.text.random - rejects a negative length, and an empty character set after the A-Z/a-z/0-9 expansion.
  • apoc.text.format - the output size is driven by the format string rather than by any argument, so this is the one site that cannot estimate up front. Charges incrementally through TrackedAppendable, which accounts for the buffer as its capacity grows.
  • apoc.number.exact.add / sub / mul / div - charges the eventual plain-string length before the arithmetic. All four share the sink: 1e1000000000 parses in microseconds but expands to a two-gigabyte string. toInteger and toFloat are deliberately untouched, as neither materialises a plain string.
  • apoc.coll.combinations - computes the result cardinality with a saturating binomial and charges it before building, so combinations(range(1,40), 20) fails in microseconds rather than after seconds of allocation. Selection sizes outside the int range are now rejected instead of silently truncating: combinations([1,2,3], 4294967298) used to narrow to 2 and return a result.

Null arguments to any of these functions are now rejected by name, checked left to right. The numeric arguments move from long to Long so that a null reaches APOC code at all: previously it stopped at the framework's argument coercion and surfaced as "ClassCastException: NoValue cannot be cast to NumberValue", naming internal Neo4j value classes. Both types map to INTEGER, so the published signatures are unchanged. apoc.text.repeat(null, n) therefore reports "'item' must not be null" rather than the NullPointerException it used to throw.

Resolves: SURF-1408, SURF-1429

@JoelBergstrand JoelBergstrand added bug Something isn't working dev labels Sep 24, 2026
@JoelBergstrand
JoelBergstrand force-pushed the surf-1408-user-controlled-sizes-drive-untracked-multi-gigabyte branch 2 times, most recently from 343c593 to 832a8d6 Compare September 24, 2026 09:48

@loveleif loveleif left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good

@fredrik-neo4j fredrik-neo4j left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! claude gave me a tip that the .div function might have something that needs overlooking but i couldnt figure out exactly what that was, so ill raise it here because perhaps you can see if its a thing. something about .div not being handled in this fix, but i cant see how it differs from the other proposals.

heres what it told me:
apoc.number.exact.div is still unbounded through precision.** quotientLength (core/src/main/java/apoc/number/exact/Exact.java:202) bounds the quotient only from the operands' plain-string lengths and ignores the caller-supplied precision. createMathContext feeds precision.intValue() straight into MathContext, so a non-terminating quotient is rounded to exactly precision significant digits. apoc.number.exact.div('1','3',2000000000) charges only a few hundred bytes, then a.divide(b, mc).toPlainString() materialises a multi-gigabyte string — the failure this PR exists to prevent. Fold precision into quotientLength and add a case such as assertMemoryLimitExceeded("RETURN apoc.number.exact.div('1','3',2000000000) AS value").

…xact

Pin the SURF-1429 reproduction add('1e2000000000','0'), a scale of
Integer.MIN_VALUE whose int absolute value is still negative, and mul/div
with precision 10, since a bounded precision caps the unscaled digits and
not the scale. Dropping the long cast in plainStringLength makes the
Integer.MIN_VALUE case fail.
@JoelBergstrand
JoelBergstrand force-pushed the surf-1408-user-controlled-sizes-drive-untracked-multi-gigabyte branch from d02692b to 9f75860 Compare September 28, 2026 10:17
apoc.number.exact.div charged only for the operands' lengths, but with a
non-zero precision a non-terminating quotient is rounded to exactly that many
significant digits: div('1', '3', 2000000000) charged a few dozen bytes and
then built a two-billion-digit string. With a precision, the quotient estimate
is now the precision plus both operands' lengths, which bound the leading or
trailing zeros their exponents can add. Precision 0 keeps the exact-quotient
estimate.
@JoelBergstrand

Copy link
Copy Markdown
Contributor Author

@fredrik-neo4j I added a commit addressing the comment from the agent. It was a real concern.

I merged the PRs in the wrong order so after rebase it is only the latest commit and some additional testing left on this branch.

@JoelBergstrand
JoelBergstrand merged commit bd050b6 into dev Sep 29, 2026
7 checks passed
@JoelBergstrand
JoelBergstrand deleted the surf-1408-user-controlled-sizes-drive-untracked-multi-gigabyte branch September 29, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dev

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants