Skip to content

Report a null graph neighbor in every language - #729

Open
Urdan0117 wants to merge 1 commit into
sysprog21:mainfrom
Urdan0117:fix/graph-null-neighbor
Open

Urdan0117 wants to merge 1 commit into
sysprog21:mainfrom
Urdan0117:fix/graph-null-neighbor

Conversation

@Urdan0117

@Urdan0117 Urdan0117 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

A Clone Graph answer whose copy holds a null neighbor got Returned graph has a NULL neighbor. in C and C++, but an exception raised inside the checker in the other three languages: java.lang.NullPointerException from the ArrayDeque in the Java graphJson, and Cannot read properties of null (reading 'val') or AttributeError: 'NoneType' object has no attribute 'val' from the JavaScript and Python serializers in web/runners.js, which skip the null in the queue and then read neighbor.val while building rows.

Each of the three now refuses the null where it queues neighbors, with the message C and C++ already use, so an answer whose only fault is a null neighbor gets the same case error in all five languages. Java reports it as java.lang.IllegalArgumentException: Returned graph has a NULL neighbor. and Python as TypeError: Returned graph has a NULL neighbor., the same shapes as their deep-copy and duplicate-ID refusals. The JavaScript check is == null, so an undefined slot is refused as well, and the browser suite covers both.

In an interview, the JavaScript answer below adds copies.get(next) to a copy's neighbors before next has a copy of its own, so the copy holds undefined. Run against Sandbox Topology Replica in the same local server, with only web/compiler-explorer.js and web/runners.js switched between main at fe04f8f and this change, three of the five cases fail either way; before, each reports an exception raised inside the checker, and after, each names the fault.

var replicateTopology = function(node) {
    if (!node) return null;
    const copies = new Map();
    const queue = [node];
    copies.set(node, new _Node(node.val));
    while (queue.length) {
        const cur = queue.shift();
        for (const next of cur.neighbors) {
            copies.get(cur).neighbors.push(copies.get(next));
            if (!copies.has(next)) {
                copies.set(next, new _Node(next.val));
                queue.push(next);
            }
        }
    }
    return copies.get(node);
};

Before, main at fe04f8f:
1794886491307_image

After, this change:
1794886390551_image

With every neighbor checked before it is queued, nothing null can reach the queue: its first entry is the root, and each serializer already returns early when the root is null. The skip at the top of the queue loop in Java, JavaScript, Python and C++ therefore can no longer run and is removed, so no loop suggests that a null neighbor is passed over quietly.

tests/browser/graph-clone-identity.test.js had the null-neighbor scenario for C only, and only for the root's own list. It now runs for C, C++ and Java in the toolchain suite and for JavaScript and Python in the browser suite, once with a neighbor of the root nulled and once with a neighbor of b, which is reached from the root. The compiled-language assertion uses endsWith, as the duplicate-ID one does, because Java prefixes the exception class. Without the fix, the JavaScript, Python and Java null-neighbor cases fail and the C and C++ ones pass; with it, all pass. A guard narrowed to the root's list fails the non-root cases.

An answer that breaks two rules on one node is not aligned by this change. With a copied node whose neighbors are [input node, null], C walks each edge in order and reports Returned graph must be a deep copy., while C++, Java, JavaScript and Python refuse the null while queueing every neighbor and report the NULL neighbor. C and C++ already differed this way on main; this change brings Java, JavaScript and Python to the C++ order. A mixed scenario in the same test file pins both outcomes, so a later change to either order has to update it on purpose.

A neighbors list that is itself null is a separate gap, filed as #736.

Close #725

cubic-dev-ai[bot]

This comment was marked as resolved.

@Urdan0117
Urdan0117 force-pushed the fix/graph-null-neighbor branch from 72d685b to 1d1ac5b Compare November 16, 2026 06:54
alanhc
alanhc previously approved these changes Nov 16, 2026

@alanhc alanhc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Matches what graphToJson already does in C++: the null is refused where neighbors are queued, with the same message, so every node in byValue has had its neighbors checked before the rows read next.val. The scenario now covers all five languages plus the JavaScript undefined slot.

One nit, not blocking: with nulls refused before they reach the queue, if (cur == null) continue; in the Java loop (and if (!current) continue / if current is None in the JS and Python ones) can no longer fire. C++ keeps the same line, so leaving it for consistency is fine.

ColtenOuO
ColtenOuO previously approved these changes Nov 16, 2026

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

jserv

This comment was marked as resolved.

@Urdan0117
Urdan0117 dismissed stale reviews from ColtenOuO and alanhc via 8ef77fa November 16, 2026 07:41
@Urdan0117
Urdan0117 force-pushed the fix/graph-null-neighbor branch from 1d1ac5b to 8ef77fa Compare November 16, 2026 07:41

@Wu-1127 Wu-1127 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.

Checked out the head (1d1ac5b) on macOS and ran:

$ node --test tests/browser/graph-clone-identity.test.js
ℹ tests 36
ℹ pass 17
ℹ fail 0
ℹ skipped 19

The C and C++ cases pass. The 19 skipped are the Java cases (no JDK on
this machine) and the JavaScript and Python cases (no Chromium), so I
checked the Python serializer directly instead: with a.neighbors = None,
_graph_to_adjacency raises "TypeError: 'NoneType' object is not
iterable".

The element-level fix looks right to me, and alanhc's note on the dead
continue guards already covers that part. Two things inline: a null
neighbors list still fails inside the Java and Python checkers, and the
new tests only null a neighbor of the root.

Comment thread web/compiler-explorer.js Resolved
jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

@jserv

jserv commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Add screenshots to the pull request description to illustrate your work, as @ColtenOuO suggests. Example: #349

@Urdan0117
Urdan0117 force-pushed the fix/graph-null-neighbor branch from afe4667 to 1d5ddc2 Compare November 16, 2026 18:01
jserv

This comment was marked as resolved.

A Clone Graph answer whose copy holds a null neighbor got "Returned
graph has a NULL neighbor." in C and C++, but an exception from inside
the checker elsewhere: a NullPointerException from ArrayDeque in Java,
and a read of neighbor.val on null in JavaScript and Python. Refuse the
null where each serializer queues neighbors, with the message C and C++
already use. Nothing null can then reach the queue, so drop the skip at
the top of each queue loop, C++ included, rather than leave it implying
that a null neighbor is passed over quietly. The null-neighbor clone
test covered only C and only the root's list; it now runs for every
language and also nulls a neighbor of a node reached from the root.
A copy that also points at an input node still gets the deep-copy
refusal in C, which checks each edge in order, and the null refusal in
the other four, which check every neighbor while queueing; a test pins
both instead of implying one message for every mixed violation.

Close sysprog21#725

Change-Id: I79634808b1e2e839ffdd87db19c54399771bfc11
@Urdan0117
Urdan0117 force-pushed the fix/graph-null-neighbor branch from 1d5ddc2 to e86f0ec Compare November 17, 2026 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants