Zig: Variable length arrays in ntdll definitions

Created on 10 Aug 2020  路  5Comments  路  Source: ziglang/zig

@daurnimator suggested leaving out the variable length arrays (VLAs) from some of our NT definitions; for example, we currently have something like this:

pub const FILE_NAME_INFORMATION = extern struct {
    FileNameLength: ULONG,
    FileName: [1]WCHAR,
};

where FileName is effectively a VLA which cannot be known a priori. The idea here is to weed that out leaving only the static elements, so in the above example, leaving in only FileNameLength. FileName VLA can then be accessed by taking a pointer just behind this static structure.

breaking enhancement os-windows standard library

Most helpful comment

I think it would be reasonable to include a getter function in structs that use VLAs:

pub const FILE_NAME_INFORMATION = extern struct {
    FileNameLength: ULONG,

    pub fn getFileName(self: *@This()) []WCHAR {
        return @intToPtr([*]WCHAR, @ptrToInt(self) + @sizeOf(ULONG))[0..self.FileNameLength];
    }
};

All 5 comments

For the record: PR where this discussion originated #5993.

I think it would be reasonable to include a getter function in structs that use VLAs:

pub const FILE_NAME_INFORMATION = extern struct {
    FileNameLength: ULONG,

    pub fn getFileName(self: *@This()) []WCHAR {
        return @intToPtr([*]WCHAR, @ptrToInt(self) + @sizeOf(ULONG))[0..self.FileNameLength];
    }
};

I think it would be reasonable to include a getter function in structs that use VLAs:

pub const FILE_NAME_INFORMATION = extern struct {
    FileNameLength: ULONG,

    pub fn getFileName(self: *@This()) []WCHAR {
        return @intToPtr([*]WCHAR, @ptrToInt(self) + @sizeOf(ULONG))[0..self.FileNameLength];
    }
};

That's a cool idea, I like it!

I think it would be reasonable to include a getter function in structs that use VLAs:

Note that I've hit zig bugs doing this in the past.

might be fixed now though

I think it would be reasonable to include a getter function in structs that use VLAs:

Note that I've hit zig bugs doing this in the past.

might be fixed now though

Thanks for the heads up @daurnimator!

Was this page helpful?
0 / 5 - 0 ratings