Skip to content

Java and Python graph clone checks crash on a null neighbors list #736

Open

Description

@Urdan0117

A Clone Graph answer that returns a node whose neighbors list itself is null, rather than a list holding a null element (#725), fails inside the Java and Python checkers, while JavaScript treats the same list as empty and grades the answer as a plain mismatch. Raised by @Wu-1127 in review of #729, which is limited to the null element.

Observed

At fe04f8f plus the change in #729, with a one-case graphNode spec (input [[2,3],[1,3],[1,2]]) and a candidate that builds the triangle and then sets the neighbors of node 2 to null:

Language Case result
Java error java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "<local3>.neighbors" is null
Python error TypeError: 'NoneType' object is not iterable (run under CPython; Pyodide was not reachable on my machine)
JavaScript fails as a mismatch: got [[2,3],[],[1,2]], expected [[2,3],[1,3],[1,2]]

Java was compiled from generateHarness output with javac; JavaScript was run through runBrowserTests in Chromium.

C++ holds neighbors in a std::vector, which cannot be null. C walks numNeighbors entries, so a NULL array with a count of 0 is already an empty list; a NULL array with a nonzero count is a candidate whose own fields disagree, which the harness cannot detect and this issue does not cover.

Cause

  • Java, graphJson in the Java template, web/compiler-explorer.js:782: both for (Node next : cur.neighbors) loops, the one that queues neighbors and the one that builds rows, iterate a null list.
  • Python, _graph_to_adjacency, web/runners.js: the queue loop at line 1149 reads getattr(current, "neighbors", []) or [], but the row building at line 1152 reads by_value[value].neighbors without the or [].
  • JavaScript, graphToAdjacency, web/runners.js:791 and :796: both reads fall back with || [] (current.neighbors || [] when queueing, byValue.get(value).neighbors || [] when building rows), so a null list is an empty one.

Proposed change

Treat a null neighbors list as empty in Java and Python, as JavaScript already does: in Java, read cur.neighbors == null ? List.of() : cur.neighbors in both loops; in Python, add or [] to the row building. A node with no neighbors and a node whose list is null then serialize the same way in all three, and a wrong answer of this shape fails as a mismatch instead of an exception from the checker. Refusing a null list with its own message would also make the three agree, but it would fail an answer that is otherwise correct for a node with no neighbors, which JavaScript accepts today.

Tests: a nullList scenario in tests/browser/graph-clone-identity.test.js for Java, JavaScript and Python, asserting the same mismatch in each.

This builds on #729 and waits for it to merge, since both change the same serializers.

Activity

  1. jserv commented on Nov 16, 2026

    @jserv
    Contributor

    @Urdan0117 Confirmed on main: both for (Node next : cur.neighbors) loops in the Java graphJson (web/compiler-explorer.js) and the Python row builder in web/runners.js iterate a null neighbors list, while JavaScript treats it as empty. Accepted as scoped, in its own pull request apart from #729: treat a null neighbors list as empty in Java and Python as JavaScript does, with one test per language. Open the pull request with Close #736.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions