Skip to content

std.mem.eql changes values memory #19564

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

Closed
Tiltimus opened this issue Apr 6, 2024 · 6 comments
Closed

std.mem.eql changes values memory #19564

Tiltimus opened this issue Apr 6, 2024 · 6 comments
Labels
question No questions on the issue tracker, please.

Comments

@Tiltimus
Copy link

Tiltimus commented Apr 6, 2024

Zig Version

0.12.0-dev.3029+723d13f83

Steps to Reproduce and Observed Behavior

I was trying to write a basic print for a date. When I ran this the first print will be:

BEFORE a: { 50, 48, 50, 52, 45, 48, 52, 45, 48, 54 } b: { 50, 48, 50, 52, 45, 48, 52, 45, 48, 54 }

and the second

AFTER a: { 8, 192, 52, 0, 0, 0, 0, 0, 8, 192 } b: { 50, 48, 50, 52, 45, 48, 52, 45, 48, 54 } equal: false

Im pretty new to zig so there is a high chance I am doing something wrong. Tried to hunt for where it could possibly be changing the value and it seems to happen before eqlBytes(sliceAsBytes(a), sliceAsBytes(b). I couldn't quite work out where though.

const std = @import("std");

pub fn main() !void {
    const actual = try print(2024, 4, 6);
    const expected = "2024-04-06";

    std.debug.print("BEFORE a: {any} b: {any}\n", .{ actual, expected });

    const areEqual = std.mem.eql(u8, actual, expected);

    std.debug.print("AFTER a: {any} b: {any} equal: {}\n", .{ actual, expected, areEqual });
}

pub fn print(year: u14, month: u4, day: u5) ![]u8 {
    var buf: [10]u8 = undefined;

    return try std.fmt.bufPrint(
        &buf,
        "{d:0>4}-{d:0>2}-{d:0>2}",
        .{
            year,
            month,
            day,
        },
    );
}

Expected Behavior

I would expect the memory from the first value I passed in not to change.

@Tiltimus Tiltimus added the bug Observed behavior contradicts documented or intended behavior label Apr 6, 2024
@Rexicon226
Copy link
Contributor

Rexicon226 commented Apr 6, 2024

The issue here is that you're creating a stack variable buf and then returning a pointer to it which will make reading from it invalid. Either allocate in print, or create the buf in a higher scope, like in main.

@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2024
@Vexu Vexu added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Apr 6, 2024
@Tiltimus
Copy link
Author

Tiltimus commented Apr 6, 2024

Oh okay me being dumb thanks for the speedy response ! :)

@nektro
Copy link
Contributor

nektro commented Apr 7, 2024

not knowing something doesnt make you dumb :)

@bronze1man
Copy link

The issue here is that you're creating a stack variable buf and then returning a pointer to it which will make reading from it invalid.

Is it very hard to implement to print an error in compiler when it seeing code like this? Or zig deliberately doesn't do this?

@nektro
Copy link
Contributor

nektro commented Apr 7, 2024

its planned, just hasn't been implemented yet #5725

@silversquirl
Copy link
Contributor

That proposal isn't accepted, so saying "it's planned" is a bit misleading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

6 participants