Skip to content

Commit

Permalink
fix: add defaults for optionals, guard against null deref calls (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice authored Oct 22, 2024
1 parent 8617368 commit b05ddbc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion template/src/schema.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 %>);
Expand All @@ -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 %>);
Expand Down

0 comments on commit b05ddbc

Please sign in to comment.