aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-04-21 18:03:33 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-04-25 11:23:40 -0700
commitedb5e493e6d9a4478b3d9c06aa590694757d8c03 (patch)
tree5d5ccca94129e3a0e177845dbb5a9b251271cd07 /lib/std
parenta5c910adb610ae530db99f10aa77aaed3e85e830 (diff)
downloadzig-edb5e493e6d9a4478b3d9c06aa590694757d8c03.tar.gz
zig-edb5e493e6d9a4478b3d9c06aa590694757d8c03.zip
update `@memcpy` to require equal src and dest lens
* Sema: upgrade operands to array pointers if possible when emitting AIR. * Implement safety checks for length mismatch and aliasing. * AIR: make ptrtoint support slice operands. Implement in LLVM backend. * C backend: implement new `@memset` semantics. `@memcpy` is not done yet.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/builtin.zig2
-rw-r--r--lib/std/crypto/aegis.zig4
-rw-r--r--lib/std/hash/murmur.zig11
-rw-r--r--lib/std/mem/Allocator.zig3
4 files changed, 11 insertions, 9 deletions
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig
index cfe22099cb..048f73482e 100644
--- a/lib/std/builtin.zig
+++ b/lib/std/builtin.zig
@@ -1002,6 +1002,8 @@ pub const panic_messages = struct {
pub const index_out_of_bounds = "index out of bounds";
pub const start_index_greater_than_end = "start index is larger than end index";
pub const for_len_mismatch = "for loop over objects with non-equal lengths";
+ pub const memcpy_len_mismatch = "@memcpy arguments have non-equal lengths";
+ pub const memcpy_alias = "@memcpy arguments alias";
};
pub noinline fn returnError(st: *StackTrace) void {
diff --git a/lib/std/crypto/aegis.zig b/lib/std/crypto/aegis.zig
index 8cc5a8320e..d7305b444a 100644
--- a/lib/std/crypto/aegis.zig
+++ b/lib/std/crypto/aegis.zig
@@ -209,7 +209,7 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type {
acc |= (computed_tag[j] ^ tag[j]);
}
if (acc != 0) {
- @memset(m.ptr, undefined, m.len);
+ @memset(m, undefined);
return error.AuthenticationFailed;
}
}
@@ -390,7 +390,7 @@ fn Aegis256Generic(comptime tag_bits: u9) type {
acc |= (computed_tag[j] ^ tag[j]);
}
if (acc != 0) {
- @memset(m.ptr, undefined, m.len);
+ @memset(m, undefined);
return error.AuthenticationFailed;
}
}
diff --git a/lib/std/hash/murmur.zig b/lib/std/hash/murmur.zig
index 15fe1b5d7d..95f7c60efe 100644
--- a/lib/std/hash/murmur.zig
+++ b/lib/std/hash/murmur.zig
@@ -99,9 +99,8 @@ pub const Murmur2_64 = struct {
pub fn hashWithSeed(str: []const u8, seed: u64) u64 {
const m: u64 = 0xc6a4a7935bd1e995;
- const len = @as(u64, str.len);
- var h1: u64 = seed ^ (len *% m);
- for (@ptrCast([*]align(1) const u64, str.ptr)[0..@intCast(usize, len >> 3)]) |v| {
+ var h1: u64 = seed ^ (@as(u64, str.len) *% m);
+ for (@ptrCast([*]align(1) const u64, str.ptr)[0..str.len / 8]) |v| {
var k1: u64 = v;
if (native_endian == .Big)
k1 = @byteSwap(k1);
@@ -111,11 +110,11 @@ pub const Murmur2_64 = struct {
h1 ^= k1;
h1 *%= m;
}
- const rest = len & 7;
- const offset = len - rest;
+ const rest = str.len & 7;
+ const offset = str.len - rest;
if (rest > 0) {
var k1: u64 = 0;
- @memcpy(@ptrCast([*]u8, &k1)[0..@intCast(usize, rest)], @ptrCast([*]const u8, &str[@intCast(usize, offset)]));
+ @memcpy(@ptrCast([*]u8, &k1)[0..rest], str[offset..]);
if (native_endian == .Big)
k1 = @byteSwap(k1);
h1 ^= k1;
diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig
index ce09cd8baa..b402fab3fa 100644
--- a/lib/std/mem/Allocator.zig
+++ b/lib/std/mem/Allocator.zig
@@ -282,7 +282,8 @@ pub fn reallocAdvanced(
const new_mem = self.rawAlloc(byte_count, log2a(Slice.alignment), return_address) orelse
return error.OutOfMemory;
- @memcpy(new_mem[0..@min(byte_count, old_byte_slice.len)], old_byte_slice);
+ const copy_len = @min(byte_count, old_byte_slice.len);
+ @memcpy(new_mem[0..copy_len], old_byte_slice[0..copy_len]);
// TODO https://github.com/ziglang/zig/issues/4298
@memset(old_byte_slice, undefined);
self.rawFree(old_byte_slice, log2a(Slice.alignment), return_address);