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

Failing test produces slightly garbled output #2484

Closed
kristoff-it opened this issue May 13, 2019 · 3 comments
Closed

Failing test produces slightly garbled output #2484

kristoff-it opened this issue May 13, 2019 · 3 comments

Comments

@kristoff-it
Copy link
Member

kristoff-it commented May 13, 2019

How to reproduce (from zig/master, on macOS):

Edit line 213 of https://github.com/kristoff-it/zig-cuckoofilter/blob/b5447d98282c780b396897ea50ca1ddafab901bb/cuckoofilter.zig from

cf.insert(2, 1) catch unreachable;

to

cf.insert(0, 1) catch unreachable;

This will cause the test to fail and show the following output:

1/5 test "Hx == (Hy XOR hash(fp))"...OK
2/5 test "is not completely broken"...OK
3/5 test "generics are not completely broken"...OK
4/5 test "too full when adding too many copies"...�`6,�Rʶ��t5i�p error: TooFull
...

The garbling happens also in 0.4.0.

@LemonBoy
Copy link
Contributor

Oh, this is quite nasty. The culprit is this line:

var rand = std.rand.DefaultPrng.init(42).random;

Here you have:

  • std.rand.DefaultPrng.init(42) that is a Xoroshiro128
  • std.rand.DefaultPrng.init(42).random that is a Random

Now, rand is just the Random part of the Xoroshiro128 structure and that means that whenever you call the fillFn and it tries to access the s field you're actually corrupting random memory.

And it turns out that your code ends up corrupting the error message stored in global_array 🎉

So it turns out it's not much of a bug, it's rather a programming error.

On a side note, why don't we allocate the buffer & slice that are passed on to panic() on the stack? Global state always feels icky to me.

@andrewrk
Copy link
Member

This is #591 which is a recognized design flaw in the language and it's planned to be a compile error once that design flaw is resolved.

@kristoff-it
Copy link
Member Author

Understood & fixed in my code. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants