Skip to content

reject out-of-range bytes in graph6/sparse6 decoders - #8692

Merged
dschult merged 1 commit into
networkx:mainfrom
uwezkhan:graph6-range-check
Jun 10, 2026
Merged

dschult merged 1 commit into
networkx:mainfrom
uwezkhan:graph6-range-check

Conversation

@uwezkhan

@uwezkhan uwezkhan commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

from_graph6_bytes only rejects characters above the valid range (the c > 63 test), and from_sparse6_bytes does no range check at all. graph6 and sparse6 characters must satisfy 63 <= ord(c) < 127, so any byte below 63 maps to a negative value that slips through and a file containing one decodes to a different graph with no error, even though from_graph6_bytes documents a ValueError for characters outside that range.

Before, an out-of-range byte either produced a silently wrong graph or surfaced later as an unrelated bit-count NetworkXError. After, both decoders raise the documented ValueError where the bytes are converted, so read_graph6 and read_sparse6 inherit the same guarantee without each repeating the check. The tradeoff is that inputs which previously decoded by accident now fail fast, which matches the documented contract for these formats.

@rossbar rossbar 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.

This does indeed seem to be a restriction of the graph/sparse6 format, at least according to this (which is the closest I could come to finding a formal spec).

The change in exception type for the graph6 corner case isn't ideal, but it is in a sense a "bug fix" as newlines are outside the valid ASCII range for these formats. While it is technically an API change, I think it's a good one on the basis of correctness (and shouldn't be too disruptive downstream). If we were really concerned about API consistency we could instead do something like:

if any(c < 0 for c in data):
    raise NetworkXError
if any(c > 63 for c in data):
    raise ValueError

but I personally don't think that's worth it (ValueError is more "correct" anyways).

Thanks for pointing this out @uwezkhan !

@uwezkhan

Copy link
Copy Markdown
Contributor Author

Sounds good, I'll leave it as the single ValueError check. The only behavioral change is that header-newline corner case: before, it decoded far enough to surface a NetworkXError on a bit-count mismatch; after, it fails fast with the documented ValueError at the byte-range step. The split version keeps the old type for the sub-63 path but needs two passes and reports a less accurate exception, so I'd make the same call. ValueError also matches the docstring contract for both decoders.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good!
Thanks @uwezkhan !! and @rossbar for good review...

@dschult
dschult merged commit 51d7a18 into networkx:main Jun 10, 2026
41 checks passed
@dschult dschult added this to the 3.7 milestone Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants