From 2ed368fb92a96a9ec2f48459b56f6eace7b2bff4 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 29 Apr 2021 14:25:58 +0200 Subject: [PATCH 1/9] std/build: add -p alias for --prefix --- lib/std/special/build_runner.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 70aa3c8dc6..c6185ef093 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -82,9 +82,9 @@ pub fn main() !void { builder.verbose = true; } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { return usage(builder, false, stdout_stream); - } else if (mem.eql(u8, arg, "--prefix")) { + } else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--prefix")) { install_prefix = nextArg(args, &arg_idx) orelse { - warn("Expected argument after --prefix\n\n", .{}); + warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; } else if (mem.eql(u8, arg, "--search-prefix")) { @@ -188,7 +188,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void \\General Options: \\ -h, --help Print this help and exit \\ --verbose Print commands before executing them - \\ --prefix [path] Override default install prefix + \\ -p, --prefix [path] Override default install prefix \\ --search-prefix [path] Add a path to look for binaries, libraries, headers \\ --color [auto|off|on] Enable or disable colored error messages \\ From 687ef42f988191b6da78bfafbda0d2fe888697cc Mon Sep 17 00:00:00 2001 From: Frank Denis <124872+jedisct1@users.noreply.github.com> Date: Thu, 29 Apr 2021 16:27:50 +0200 Subject: [PATCH 2/9] x: comptime bool -> comptime x: bool (#8639) --- lib/std/crypto/25519/field.zig | 2 +- lib/std/crypto/poly1305.zig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/crypto/25519/field.zig b/lib/std/crypto/25519/field.zig index aae53e9081..3378191451 100644 --- a/lib/std/crypto/25519/field.zig +++ b/lib/std/crypto/25519/field.zig @@ -292,7 +292,7 @@ pub const Fe = struct { return _carry128(&r); } - fn _sq(a: Fe, double: comptime bool) callconv(.Inline) Fe { + fn _sq(a: Fe, comptime double: bool) callconv(.Inline) Fe { var ax: [5]u128 = undefined; var r: [5]u128 = undefined; comptime var i = 0; diff --git a/lib/std/crypto/poly1305.zig b/lib/std/crypto/poly1305.zig index 739c057178..375cf0a3cb 100644 --- a/lib/std/crypto/poly1305.zig +++ b/lib/std/crypto/poly1305.zig @@ -39,7 +39,7 @@ pub const Poly1305 = struct { }; } - fn blocks(st: *Poly1305, m: []const u8, last: comptime bool) void { + fn blocks(st: *Poly1305, m: []const u8, comptime last: bool) void { const hibit: u64 = if (last) 0 else 1 << 40; const r0 = st.r[0]; const r1 = st.r[1]; From 585479500e1941da25bcc4c55a4c3abc69fec031 Mon Sep 17 00:00:00 2001 From: Matthew Borkowski Date: Thu, 29 Apr 2021 10:13:10 -0400 Subject: [PATCH 3/9] check for overflow when reading code lengths for a block with dynamic Huffman codes --- lib/std/compress/deflate.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/std/compress/deflate.zig b/lib/std/compress/deflate.zig index e680dc9e6f..09e162933c 100644 --- a/lib/std/compress/deflate.zig +++ b/lib/std/compress/deflate.zig @@ -384,6 +384,8 @@ pub fn InflateStream(comptime ReaderType: type) type { const last_length = lengths[i - 1]; const repeat = 3 + (try self.readBits(2)); const last_index = i + repeat; + if (last_index > lengths.len) + return error.InvalidLength; while (i < last_index) : (i += 1) { lengths[i] = last_length; } @@ -655,3 +657,19 @@ pub fn InflateStream(comptime ReaderType: type) type { pub fn inflateStream(reader: anytype, window_slice: []u8) InflateStream(@TypeOf(reader)) { return InflateStream(@TypeOf(reader)).init(reader, window_slice); } + +test "lengths overflow" { + // malformed final dynamic block, tries to write 321 code lengths (MAXCODES is 316) + // f dy hlit hdist hclen 16 17 18 0 (18) x138 (18) x138 (18) x39 (16) x6 + // 1 10 11101 11101 0000 010 010 010 010 (11) 1111111 (11) 1111111 (11) 0011100 (01) 11 + const stream = [_]u8{ + 0b11101101, 0b00011101, 0b00100100, 0b11101001, 0b11111111, 0b11111111, 0b00111001, 0b00001110 + }; + + const reader = std.io.fixedBufferStream(&stream).reader(); + var window: [0x8000]u8 = undefined; + var inflate = inflateStream(reader, &window); + + var buf: [1]u8 = undefined; + std.testing.expectError(error.InvalidLength, inflate.read(&buf)); +} From 5079d11b213a02e9d9465f3371d0833021cfc636 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 29 Apr 2021 14:34:45 +0200 Subject: [PATCH 4/9] std/build: change default install prefix to zig-out Currently the default install prefix is $BUILD_ROOT/zig-cache, but mixing cache and artifacts makes little sense. Instead make $BUILD_ROOT/zig-out the default. --- .gitignore | 1 + lib/std/build.zig | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9fa6f71cc7..feda423c10 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # -andrewrk zig-cache/ +zig-out/ /release/ /debug/ /build/ diff --git a/lib/std/build.zig b/lib/std/build.zig index fce3a0bceb..1811073279 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -195,7 +195,8 @@ pub const Builder = struct { self.install_prefix = install_prefix orelse "/usr"; self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, self.install_prefix }) catch unreachable; } else { - self.install_prefix = install_prefix orelse self.cache_root; + self.install_prefix = install_prefix orelse + (fs.path.join(self.allocator, &[_][]const u8{ self.build_root, "zig-out" }) catch unreachable); self.install_path = self.install_prefix; } self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable; From f79b487337d7d11ae35fd4a15294c75e23c5f02b Mon Sep 17 00:00:00 2001 From: Yorhel Date: Fri, 30 Apr 2021 11:29:37 +0200 Subject: [PATCH 5/9] Handle EPERM and ELOOP in os.fstatat() --- lib/std/os.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index 7342db0d01..dfebda6104 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3419,7 +3419,7 @@ pub fn fstat(fd: fd_t) FStatError!Stat { } } -pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound }; +pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound, SymLinkLoop }; /// Similar to `fstat`, but returns stat of a resource pointed to by `pathname` /// which is relative to `dirfd` handle. @@ -3466,8 +3466,10 @@ pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!S EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, EACCES => return error.AccessDenied, + EPERM => return error.AccessDenied, EFAULT => unreachable, ENAMETOOLONG => return error.NameTooLong, + ELOOP => return error.SymLinkLoop, ENOENT => return error.FileNotFound, ENOTDIR => return error.FileNotFound, else => |err| return unexpectedErrno(err), From d3f55782b0a48a4472d7d535ac1215c8f5d43e31 Mon Sep 17 00:00:00 2001 From: Devin Bayer Date: Fri, 30 Apr 2021 21:31:01 +0200 Subject: [PATCH 6/9] rename doc refs to deprecated functions like openC (#8467) --- lib/std/os.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index dfebda6104..ef6d6a85cd 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1049,7 +1049,7 @@ pub const OpenError = error{ } || UnexpectedError; /// Open and possibly create a file. Keeps trying if it gets interrupted. -/// See also `openC`. +/// See also `openZ`. pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t { if (std.Target.current.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); @@ -1147,7 +1147,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` is relative to the open directory handle `dir_fd`. -/// See also `openatC`. +/// See also `openatZ`. pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t { if (builtin.os.tag == .wasi) { @compileError("use openatWasi instead"); @@ -1754,7 +1754,7 @@ pub const UnlinkError = error{ } || UnexpectedError; /// Delete a name and possibly the file it refers to. -/// See also `unlinkC`. +/// See also `unlinkZ`. pub fn unlink(file_path: []const u8) UnlinkError!void { if (builtin.os.tag == .wasi) { @compileError("unlink is not supported in WASI; use unlinkat instead"); From 33cb6608387d603f59b161c61db7255ef6e55704 Mon Sep 17 00:00:00 2001 From: Frank Denis <124872+jedisct1@users.noreply.github.com> Date: Sat, 1 May 2021 00:41:59 +0200 Subject: [PATCH 7/9] Doc: zig-cache/bin -> zig-out/bin (#8659) --- doc/langref.html.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index f52abeab73..05bdfbd043 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -10016,7 +10016,7 @@ pub fn build(b: *Builder) void { {#code_end#}

