From 8be95fbf40e1d3ec146e3331c708e1813931f263 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 29 Nov 2026 20:04:55 +0100 Subject: [PATCH 1/2] fix: bound plNested's depth and element count plNested took the shape from the first element at each level, so a self-containing Array (a = []; a << a) grew the shape list without end. A shared sub-Array repeated (a = [a, a]) was likewise walked at each place. The depth is now at most 64 levels and the walk at most 2^22 numbers, an ArgumentError past either. Co-Authored-By: Claude Sonnet 5.5 --- plnested_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ plot.go | 19 ++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 plnested_test.go diff --git a/plnested_test.go b/plnested_test.go new file mode 100644 index 0000000..06deab3 --- /dev/null +++ b/plnested_test.go @@ -0,0 +1,55 @@ +package ruby + +import ( + "runtime/debug" + "testing" +) + +// A self-containing Array has no end to its shape, and a shared one is walked +// at every place it appears: both are an ArgumentError, with memory bounded, +// never a loop that grows until the host dies. +func TestPlotNestedArraysAreBounded(t *testing.T) { + defer debug.SetMemoryLimit(debug.SetMemoryLimit(512 << 20)) + in := newInterp(t) + eval(t, in, `fig, ax = Plot.subplots +cyc = []; cyc << cyc +cyc2 = []; cyc2 << [cyc2, cyc2] +deep = 1; 70.times { deep = [deep] } +wide = 1; 30.times { wide = [wide, wide] }`) + evalErr(t, in, `ax.imshow(cyc)`, "ArgumentError", "nested more than 64 levels") + evalErr(t, in, `ax.imshow(cyc2)`, "ArgumentError", "nested more than 64 levels") + evalErr(t, in, `ax.plot([cyc])`, "ArgumentError", "nested more than 64 levels") + evalErr(t, in, `ax.imshow(deep)`, "ArgumentError", "nested more than 64 levels") + evalErr(t, in, `ax.imshow(wide)`, "ArgumentError", "more than 4194304 numbers") +} + +// The limits are exact: plMaxDepth levels and plMaxElems numbers convert, one +// more of either is refused. +func TestPlNestedLimitsAreExact(t *testing.T) { + nest := func(n int) []any { + a := []any{1.0} + for ; n > 1; n-- { + a = []any{a} + } + return a + } + if _, err := plNested(nest(plMaxDepth)); err != nil { + t.Errorf("%d levels: %v", plMaxDepth, err) + } + if _, err := plNested(nest(plMaxDepth + 1)); err == nil { + t.Errorf("%d levels converted", plMaxDepth+1) + } + row := func(n int) []any { + r := make([]any, n) + for i := range r { + r[i] = 1.0 + } + return r + } + if _, err := plNested([]any{row(plMaxElems)}); err != nil { + t.Errorf("%d numbers: %v", plMaxElems, err) + } + if _, err := plNested([]any{row(plMaxElems + 1)}); err == nil { + t.Errorf("%d numbers converted", plMaxElems+1) + } +} diff --git a/plot.go b/plot.go index a800998..effbbaf 100644 --- a/plot.go +++ b/plot.go @@ -299,11 +299,22 @@ func plData(v any) (any, error) { return nil, typeError(v, "plot data (an Array of numbers or an NDArray)") } +const ( + // plMaxDepth is far deeper than any plot data and far shallower than the stack. + plMaxDepth = 64 + // plMaxElems is far more than any plot has points (32 MiB of float64). + plMaxElems = 1 << 22 +) + // plNested converts nested Arrays: two levels to a [][]float64, more to an // NDArray of their shape. Every level must be rectangular. +// +// A self-containing Array (a = []; a << a) has no end to its shape, and a +// shared sub-Array (a = [a, a], repeated) is walked at each place: the depth +// and the number of values are bounded, an ArgumentError past either. func plNested(x []any) (any, error) { var shape []int - for e := any(x); ; { + for e := any(x); len(shape) <= plMaxDepth; { l, ok := e.([]any) if !ok || len(l) == 0 { break @@ -311,10 +322,16 @@ func plNested(x []any) (any, error) { shape = append(shape, len(l)) e = l[0] } + if len(shape) > plMaxDepth { + return nil, argError("plot data nested more than %d levels deep (a cycle?)", plMaxDepth) + } var flat []float64 var walk func(e any, depth int) error walk = func(e any, depth int) error { if depth == len(shape) { + if len(flat) >= plMaxElems { + return argError("plot data holds more than %d numbers", plMaxElems) + } f, err := toFloat(e) flat = append(flat, f) return err From 41df6af398bff50e076b09fadd856ece9d85c4f0 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 29 Nov 2026 20:57:03 +0100 Subject: [PATCH 2/2] fix: raise plNested's number cap to 2^25 so a 4096x4096 image converts Co-Authored-By: Claude Sonnet 5.5 --- plnested_test.go | 9 ++++++++- plot.go | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/plnested_test.go b/plnested_test.go index 06deab3..1385447 100644 --- a/plnested_test.go +++ b/plnested_test.go @@ -10,6 +10,8 @@ import ( // never a loop that grows until the host dies. func TestPlotNestedArraysAreBounded(t *testing.T) { defer debug.SetMemoryLimit(debug.SetMemoryLimit(512 << 20)) + defer func(n int) { plMaxElems = n }(plMaxElems) + plMaxElems = 1 << 16 // the walk of wide stops here, not after 2^25 numbers in := newInterp(t) eval(t, in, `fig, ax = Plot.subplots cyc = []; cyc << cyc @@ -20,12 +22,17 @@ wide = 1; 30.times { wide = [wide, wide] }`) evalErr(t, in, `ax.imshow(cyc2)`, "ArgumentError", "nested more than 64 levels") evalErr(t, in, `ax.plot([cyc])`, "ArgumentError", "nested more than 64 levels") evalErr(t, in, `ax.imshow(deep)`, "ArgumentError", "nested more than 64 levels") - evalErr(t, in, `ax.imshow(wide)`, "ArgumentError", "more than 4194304 numbers") + evalErr(t, in, `ax.imshow(wide)`, "ArgumentError", "more than 65536 numbers") } // The limits are exact: plMaxDepth levels and plMaxElems numbers convert, one // more of either is refused. func TestPlNestedLimitsAreExact(t *testing.T) { + if plMaxElems != 1<<25 { + t.Fatalf("plMaxElems = %d, want 2^25 (a 4096x4096 image must convert)", plMaxElems) + } + defer func(n int) { plMaxElems = n }(plMaxElems) + plMaxElems = 1 << 12 // the exact edge, without building 2^25 values nest := func(n int) []any { a := []any{1.0} for ; n > 1; n-- { diff --git a/plot.go b/plot.go index effbbaf..d3903f1 100644 --- a/plot.go +++ b/plot.go @@ -299,12 +299,12 @@ func plData(v any) (any, error) { return nil, typeError(v, "plot data (an Array of numbers or an NDArray)") } -const ( - // plMaxDepth is far deeper than any plot data and far shallower than the stack. - plMaxDepth = 64 - // plMaxElems is far more than any plot has points (32 MiB of float64). - plMaxElems = 1 << 22 -) +// plMaxDepth is far deeper than any plot data and far shallower than the stack. +const plMaxDepth = 64 + +// plMaxElems bounds the numbers one conversion produces (2^25, 256 MiB of +// float64, a 4096x8192 image). It is a variable so a test can lower it. +var plMaxElems = 1 << 25 // plNested converts nested Arrays: two levels to a [][]float64, more to an // NDArray of their shape. Every level must be rectangular.