Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add defaults for optionals, guard against null deref calls #9

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading