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

std.debug.dump_hex address on partial line incorrect #20048

Open
Khitiara opened this issue May 23, 2024 · 4 comments · May be fixed by #20052
Open

std.debug.dump_hex address on partial line incorrect #20048

Khitiara opened this issue May 23, 2024 · 4 comments · May be fixed by #20052
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@Khitiara
Copy link

Zig Version

0.13.0-dev.245+5fe9f88b1

Steps to Reproduce and Observed Behavior

replication source and zig run output:
https://gist.github.com/Khitiara/ce35b7ba82304a8d540cda822512454e

Expected Behavior

expected output also provided along with a possible fix in the above gist, the important line being on line 18:

-        const address = (@intFromPtr(bytes.ptr) + 0x10 * (chunks.index orelse 0) / 16) - 0x10;
+        const address = (@intFromPtr(bytes.ptr) + 0x10 * (try std.math.divCeil(usize, chunks.index orelse bytes.len, 16))) - 0x10;
@Khitiara Khitiara added the bug Observed behavior contradicts documented or intended behavior label May 23, 2024
@Arnau478
Copy link
Contributor

About the potential fix: I think the divCeil call should have a catch unreachable instead of a try. This is because divCeil can only fail when the denominator is zero (obviously) or between 1 and -1 (it could overflow). 16 cannot cause an error.

Arnau478 added a commit to Arnau478/zig that referenced this issue May 23, 2024
@Arnau478 Arnau478 linked a pull request May 23, 2024 that will close this issue
@squeek502
Copy link
Collaborator

Just want to note that std.testing.expectEqualSlices doesn't exhibit this bug (dump_hex and expectEqualSlices don't share implementations):

const std = @import("std");

test {
    const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 };
    try std.testing.expectEqualSlices(u8, bytes, "");
}
Test [1/1] dump_hex.test_0... slices differ. first difference occurs at index 0 (0x0)

============ expected this output: =============  len: 19 (0x13)

00 11 22 33 44 55 66 77  88 99 AA BB CC DD EE FF  .."3DUfw........
01 12 13                                          ...

============= instead found this: ==============  len: 0 (0x0)



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

@Khitiara
Copy link
Author

@squeek502 note that the std.testing function does not report the address of each 16-byte line which is where the bug is in std.debug.dump_hex

@squeek502
Copy link
Collaborator

squeek502 commented May 23, 2024

Oh, I was looking at the wrong thing. I saw the missing ... on the bottom line of the ouput.txt and thought that was the problem. Ignore me then

@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label May 24, 2024
@Vexu Vexu added this to the 0.14.0 milestone May 24, 2024
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 standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants