neo4j-contrib / neo4j-apoc-procedures

Awesome Procedures On Cypher for Neo4j - codenamed "apoc"                     If you like it, please ★ above ⇧            
https://neo4j.com/labs/apoc
Apache License 2.0
1.71k stars 493 forks source link

apoc.generate.er fails with IndexOutOfBoundsException #1747

Closed pecollet closed 3 years ago

pecollet commented 3 years ago

Expected Behavior (Mandatory)

apoc.generate.er goes through and generates the graph

Actual Behavior (Mandatory)

How to Reproduce the Problem

call apoc.generate.er(510000,750000, 'Node','HAS')

Steps (Mandatory)

  1. call cypher above

Screenshots (where it's possibile)

er_bug

Specifications (Mandatory)

Versions

pecollet commented 3 years ago

in ErdosRenyiRelationshipGenerator :

protected List<Pair<Integer, Integer>> doGenerateEdges() {
        long threshold = getConfiguration().getNumberOfEdges() * 4;
        long potentialEdges = getConfiguration().getNumberOfNodes() * (getConfiguration().getNumberOfNodes() - 1); 
        if (threshold > potentialEdges) {
            return doGenerateEdgesWithOmitList(); // Make sure to avoid edges
        }

The integer multiplication of numberOfNodes can silently overflow and produce negative values, causing the code to branch into doGenerateEdgesWithOmitList() and then access negative list indexes. => convert to long before multiplying

The bug actually affects calls to the procedure with numberOfNodes such that numberOfNodes * (numberOfNodes-1) overflows as a negative int (and when it overflows positive it may also do unexpected things). So nothing to do with 500000 as my tests made me believe. It can happen for any value over 46341 which is the limit at which the integer multiplication starts overflowing.