Skip to content

Fix stack overflow when converting arrays with circular references to primitives#695

Merged
dop251 merged 2 commits intodop251:masterfrom
monkeyWie:master
Jan 6, 2026
Merged

Fix stack overflow when converting arrays with circular references to primitives#695
dop251 merged 2 commits intodop251:masterfrom
monkeyWie:master

Conversation

@monkeyWie
Copy link
Contributor

@monkeyWie monkeyWie commented Dec 26, 2025

Summary

Stack overflow occurs in goja when attempting to convert an object containing a circular reference to a primitive value (via toPrimitive, toString, toNumeric, etc.).

Minimal Reproduction

package main

import (
	"fmt"
	"github.com/dop251/goja"
)

func main() {
	vm := goja.New()
	
	code := `
		var T = [1, 2, 3];
		T[42] = T;       // Create circular reference
		var x = T % 2;   // Modulo operation triggers toPrimitive -> toString
	`
	
	_, err := vm.RunString(code)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}
}

Environment

  • Go version: go1.25.4 darwin/arm64
  • OS: macOS

Copy link
Owner

@dop251 dop251 left a comment

Choose a reason for hiding this comment

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

Thanks for submitting thiis! I've added a few comments.

@monkeyWie
Copy link
Contributor Author

@dop251 has already submitted a modification, please review

@dop251 dop251 merged commit 651366f into dop251:master Jan 6, 2026
4 checks passed
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.

3 participants