aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-09-19 14:36:41 +0300
committerVeikka Tuominen <git@vexu.eu>2022-09-20 00:50:13 +0300
commit541b3e3a31946475f29d21e7a742bf80c5952791 (patch)
tree4516d103301ab7913786679f3f2e988bf7029eb5 /lib/std
parentfb91483e48fe6cfa21edc613f266e27bd6bf9dbf (diff)
downloadzig-541b3e3a31946475f29d21e7a742bf80c5952791.tar.gz
zig-541b3e3a31946475f29d21e7a742bf80c5952791.zip
Sema: check pointer qualifiers before implicit cast
Closes #12881
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/json.zig6
-rw-r--r--lib/std/mem.zig23
-rw-r--r--lib/std/meta.zig4
-rw-r--r--lib/std/os/linux.zig2
-rw-r--r--lib/std/zig/c_translation.zig2
5 files changed, 19 insertions, 18 deletions
diff --git a/lib/std/json.zig b/lib/std/json.zig
index 548a3db94e..87fe1c9dea 100644
--- a/lib/std/json.zig
+++ b/lib/std/json.zig
@@ -1560,7 +1560,7 @@ fn parseInternal(
}
}
if (field.is_comptime) {
- if (!try parsesTo(field.field_type, @ptrCast(*const field.field_type, field.default_value.?).*, tokens, child_options)) {
+ if (!try parsesTo(field.field_type, @ptrCast(*align(1) const field.field_type, field.default_value.?).*, tokens, child_options)) {
return error.UnexpectedValue;
}
} else {
@@ -1587,7 +1587,7 @@ fn parseInternal(
if (!fields_seen[i]) {
if (field.default_value) |default_ptr| {
if (!field.is_comptime) {
- const default = @ptrCast(*const field.field_type, default_ptr).*;
+ const default = @ptrCast(*align(1) const field.field_type, default_ptr).*;
@field(r, field.name) = default;
}
} else {
@@ -1667,7 +1667,7 @@ fn parseInternal(
}
if (ptrInfo.sentinel) |some| {
- const sentinel_value = @ptrCast(*const ptrInfo.child, some).*;
+ const sentinel_value = @ptrCast(*align(1) const ptrInfo.child, some).*;
try arraylist.append(sentinel_value);
const output = arraylist.toOwnedSlice();
return output[0 .. output.len - 1 :sentinel_value];
diff --git a/lib/std/mem.zig b/lib/std/mem.zig
index bf841bff90..4000030fc0 100644
--- a/lib/std/mem.zig
+++ b/lib/std/mem.zig
@@ -297,7 +297,7 @@ pub fn zeroes(comptime T: type) T {
},
.Array => |info| {
if (info.sentinel) |sentinel_ptr| {
- const sentinel = @ptrCast(*const info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const info.child, sentinel_ptr).*;
return [_:sentinel]info.child{zeroes(info.child)} ** info.len;
}
return [_]info.child{zeroes(info.child)} ** info.len;
@@ -443,7 +443,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
inline for (struct_info.fields) |field| {
if (field.default_value) |default_value_ptr| {
- const default_value = @ptrCast(*const field.field_type, default_value_ptr).*;
+ const default_value = @ptrCast(*align(1) const field.field_type, default_value_ptr).*;
@field(value, field.name) = default_value;
}
}
@@ -687,7 +687,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
const l = len(ptr);
const ptr_info = @typeInfo(Result).Pointer;
if (ptr_info.sentinel) |s_ptr| {
- const s = @ptrCast(*const ptr_info.child, s_ptr).*;
+ const s = @ptrCast(*align(1) const ptr_info.child, s_ptr).*;
return ptr[0..l :s];
} else {
return ptr[0..l];
@@ -719,7 +719,7 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
// to find the value searched for, which is only the case if it matches
// the sentinel of the type passed.
if (array_info.sentinel) |sentinel_ptr| {
- const sentinel = @ptrCast(*const array_info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const array_info.child, sentinel_ptr).*;
if (end == sentinel) {
new_ptr_info.sentinel = &end;
} else {
@@ -734,7 +734,7 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
// to find the value searched for, which is only the case if it matches
// the sentinel of the type passed.
if (ptr_info.sentinel) |sentinel_ptr| {
- const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const ptr_info.child, sentinel_ptr).*;
if (end == sentinel) {
new_ptr_info.sentinel = &end;
} else {
@@ -772,7 +772,7 @@ pub fn sliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) SliceTo(@Typ
const length = lenSliceTo(ptr, end);
const ptr_info = @typeInfo(Result).Pointer;
if (ptr_info.sentinel) |s_ptr| {
- const s = @ptrCast(*const ptr_info.child, s_ptr).*;
+ const s = @ptrCast(*align(1) const ptr_info.child, s_ptr).*;
return ptr[0..length :s];
} else {
return ptr[0..length];
@@ -825,7 +825,7 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
.One => switch (@typeInfo(ptr_info.child)) {
.Array => |array_info| {
if (array_info.sentinel) |sentinel_ptr| {
- const sentinel = @ptrCast(*const array_info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const array_info.child, sentinel_ptr).*;
if (sentinel == end) {
return indexOfSentinel(array_info.child, end, ptr);
}
@@ -835,7 +835,7 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
else => {},
},
.Many => if (ptr_info.sentinel) |sentinel_ptr| {
- const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const ptr_info.child, sentinel_ptr).*;
// We may be looking for something other than the sentinel,
// but iterating past the sentinel would be a bug so we need
// to check for both.
@@ -849,7 +849,7 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
},
.Slice => {
if (ptr_info.sentinel) |sentinel_ptr| {
- const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const ptr_info.child, sentinel_ptr).*;
if (sentinel == end) {
return indexOfSentinel(ptr_info.child, sentinel, ptr);
}
@@ -911,7 +911,7 @@ pub fn len(value: anytype) usize {
.Many => {
const sentinel_ptr = info.sentinel orelse
@compileError("length of pointer with no sentinel");
- const sentinel = @ptrCast(*const info.child, sentinel_ptr).*;
+ const sentinel = @ptrCast(*align(1) const info.child, sentinel_ptr).*;
return indexOfSentinel(info.child, sentinel, value);
},
.C => {
@@ -2882,7 +2882,8 @@ fn AsBytesReturnType(comptime P: type) type {
/// Given a pointer to a single item, returns a slice of the underlying bytes, preserving pointer attributes.
pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
const P = @TypeOf(ptr);
- return @ptrCast(AsBytesReturnType(P), ptr);
+ const T = AsBytesReturnType(P);
+ return @ptrCast(T, @alignCast(meta.alignment(T), ptr));
}
test "asBytes" {
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 7e64eff49f..993d88eb04 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -204,12 +204,12 @@ pub fn sentinel(comptime T: type) ?Elem(T) {
switch (info.size) {
.Many, .Slice => {
const sentinel_ptr = info.sentinel orelse return null;
- return @ptrCast(*const info.child, sentinel_ptr).*;
+ return @ptrCast(*align(1) const info.child, sentinel_ptr).*;
},
.One => switch (@typeInfo(info.child)) {
.Array => |array_info| {
const sentinel_ptr = array_info.sentinel orelse return null;
- return @ptrCast(*const array_info.child, sentinel_ptr).*;
+ return @ptrCast(*align(1) const array_info.child, sentinel_ptr).*;
},
else => {},
},
diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig
index c4dafcd3b8..892cb1e8d3 100644
--- a/lib/std/os/linux.zig
+++ b/lib/std/os/linux.zig
@@ -888,7 +888,7 @@ else
const vdso_clock_gettime_ty = if (builtin.zig_backend == .stage1)
fn (i32, *timespec) callconv(.C) usize
else
- *const fn (i32, *timespec) callconv(.C) usize;
+ *align(1) const fn (i32, *timespec) callconv(.C) usize;
pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
if (@hasDecl(VDSO, "CGT_SYM")) {
diff --git a/lib/std/zig/c_translation.zig b/lib/std/zig/c_translation.zig
index 6847a92eae..9fd89ef8fb 100644
--- a/lib/std/zig/c_translation.zig
+++ b/lib/std/zig/c_translation.zig
@@ -193,7 +193,7 @@ pub fn sizeof(target: anytype) usize {
const array_info = @typeInfo(ptr.child).Array;
if ((array_info.child == u8 or array_info.child == u16) and
array_info.sentinel != null and
- @ptrCast(*const array_info.child, array_info.sentinel.?).* == 0)
+ @ptrCast(*align(1) const array_info.child, array_info.sentinel.?).* == 0)
{
// length of the string plus one for the null terminator.
return (array_info.len + 1) * @sizeOf(array_info.child);