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

Debug and ReleaseSafe different output #16467

Closed
bmacho opened this issue Jul 21, 2023 · 2 comments
Closed

Debug and ReleaseSafe different output #16467

bmacho opened this issue Jul 21, 2023 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@bmacho
Copy link

bmacho commented Jul 21, 2023

Zig Version

0.10.1

Steps to Reproduce and Observed Behavior

The following code

const std = @import("std");
const print = std.debug.print;

fn foo() fn() *u32 {
	
	const T = struct {

        fn bar() *u32 {
            var x: u32 = 123;
            return &x;
        }

	};
	
	return T.bar;
}

pub fn main() void {

    print("Result: {}", .{foo()().*});

}

gives different outputs with different build flags. It prints Result: 123 for

zig build-exe example.zig & example.exe

and Result: 00 for

zig build-exe example.zig -O ReleaseSafe & example.exe

It works for me locally also on godbolt:

Expected Behavior

I expected to have the same output, or the program to panic at runtime, or to not to compile at all.

@bmacho bmacho added the bug Observed behavior contradicts documented or intended behavior label Jul 21, 2023
@bmacho
Copy link
Author

bmacho commented Jul 21, 2023

I have no idea about the expected behaviour, but this smells bad.

The code is from the ziggit.dev thread https://ziggit.dev/t/how-to-declare-local-functions-in-functions/1234/

@bmacho bmacho changed the title Difference between -O ReleaseSafe and -Doptimize=ReleaseSafe Debug and ReleaseSafe different output Jul 21, 2023
@ifreund
Copy link
Member

ifreund commented Jul 21, 2023

Dereferencing a pointer to the stack variable x after bar() has returned is illegal behavior.

See #2646 and #2301

@ifreund ifreund closed this as completed Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants