Skip to content

Java, JavaScript and Python graph clone checks crash on a null neighbor #725

Closed

Description

@Urdan0117

A Clone Graph answer whose copy holds a null neighbor gets "Returned graph has a NULL neighbor." in C and C++, but a bare exception in Java, JavaScript and Python. The candidate sees an error raised inside the checker instead of what is wrong with the returned graph.

Observed

At fe04f8f, with a one-case graphNode spec (input [[2,3],[1,3],[1,2]]) and a candidate that builds the triangle and then sets the first neighbor of node 1 to null:

Language Where it runs Case error
C Compiler Explorer Returned graph has a NULL neighbor.
C++ Compiler Explorer Returned graph has a NULL neighbor.
Java Compiler Explorer java.lang.NullPointerException
JavaScript browser Cannot read properties of null (reading 'val')
Python browser (Pyodide) AttributeError: 'NoneType' object has no attribute 'val'

C, C++ and Java were compiled locally from generateHarness output (g++ -std=c++20, javac). JavaScript was run through runBrowserTests in Chromium. For Python, Pyodide could not be fetched on my machine, so _graph_to_adjacency was run under CPython; the Pyodide run is still to do.

Cause

C (json_graph) and C++ (graphToJson) check each neighbor for null before queueing it. The other three do not:

  • Java, graphJson in the Java template, web/compiler-explorer.js:782: q.add(next) hands the null to an ArrayDeque, which refuses null elements and throws before any check of the harness runs.
  • JavaScript, graphToAdjacency in JS_RUNNER_SOURCE, web/runners.js:791: the null is queued, skipped by if (!current) continue;, and then read as neighbor.val while the rows are built.
  • Python, _graph_to_adjacency, web/runners.js:1149: same as JavaScript, neighbor.val on None.

Proposed change

Refuse a null neighbor with the message C and C++ already use, at the point where each serializer queues neighbors.

Java (excerpt of the one-line template at web/compiler-explorer.js:782):

-for (Node next : cur.neighbors) q.add(next);
+for (Node next : cur.neighbors) { if (next == null) throw new IllegalArgumentException("Returned graph has a NULL neighbor."); q.add(next); }

javaCase turns the exception into the case's error, so it reads java.lang.IllegalArgumentException: Returned graph has a NULL neighbor., the same shape as the existing deep-copy and duplicate-ID refusals.

JavaScript, web/runners.js:791:

-          for (const neighbor of current.neighbors || []) queue.push(neighbor);
+          for (const neighbor of current.neighbors || []) {
+            if (neighbor == null) throw new TypeError("Returned graph has a NULL neighbor.");
+            queue.push(neighbor);
+          }

Python, web/runners.js:1149:

-        queue.extend(getattr(current, "neighbors", []) or [])
+        for neighbor in getattr(current, "neighbors", []) or []:
+            if neighbor is None:
+                raise TypeError("Returned graph has a NULL neighbor.")
+            queue.append(neighbor)

Tests, tests/browser/graph-clone-identity.test.js: candidate() gains a null-neighbor line for JavaScript, Python, C++ and Java, mirroring the C one (a->neighbors[0] = NULL;); the toolchain suite runs the nullNeighbor scenario for every compiled language instead of only C, with endsWith in place of equal because Java prefixes the exception class, as the duplicate-ID assertion already does; the browser suite gains one nullNeighbor row.

With the test changes and without the fix, the JavaScript and Java null-neighbor cases fail and C and C++ pass; with the fix all of them pass. The whole change is 3 files, +31/-10.

What it does not touch

C and C++ code, the input builders (graphNode and graphFromAdjacency), how results are compared, the problem bank, and the serializers for every other node type.

Overlap with open work

None of the 17 open pull requests changes web/runners.js or tests/browser/graph-clone-identity.test.js, or the Java graphJson. The nearest are #684 (tests for C++ delegating constructors in tests/browser/compiler-explorer.test.js), #664 (findTreeNode in the C harness) and #658 (how strings are written as C and C++ literals); each is in web/compiler-explorer.js or its tests but outside the graph serializers.

Related: #464 made C report graph refusals as the case's error, and #710 added the wrong-root check to all five languages; this closes the same kind of gap for a null neighbor.

Activity

  1. jserv commented on Nov 16, 2026

    @jserv
    Contributor

    Provide more analysis and detail about the code changes so that other developers can determine whether your proposed changes overlap with their work.

  2. Urdan0117 commented on Nov 16, 2026

    @Urdan0117
    ContributorAuthor

    Updated the issue body with the cause at file:line for each language, the proposed diff, what the change leaves untouched, and the open pull requests that share web/compiler-explorer.js. JavaScript is now observed rather than inferred.

  3. added 5 commits that reference this issue on Nov 16, 2026
    1d1ac5b
    8ef77fa
    afe4667
    1d5ddc2
    e86f0ec
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