terminal

$ zig build
-$ ./zig-cache/bin/test
+$ ./zig-out/bin/test
 all your base are belong to us
{#see_also|Targets|Zig Build System#} {#header_close#} From 5fcc922ff2e0cfc52122e0a00270463e4d58ff00 Mon Sep 17 00:00:00 2001 From: Devin Bayer Date: Sat, 1 May 2021 01:05:45 +0200 Subject: [PATCH 8/9] add doc in `Anonymous Struct Literal` section for special @"0" syntax. (#8630) --- doc/langref.html.in | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index 05bdfbd043..744d33f85c 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2861,6 +2861,37 @@ fn dump(args: anytype) void { expect(args.b); expect(args.s[0] == 'h'); expect(args.s[1] == 'i'); +} + {#code_end#} +

+ Anonymous structs can be created without specifying field names, and are referred to as "tuples". +

+

+ The fields are implicitly named using numbers starting from 0. Because their names are integers, + the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers. +

+

+ Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}. +

+ {#code_begin|test|tuple#} +const std = @import("std"); +const expect = std.testing.expect; + +test "tuple" { + const values = .{ + @as(u32, 1234), + @as(f64, 12.34), + true, + "hi", + } ++ .{false} ** 2; + expect(values[0] == 1234); + expect(values[4] == false); + inline for (values) |v, i| { + if (i != 2) continue; + expect(v); + } + expect(values.len == 6); + expect(values.@"3"[0] == 'h'); } {#code_end#} {#header_close#} From 557eb414eee96201d1ae01b15ebf8955eca1da6d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 30 Apr 2021 21:55:28 -0700 Subject: [PATCH 9/9] CLI: fix incorrect error message with -cflags --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index f0772abbbd..a0a11bbbbc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -672,7 +672,7 @@ fn buildOutputType( extra_cflags.shrinkRetainingCapacity(0); while (true) { i += 1; - if (i + 1 >= args.len) fatal("expected -- after -cflags", .{}); + if (i >= args.len) fatal("expected -- after -cflags", .{}); if (mem.eql(u8, args[i], "--")) break; try extra_cflags.append(args[i]); }