Skip to content

Commit

Permalink
use an array for 'id' so that we're not returning pointer to a local
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Jun 4, 2024
1 parent 3caa118 commit 151e112
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/cmd/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:
fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module, alloc: std.mem.Allocator) !void {
for (list) |mod| {
if (mod.type == .system_lib or mod.type == .framework) continue;
if (std.mem.eql(u8, mod.id, "root")) {
if (std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
try w.writeAll(" pub const _root = \"\";\n");
continue;
}
Expand Down Expand Up @@ -325,7 +325,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul
mod.short_id(),
mod.short_id(),
});
if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {
if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
try w.print(
\\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }},
\\
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath:
.hg => @panic("TODO"),
.http => @panic("TODO"),
}
if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {
if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
try w.print(" .name = \"{s}\",\n", .{mod.name});
try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath), mod.main });

Expand Down
9 changes: 4 additions & 5 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
}
return zigmod.Module{
.type = .local,
.id = "root",
.id = zigmod.Module.ROOT,
.name = "root",
.main = m.main,
.deps = try moduledeps.toOwnedSlice(),
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
.system_lib, .framework => {
return zigmod.Module{
.type = d.type,
.id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path),
.id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Shake(96), d.path),
.name = d.path,
.only_os = d.only_os,
.except_os = d.except_os,
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
dd.for_build = d.for_build;
const save = dd;
if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..];
if (dd.id.len == 0) dd.id = &u.random_string(48);
if (std.mem.eql(u8, &dd.id, &zigmod.Dep.EMPTY)) dd.id = u.random_string(48);
if (d.name.len > 0) dd.name = d.name;
if (d.main.len > 0) dd.main = d.main;
if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;
Expand Down Expand Up @@ -352,12 +352,11 @@ pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]str
.type = std.meta.stringToEnum(zigmod.Dep.Type, iter.next().?).?,
.path = iter.next().?,
.version = iter.next().?,
.id = "",
.id = zigmod.Dep.EMPTY,
.name = "",
.main = "",
.yaml = null,
.deps = &.{},
.parent_id = "",
};
try list.append([4]string{
try asdep.clean_path(alloc),
Expand Down
5 changes: 3 additions & 2 deletions src/util/dep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const Dep = struct {

type: Type,
path: string,
id: string,
id: [48]u8,
name: string,
main: string,
version: string,
Expand All @@ -28,7 +28,8 @@ pub const Dep = struct {
deps: []zigmod.Dep,
keep: bool = false,
for_build: bool = false,
parent_id: string,

pub const EMPTY = (" " ** 48).*;

pub const Type = @import("./dep_type.zig").DepType;

Expand Down
10 changes: 5 additions & 5 deletions src/util/funcs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)
const data = try file.reader().readAllAlloc(alloc, gb);
const expected = hash.string;
const actual = switch (hash.id) {
.blake3 => try do_hash(alloc, std.crypto.hash.Blake3, data),
.sha256 => try do_hash(alloc, std.crypto.hash.sha2.Sha256, data),
.sha512 => try do_hash(alloc, std.crypto.hash.sha2.Sha512, data),
.blake3 => &try do_hash(alloc, std.crypto.hash.Blake3, data),
.sha256 => &try do_hash(alloc, std.crypto.hash.sha2.Sha256, data),
.sha512 => &try do_hash(alloc, std.crypto.hash.sha2.Sha512, data),
};
const result = std.mem.startsWith(u8, actual, expected);
if (!result) {
Expand All @@ -143,13 +143,13 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)
return result;
}

pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) !string {
pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) ![algo.digest_length * 2]u8 {
var h = algo.init(.{});
var out: [algo.digest_length]u8 = undefined;
h.update(data);
h.final(&out);
const hex = try std.fmt.allocPrint(alloc, "{x}", .{std.fmt.fmtSliceHexLower(out[0..])});
return hex;
return hex[0 .. algo.digest_length * 2].*;
}

/// Returns the result of running `git rev-parse HEAD`
Expand Down
14 changes: 9 additions & 5 deletions src/util/modfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mb = kb * 1024;
pub const ModFile = struct {
const Self = @This();

id: string,
id: [48]u8,
name: string,
main: string,
c_include_dirs: []const string,
Expand Down Expand Up @@ -52,7 +52,9 @@ pub const ModFile = struct {
}

pub fn from_mapping(alloc: std.mem.Allocator, mapping: yaml.Mapping) !Self {
const id = mapping.get_string("id") orelse "";
var id = zigmod.Dep.EMPTY;
std.mem.copyForwards(u8, &id, mapping.get_string("id") orelse &u.random_string(48));

const name = mapping.get_string("name") orelse "";
const main = mapping.get_string("main") orelse "";

Expand All @@ -61,7 +63,7 @@ pub const ModFile = struct {
}

return Self{
.id = if (id.len == 0) &u.random_string(48) else id,
.id = id,
.name = name,
.main = main,
.c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),
Expand Down Expand Up @@ -120,10 +122,13 @@ pub const ModFile = struct {
}
}

var id = zigmod.Dep.EMPTY;
std.mem.copyForwards(u8, &id, item.mapping.get_string("id") orelse "");

try dep_list.append(zigmod.Dep{
.type = dep_type,
.path = path,
.id = item.mapping.get_string("id") orelse "",
.id = id,
.name = name,
.main = main,
.version = version.?,
Expand All @@ -136,7 +141,6 @@ pub const ModFile = struct {
.deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build),
.keep = std.mem.eql(u8, "true", item.mapping.get_string("keep") orelse ""),
.for_build = for_build,
.parent_id = mapping.get_string("id") orelse "",
});
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/util/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const common = @import("./../common.zig");

pub const Module = struct {
type: zigmod.Dep.Type,
id: string,
id: [48]u8,
name: string,
main: string,
c_include_dirs: []const string = &.{},
Expand All @@ -28,6 +28,8 @@ pub const Module = struct {
for_build: bool = false,
min_zig_version: ?std.SemanticVersion,

pub const ROOT: [48]u8 = ("root" ++ (" " ** 44)).*;

pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, cachepath: string, options: *common.CollectOptions) !Module {
var moddeps = std.ArrayList(Module).init(alloc);
errdefer moddeps.deinit();
Expand All @@ -37,9 +39,13 @@ pub const Module = struct {
try moddeps.append(founddep);
}
}

var id = dep.id;
if (std.mem.eql(u8, &id, &zigmod.Dep.EMPTY)) id = u.random_string(48);

return Module{
.type = dep.type,
.id = if (dep.id.len > 0) dep.id else &u.random_string(48),
.id = id,
.name = dep.name,
.main = dep.main,
.c_include_dirs = dep.c_include_dirs,
Expand All @@ -57,7 +63,7 @@ pub const Module = struct {
}

pub fn eql(self: Module, another: Module) bool {
return std.mem.eql(u8, self.id, another.id);
return std.mem.eql(u8, &self.id, &another.id);
}

pub fn get_hash(self: Module, alloc: std.mem.Allocator, cdpath: string) !string {
Expand Down Expand Up @@ -131,8 +137,8 @@ pub const Module = struct {
return false;
}

pub fn short_id(self: Module) string {
return u.slice(u8, self.id, 0, 12);
pub fn short_id(self: *const Module) string {
return u.slice(u8, &self.id, 0, @min(12, std.mem.indexOfScalar(u8, &self.id, ' ') orelse self.id.len));
}

pub fn minZigVersion(self: Module) ?std.SemanticVersion {
Expand Down

0 comments on commit 151e112

Please sign in to comment.