From 74a4469dfba9a63a57caabc65c4faa65b2a59308 Mon Sep 17 00:00:00 2001 From: suresh Date: Sun, 25 Jan 2026 12:06:17 -0500 Subject: fixed beffer overflow in vector. Added serialize and deserialize in json with vector reading from the struct with json --- examples/data/json_config.zc | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/data/json_config.zc b/examples/data/json_config.zc index 4c21742..6c1bee0 100644 --- a/examples/data/json_config.zc +++ b/examples/data/json_config.zc @@ -13,6 +13,12 @@ struct Features { metrics: bool; } +impl Features { + fn drop(self) { + // No heap allocations to free + } +} + @derive(FromJson, ToJson) struct Config { server_name: String; @@ -22,6 +28,17 @@ struct Config { allowed_hosts: Vec; } +impl Config { + fn drop(self) { + self.server_name.free(); + for i in 0..self.allowed_hosts.length() { + self.allowed_hosts.get(i).free(); + } + self.allowed_hosts.free(); + self.features.drop(); + } +} + fn main() { let path = "examples/data/config.json"; @@ -78,7 +95,7 @@ fn main() { let hosts = hosts_opt.unwrap(); let count = hosts.len(); "Allowed Hosts: ({count} entries)"; - for let i: usize = 0; i < count; i = i + 1 { + for i in 0..count { let host_opt = hosts.at(i); if host_opt.is_some() { let host_val = host_opt.unwrap(); @@ -109,7 +126,7 @@ fn main() { " features.metrics: {config.features.metrics}"; let hosts_count = config.allowed_hosts.length(); " allowed_hosts: ({hosts_count} entries)"; - for let i: usize = 0; i < hosts_count; i = i + 1 { + for i in 0..hosts_count { let host = config.allowed_hosts.get(i).c_str(); " - {host}"; } @@ -149,12 +166,8 @@ fn main() { " features.metrics: {out_metrics}"; " allowed_hosts: {out_hosts_count} entries"; - // Cleanup - config.server_name.free(); - for let i: usize = 0; i < config.allowed_hosts.length(); i = i + 1 { - config.allowed_hosts.get(i).free(); - } - config.allowed_hosts.free(); + // Cleanup - RAII via drop() + config.drop(); json_out.free(); json_str.free(); JsonValue::free(root); -- cgit v1.2.3