Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite recursion for Debug::fmt #609

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 13 additions & 1 deletion boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub static PROTOTYPE: &str = "prototype";
// pub static INSTANCE_PROTOTYPE: &str = "__proto__";

/// The internal representation of an JavaScript object.
#[derive(Debug, Trace, Finalize, Clone)]
#[derive(Trace, Finalize, Clone)]
pub struct Object {
/// The type of the object.
pub data: ObjectData,
Expand All @@ -64,6 +64,18 @@ pub struct Object {
extensible: bool,
}

impl Debug for Object {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Object")
.field("data", &self.data)
.field("properties", &self.properties)
.field("symbol_properties", &self.symbol_properties)
Comment on lines +71 to +72
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't any of these overflow for example:

let x = {};
x.itSelf = x;

the itSelf property is contained in properties and this would cause a stack overflow, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prototype always overflows, I thought it valuable to get a fix in for that (because I suspect it is breaking the test262 PR) and then figure out how to approach this in a general way after some discussion.

.field("state", &self.state)
.field("extensible", &self.extensible)
.finish()
}
}

/// Defines the different types of objects.
#[derive(Debug, Trace, Finalize, Clone)]
pub enum ObjectData {
Expand Down
12 changes: 12 additions & 0 deletions boa/src/builtins/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ fn display_negative_zero_object() {
assert_eq!(value.to_string(), "Number { -0 }")
}

#[test]
fn debug_object() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let value = forward_val(&mut engine, "new Array([new Date()])").unwrap();

// We don't care about the contents of the debug display (it is *debug* after all).
// In the commit that this test was added, this would cause a stack overflow, so
// executing Debug::fmt is the assertion.
let _ = format!("{:?}", value);
}

#[test]
#[ignore] // TODO: Once objects are printed in a simpler way this test can be simplified and used
fn display_object() {
Expand Down