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

Nasty stack overflow segfault bug when implementing the Default trait #6207

Closed
HugoPeters1024 opened this issue Oct 8, 2022 · 4 comments
Closed
Labels
A-Diagnostics Logging, crash handling, error reporting and performance analysis A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior S-Blocked This cannot move forward until something else changes S-Duplicate This issue or PR already exists S-Needs-Investigation This issue requires detective work to figure out what's going wrong

Comments

@HugoPeters1024
Copy link
Contributor

Bevy version

0.8.1

The issue

I stumbled upon a situation where when I spawned my player bundle the program would segfault with a stack overflow. With gdb I discovered that this occurred in the default function. The problem might be obvious to some:

#[derive(Bundle)]
pub struct PlayerBundle {
    player: Player,

    #[bundle]
    sprite: SpriteBundle,

    rigid_body: RigidBody,
    collider: Collider,
}

impl Default for PlayerBundle {
    fn default() -> Self {
        Self {
            sprite: SpriteBundle {
                sprite: Sprite {
                    custom_size: Some(Vec2::new(24.0, 24.0)),
                    ..default()
                },
                ..default()
            },
            ..default()
        }
    }
}

Namely, the default function is recursive and non terminating (I wrongly assumed that ..default() calls upon the Default trait of each respective field). This is quite problematic which is why in a non bevy project like this sandboxed example:

struct DefaultOne {
    v: u32,
}

impl Default for DefaultOne {
    fn default() -> Self {
        Self { v: 1 }
    }
}

struct DefaultFour {
    v: u32,
}

impl Default for DefaultFour {
    fn default() -> Self {
        Self { v: 5 }
    }

}
struct Pair {
    x: DefaultOne,
    y: DefaultFour,
}

impl Default for Pair {
    fn default() -> Self {
        Self {
            x: DefaultOne { v: 42 },
            ..Default::default()
        }
    }
}

rust analyzer produces a big fat warning about possible non termination. However, somehow when using the ..default() syntax made available in the bevy prelude, rust analyzer is blind to the issue and lets you write a nasty bug like this. It would be good to research if it is possible to make sure that in the bevy case this warning is still emitted.

@HugoPeters1024 HugoPeters1024 added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Oct 8, 2022
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events A-Diagnostics Logging, crash handling, error reporting and performance analysis S-Needs-Investigation This issue requires detective work to figure out what's going wrong and removed S-Needs-Triage This issue needs to be labelled labels Oct 8, 2022
@harudagondi
Copy link
Member

It seems that there is no diagnostics even with a custom default fn and the default_free_fn feature in rust nightly. I'll try filing an issue for this in rust-lang.

@DJMcNab
Copy link
Member

DJMcNab commented Oct 9, 2022

We can't hope to solve this until the default free function stabilises in some form, unless we remove our own free function.

@harudagondi
Copy link
Member

This is in fact, not a Bevy problem, but a Rust problem. See rust-lang/rust#57965

@DJMcNab DJMcNab added the S-Blocked This cannot move forward until something else changes label Oct 14, 2022
@nicopap
Copy link
Contributor

nicopap commented Jun 20, 2023

#8879 is a duplicate of this, but since it has more up-to-date information, I'm closing this one in favor of #8879

@nicopap nicopap closed this as not planned Won't fix, can't repro, duplicate, stale Jun 20, 2023
@nicopap nicopap added the S-Duplicate This issue or PR already exists label Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Diagnostics Logging, crash handling, error reporting and performance analysis A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior S-Blocked This cannot move forward until something else changes S-Duplicate This issue or PR already exists S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
Development

No branches or pull requests

5 participants