You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe I've encountered a bug in the following Zig code. The function below returns a pointer to a captured field (self.bar) from a recursive function. In debug mode (when running zig build test), the code works fine. However, when using the -Doptimize flag, the returned pointer is incorrect.
The code compiles correctly if I return the field directly without specifying a const pointer and let the compiler handle it. Using |*b| also works fine.
Regards,
Jonas
Expected Behavior
Function returns the correct pointer or &b is disallowed.
The text was updated successfully, but these errors were encountered:
jonascloud
added
the
bug
Observed behavior contradicts documented or intended behavior
label
May 4, 2025
b is not an alias for bar's u32 component, it is a constant local variable equal to bar's u32 component. A pointer to a variable will not be valid outside of the variable's scope, so you are invoking undefined behavior by dereferencing the pointer returned by get.
Ah, yes. That's probably what's happening, thank you. b is a "copy" of the underlying memory and there are no guarantees, that this copy lives after the scope ends.
Referencing "introduce runtime safety for dangling stack pointers" and closing as duplicate.
Zig Version
0.14.0
Steps to Reproduce and Observed Behavior
Hey,
I believe I've encountered a bug in the following Zig code. The function below returns a pointer to a captured field (
self.bar
) from a recursive function. In debug mode (when runningzig build test
), the code works fine. However, when using the-Doptimize
flag, the returned pointer is incorrect.Here is the problematic code:
The code compiles correctly if I return the field directly without specifying a const pointer and let the compiler handle it. Using
|*b|
also works fine.Regards,
Jonas
Expected Behavior
Function returns the correct pointer or &b is disallowed.
The text was updated successfully, but these errors were encountered: