authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-17 13:35:39+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-17 13:35:39+02:00
log5bc4417d2a5155ceb10c73b5f56229c8f66539fc
tree63bf3de943a4531e145f00e29cb52381abf8f94c
parent17af53554e1362f9632aff592afcba11190c94b1

tapi: fix memory bugs in yaml parser


1 files changed, 20 insertions(+), 13 deletions(-)

src/link/tapi/parse.zig+20-13
......@@ -40,10 +40,26 @@ pub const Node = struct {
4040
4141 pub fn deinit(self: *Node, allocator: Allocator) void {
4242 switch (self.tag) {
43 .doc => @fieldParentPtr(Node.Doc, "base", self).deinit(allocator),
44 .map => @fieldParentPtr(Node.Map, "base", self).deinit(allocator),
45 .list => @fieldParentPtr(Node.List, "base", self).deinit(allocator),
46 .value => @fieldParentPtr(Node.Value, "base", self).deinit(allocator),
43 .doc => {
44 const parent = @fieldParentPtr(Node.Doc, "base", self);
45 parent.deinit(allocator);
46 allocator.destroy(parent);
47 },
48 .map => {
49 const parent = @fieldParentPtr(Node.Map, "base", self);
50 parent.deinit(allocator);
51 allocator.destroy(parent);
52 },
53 .list => {
54 const parent = @fieldParentPtr(Node.List, "base", self);
55 parent.deinit(allocator);
56 allocator.destroy(parent);
57 },
58 .value => {
59 const parent = @fieldParentPtr(Node.Value, "base", self);
60 parent.deinit(allocator);
61 allocator.destroy(parent);
62 },
4763 }
4864 }
4965
......@@ -76,7 +92,6 @@ pub const Node = struct {
7692 pub fn deinit(self: *Doc, allocator: Allocator) void {
7793 if (self.value) |node| {
7894 node.deinit(allocator);
79 allocator.destroy(node);
8095 }
8196 }
8297
......@@ -122,7 +137,6 @@ pub const Node = struct {
122137 for (self.values.items) |entry| {
123138 if (entry.value) |value| {
124139 value.deinit(allocator);
125 allocator.destroy(value);
126140 }
127141 }
128142 self.values.deinit(allocator);
......@@ -163,7 +177,6 @@ pub const Node = struct {
163177 pub fn deinit(self: *List, allocator: Allocator) void {
164178 for (self.values.items) |node| {
165179 node.deinit(allocator);
166 allocator.destroy(node);
167180 }
168181 self.values.deinit(allocator);
169182 }
......@@ -239,7 +252,6 @@ pub const Tree = struct {
239252 self.line_cols.deinit();
240253 for (self.docs.items) |doc| {
241254 doc.deinit(self.allocator);
242 self.allocator.destroy(doc);
243255 }
244256 self.docs.deinit(self.allocator);
245257 }
......@@ -386,7 +398,6 @@ const Parser = struct {
386398 }
387399 errdefer if (node.value) |val| {
388400 val.deinit(self.allocator);
389 self.allocator.destroy(val);
390401 };
391402
392403 // Parse footer
......@@ -426,7 +437,6 @@ const Parser = struct {
426437 for (node.values.items) |entry| {
427438 if (entry.value) |val| {
428439 val.deinit(self.allocator);
429 self.allocator.destroy(val);
430440 }
431441 }
432442 node.values.deinit(self.allocator);
......@@ -467,7 +477,6 @@ const Parser = struct {
467477 const val = try self.value();
468478 errdefer if (val) |v| {
469479 v.deinit(self.allocator);
470 self.allocator.destroy(v);
471480 };
472481
473482 if (val) |v| {
......@@ -503,7 +512,6 @@ const Parser = struct {
503512 errdefer {
504513 for (node.values.items) |val| {
505514 val.deinit(self.allocator);
506 self.allocator.destroy(val);
507515 }
508516 node.values.deinit(self.allocator);
509517 }
......@@ -535,7 +543,6 @@ const Parser = struct {
535543 errdefer {
536544 for (node.values.items) |val| {
537545 val.deinit(self.allocator);
538 self.allocator.destroy(val);
539546 }
540547 node.values.deinit(self.allocator);
541548 }