Repository navigation
SURF-1408: Bound user-controlled allocation sizes in apoc.text, apoc.number.exact and apoc.coll - #974
Conversation
343c593 to
832a8d6
Compare
There was a problem hiding this comment.
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.
d02692b to
9f75860
Compare
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.
|
@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. |
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.ProcedureMemoryAPI before allocating anything, so dbms.memory.transaction.* decides what is allowed and the cost is visible inSHOW 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 ordbms.killQuerycould 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 andtoFloatare deliberately untouched, as neither materialises a plain string.apoc.coll.combinations- computes the result cardinality with a saturating binomial and charges it before building, socombinations(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 to2and 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 toINTEGER, so the published signatures are unchanged.apoc.text.repeat(null, n)therefore reports "'item' must not be null" rather than theNullPointerExceptionit used to throw.Resolves: SURF-1408, SURF-1429