From d46384e098ea684c6a321fdc3c2ddefbbc1de9a9 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 22 Oct 2024 12:02:52 -0700 Subject: [PATCH] fix: add defaults for optionals, guard against null deref calls --- template/src/schema.zig.ejs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/template/src/schema.zig.ejs b/template/src/schema.zig.ejs index bb0376c..edcb20c 100644 --- a/template/src/schema.zig.ejs +++ b/template/src/schema.zig.ejs @@ -95,7 +95,7 @@ pub const Host = struct { <% if (p.description) { -%> /// <%- formatCommentBlock(p.description, "/// ") %> <% } -%> - <%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %>, + <%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %><%- p.nullable ? " = null" : null %>, <% }) %> /// Internally used function, should not be called by plugin authors. @@ -105,7 +105,13 @@ pub const Host = struct { <% } %> <% schema.properties.forEach(p => { -%> <% if (p.$ref && !p.$ref.enum) { %> + <% if (p.nullable) { %> + if (self.<%- p.name %> != null) { + <% } -%> self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__decodeBase64Fields()).*; + <% if (p.nullable) { %> + } + <% } -%> <% } else if (p.type === 'buffer') { %> const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, try b64dec.calcSizeForSlice(self.<%- p.name %>)); try b64dec.decode(dest_<%- p.name %>, self.<%- p.name %>); @@ -123,7 +129,13 @@ pub const Host = struct { <% } %> <% schema.properties.forEach(p => { -%> <% if (p.$ref && !p.$ref.enum) { %> + <% if (p.nullable) { %> + if (self.<%- p.name %> != null) { + <% } -%> self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__encodeBase64Fields()).*; + <% if (p.nullable) { %> + } + <% } -%> <% } else if (p.type === 'buffer') { %> const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, b64enc.calcSize(self.<%- p.name %>.len)); self.<%- p.name %> = b64enc.encode(dest_<%- p.name %>, self.<%- p.name %>);