Skip to content

Slice accessors alias the graph's internal storage #532

Closed

Description

@David-Noble-at-work

Three accessors return the graph's own slice, so a caller outside pkg/op can rewrite which pointers the
graph records:

func (g *Graph) Edges() []Edge                 { return g.root.edges }        // graph.go:546
func (s *Subgraph) Edges() []Edge              { return s.edges }             // subgraph.go:128
func (s *Subgraph) Children() []ExecutableUnit { return s.executableUnits }   // subgraph.go:117

g.Edges()[0] = otherEdge mutates the graph. Root().Children()[2] = otherUnit re-parents a subtree.
Both leave the construction checksum describing contents that no longer exist.

Subgraph.Edges documents the defect instead of fixing it: "The returned slice aliases the underlying
storage; callers must not mutate it directly."

Fix: slices.Clone on return. One allocation per call, and external mutation becomes impossible rather
than discouraged — the pointers inside stay identical, which is exactly the sharing the rule permits.

Exit: these three return copies; their "must not mutate" comments are deleted because they are no
longer true; a test writes through each returned slice and asserts the graph is unchanged.

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions