Skip to content

Incorrect slice bounds detection in comptime @ptrCast #1292

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
tgschultz opened this issue Jul 26, 2018 · 2 comments
Closed

Incorrect slice bounds detection in comptime @ptrCast #1292

tgschultz opened this issue Jul 26, 2018 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@tgschultz
Copy link
Contributor

const std = @import("std");
const builtin = @import("builtin");
const debug = std.debug;

pub fn bytesOf(ptr: var) []u8 {
    const PtrType = @typeOf(ptr);
    const PtrTypeId = @typeId(PtrType);
    debug.assert(PtrTypeId == builtin.TypeId.Pointer);
    
    var aligned = @alignCast(@alignOf(*u8), ptr);
    return @ptrCast([*]u8, aligned)[0..@sizeOf(PtrType.Child)];
}

test "comptime *T to []u8" {
    comptime {
        var x = usize(200);
        var s = bytesOf(&x);
    }
}
error: out of bounds slice
    return @ptrCast([*]u8, aligned)[0..@sizeOf(PtrType.Child)];
                                   ^

Compiler seems to think that only [0..1] is valid. Outside of comptime this works fine.

@andrewrk andrewrk added this to the 0.3.0 milestone Jul 26, 2018
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Jul 26, 2018
@andrewrk
Copy link
Member

Simplified test case:

const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;

comptime {
    var x: u32 = 0x1a2b3c4d;
    var s = @ptrCast([*]u8, &x)[0..@sizeOf(u32)];
    if (builtin.endian == builtin.Endian.Big) {
        assert(s[0] == 0x1a);
        assert(s[1] == 0x2b);
        assert(s[2] == 0x3c);
        assert(s[3] == 0x4d);
    } else {
        assert(s[0] == 0x4d);
        assert(s[1] == 0x3c);
        assert(s[2] == 0x2b);
        assert(s[3] == 0x1a);
    }
}

I started working on this, but I think it would be better for post-0.3.0, it's more an issue of "implement more compile-time pointer reinterpreting support" which is a tall order for 1 week before release.

@SamTebbs33
Copy link
Contributor

Duplicate of #1150

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 28, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
andrewrk added a commit that referenced this issue Dec 18, 2022
andrewrk added a commit that referenced this issue Dec 18, 2022
andrewrk added a commit that referenced this issue Dec 18, 2022
@andrewrk andrewrk removed this from the 0.12.0 milestone Dec 19, 2022
@andrewrk andrewrk added this to the 0.11.0 milestone Dec 19, 2022
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants