Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions plnested_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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))
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
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 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-- {
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)
}
}
19 changes: 18 additions & 1 deletion plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,39 @@ func plData(v any) (any, error) {
return nil, typeError(v, "plot data (an Array of numbers or an NDArray)")
}

// 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.
//
// 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
}
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
Expand Down
Loading