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

std.HashMap(?) incorrect behavior when overwriting value for key #879

Closed
tgschultz opened this Issue Mar 31, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@tgschultz
Contributor

tgschultz commented Mar 31, 2018

I'm not 100% this is HashMap, it might be BufMap, which is where the problem first surfaced for me, or a codegen issue. The behavior is different depending on if I run this test in Windows or Linux, if I have the BufMap section commented out or not, and if I'm running it as a test or an exe. I ran a test or two at one point using a fixed buffer allocator instead, but the results didn't seem to be different.

const mem = @import("std").mem;
const warn = @import("std").debug.warn;
const BufMap = @import("std").BufMap;
const HashMap = @import("std").HashMap;


//////TESTS///////

test "BufMap/HashMap bug"
//pub fn main() !void
{
    const DirectAllocator = @import("std").heap.DirectAllocator;
    var alloc = DirectAllocator.init().allocator;
    
    //comment these out and the hash map test will still fail on Windows
    var b = BufMap.init(&alloc);
    try b.set("test", "");
    warn("{}\n", b.count());
    warn("{}\n", b.get("test"));
    try b.set("test", "abcdefg");
    warn("{}\n", b.count());
    warn("{}\n", b.get("test"));
    
    const StringHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);
    
    var h = StringHashMap.init(&alloc);
    _ = try h.put("test", "");
    warn("{}\n", h.size);
    var entry = h.get("test") ?? unreachable;
    warn("{}\n", entry.value);
    _ = try h.put("test", "abcdefg");
    warn("{}\n", h.size);
    entry = h.get("test") ?? unreachable;
    warn("{}\n", entry.value);
}

summary of results:

  Test w/BM Exe w/BM Test Exe
Windows corrupt corrupt assert expected
Linux assert assert expected expected

================================

Windows output, BufMap section enabled, test:

Test 1/1 bug...1

1
P�▒,��
1

1
abcdefg
OK

exe is identical save for the lines added by test

Windows output, BufMap section commented out, test:

Test 1/1 BufMap/HashMap bug...
Tests failed. Use the following command to reproduce the failure:
.\zig-cache\test.exe

Windows output, BufMap section disabled, exe:

1

1
abcdefg

Linux output, BufMap section enabled, test:

Test 1/1 bug...1

1
reached unreachable code
/home/tgschultz/zig/lib/zig/std/os/index.zig:246:47: 0x207fe4 in ??? (test)
                posix.EINVAL, posix.EFAULT => unreachable,
                                              ^
/home/tgschultz/zig/lib/zig/std/os/file.zig:304:30: 0x207e0f in ??? (test)
            try os.posixWrite(self.handle, bytes);
                             ^
/home/tgschultz/zig/lib/zig/std/io.zig:92:31: 0x20706c in ??? (test)
        return self.file.write(bytes);
                              ^
/home/tgschultz/zig/lib/zig/std/fmt/index.zig:236:26: 0x207195 in ??? (test)
            return output(context, casted_value);
                         ^
/home/tgschultz/zig/lib/zig/std/fmt/index.zig:211:35: 0x207bfc in ??? (test)
                return formatValue(payload, context, Errors, output);
                                  ^
/home/tgschultz/zig/lib/zig/std/fmt/index.zig:61:36: 0x207b1e in ??? (test)
                    try formatValue(args[next_arg], context, Errors, output);
                                   ^
/home/tgschultz/zig/lib/zig/std/io.zig:236:34: 0x206e3d in ??? (test)
            return std.fmt.format(self, Error, self.writeFn, format, args);
                                 ^
/home/tgschultz/zig/lib/zig/std/debug/index.zig:22:17: 0x2043cf in ??? (test)
    stderr.print(fmt, args) catch return;
                ^
/home/tgschultz/projects/ini/test.zig:19:9: 0x2038a9 in ??? (test)
    warn("{}\n", b.get("test"));
        ^
/home/tgschultz/zig/lib/zig/std/special/test_runner.zig:11:25: 0x220d88 in ??? (test)
        try test_fn.func();
                        ^
/home/tgschultz/zig/lib/zig/std/special/bootstrap.zig:85:22: 0x220b8b in ??? (test)
            root.main() catch |err| {
                     ^
/home/tgschultz/zig/lib/zig/std/special/bootstrap.zig:62:20: 0x220aeb in ??? (test)
    return callMain();
                   ^
/home/tgschultz/zig/lib/zig/std/special/bootstrap.zig:52:39: 0x22097c in ??? (test)
    std.os.posix.exit(callMainWithArgs(argc, argv, envp));
                                      ^
/home/tgschultz/zig/lib/zig/std/special/bootstrap.zig:39:5: 0x220900 in ??? (test)
    @noInlineCall(posixCallMainAndExit);
    ^

Tests failed. Use the following command to reproduce the failure:
./zig-cache/test

exe is identical save for the lines added by test

Linux output, BufMap section commented out, test:

Test 1/1 bug...1

1
abcdefg
OK

exe is identical save for the lines added by test

@andrewrk andrewrk added the bug label Mar 31, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Mar 31, 2018

bnoordhuis added a commit to bnoordhuis/zig that referenced this issue Apr 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment