Skip to content

Instantly share code, notes, and snippets.

@Namek
Last active March 10, 2023 19:33
Show Gist options
  • Save Namek/c597e812d4a72df5e5e22d95edc3bb97 to your computer and use it in GitHub Desktop.
Save Namek/c597e812d4a72df5e5e22d95edc3bb97 to your computer and use it in GitHub Desktop.
getty-zig: unitialized struct field that is not found in JSON gets freed causing segmentation fault
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "bug",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const opts = .{ .target = target, .optimize = optimize };
const json_module = b.dependency("json", opts).module("json");
exe.addModule("json", json_module);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
exe.install();
// This *creates* a RunStep in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = exe.run();
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing.
const exe_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}
.{
.name = "app",
.version = "1.0.0",
.dependencies = .{
.json = .{
.url = "https://github.com/getty-zig/json/archive/7aee46311bfd1625f6641914c26a49e8d3085880.tar.gz",
.hash = "12202fdb45919938ff856cc6129a52655415dabc4443dc9a05a16b74794d62fb57b7",
},
},
}
const std = @import("std");
const json = @import("json");
const WorldState = struct {
entities: std.ArrayList(Entity),
pub fn deinit(self: *WorldState) void {
self.entities.deinit();
}
};
const Entity = struct {
pub const @"getty.sb" = struct {
pub const attributes = .{
.Container = .{ .ignore_unknown_fields = true },
._cache = .{ .skip = true },
};
};
pub const @"getty.db" = struct {
pub const attributes = .{
.Container = .{ .ignore_unknown_fields = true },
._cache = .{ .skip = true },
};
};
id: u32 = 0,
_cache: bool,
};
pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const json_text =
\\{
\\
\\ "entities": [
\\ { "id": 32 }
\\ ]
\\}
;
const world = try json.fromSlice(gpa.allocator(), *WorldState, json_text);
std.log.debug("entity count: {d}", .{world.entities.items.len});
world.deinit();
gpa.allocator().destroy(world);
}
const std = @import("std");
const json = @import("json");
const WorldState = struct {
entities: std.ArrayList(Entity),
pub fn deinit(self: *WorldState) void {
self.entities.deinit();
}
};
const Entity = struct {
pub const @"getty.sb" = struct {
pub const attributes = .{
.Container = .{ .ignore_unknown_fields = true },
._cache = .{ .skip = true },
};
};
pub const @"getty.db" = struct {
pub const attributes = .{
.Container = .{ .ignore_unknown_fields = true },
._cache = .{ .skip = true },
};
};
id: u32 = 0,
_cache: bool = undefined,
};
pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const json_text =
\\{
\\
\\ "entities": [
\\ { "id": 32 }
\\ ]
\\}
;
const world = try json.fromSlice(gpa.allocator(), *WorldState, json_text);
std.log.debug("entity count: {d}", .{world.entities.items.len});
world.deinit();
gpa.allocator().destroy(world);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment