From d9962de8edaa5d641830e0c6e2037bce86347165 Mon Sep 17 00:00:00 2001
From: Mykyta Holubakha <hilobakho@gmail.com>
Date: Wed, 29 May 2024 19:16:28 +0300
Subject: [PATCH] std: fix some low-hanging memory leaks

Origin: Upstream
Upstream-Status: Backport [commit e6b95a90c0c9c863a40b2d6201d18a99d79600cf]
Signed-off-by: Ismael Luceno <ismael@sourcemage.org>
---
 net/uri/parse.ha         | 4 ++++
 regex/regex.ha           | 2 ++
 time/chrono/timescale.ha | 7 +++++++
 3 files changed, 13 insertions(+)

diff --git a/net/uri/parse.ha b/net/uri/parse.ha
index 74c390c9bfd4..03c1fde3ba45 100644
--- a/net/uri/parse.ha
+++ b/net/uri/parse.ha
@@ -91,6 +91,7 @@ export fn parse(in: str) (uri | invalid) = {
 		case let s: str =>
 			yield match (ip::parse(s)) {
 			case let a: ip::addr =>
+				free(s);
 				yield a;
 			case ip::invalid =>
 				yield s;
@@ -167,6 +168,7 @@ fn parse_authority(
 			};
 
 			const addr = percent_decode(memio::string(&buf)!)?;
+			defer free(addr);
 			match (ip::parse(addr)) {
 			case let v6: ip::addr6 =>
 				host = v6;
@@ -363,6 +365,7 @@ fn percent_decode_static(out: io::handle, s: str) (void | invalid) = {
 						return invalid;
 					};
 
+					free(percent_data);
 					percent_data = [];
 				};
 
@@ -377,6 +380,7 @@ fn percent_decode_static(out: io::handle, s: str) (void | invalid) = {
 					return invalid;
 				};
 
+				free(percent_data);
 				percent_data = [];
 			};
 
diff --git a/regex/regex.ha b/regex/regex.ha
index 5d4792cd9e7b..bbdbb60ca842 100644
--- a/regex/regex.ha
+++ b/regex/regex.ha
@@ -909,6 +909,7 @@ fn parse_replace_target(targetstr: str) ([]([]u8 | size) | error) = {
 
 			const r = match (strings::next(&iter)) {
 			case void =>
+				free(target);
 				return "Trailing backslash": error;
 			case let r: rune =>
 				yield r;
@@ -919,6 +920,7 @@ fn parse_replace_target(targetstr: str) ([]([]u8 | size) | error) = {
 			} else if (ascii::isdigit(r)) {
 				append(target, r: u32: size - 0x30);
 			} else {
+				free(target);
 				return "Backslash must be followed by positive decimal number or a backslash": error;
 			};
 
diff --git a/time/chrono/timescale.ha b/time/chrono/timescale.ha
index 71c0007f78c5..d3b9ba504411 100644
--- a/time/chrono/timescale.ha
+++ b/time/chrono/timescale.ha
@@ -553,6 +553,10 @@ fn mtc_convfrom(ts: *timescale, i: time::instant) ([]time::instant | void) = {
 				},
 			));
 		};
+		if (actual is []time::instant) {
+			free(actual as []time::instant);
+		};
+
 	};
 };
 
@@ -649,5 +653,8 @@ fn mtc_convfrom(ts: *timescale, i: time::instant) ([]time::instant | void) = {
 				},
 			));
 		};
+		if (actual is []time::instant) {
+			free(actual as []time::instant);
+		};
 	};
 };
-- 
2.44.0

