commit 56fa07ad41876e6df06025b5635e6ede83076f14
parent 61dc4ff8dfb1d70eed51ad609064cdad70f53024
Author: Lou Woell <lou.woell@posteo.de>
Date: Thu, 11 Sep 2025 13:45:57 +0200
Fix memory leaks
Diffstat:
4 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/cmd/harehelper/list_symbols.ha b/cmd/harehelper/list_symbols.ha
@@ -24,11 +24,11 @@ fn list (cmd: getopt::command, ctx: *module::context) void = {
defer ast::ident_free(id);
let list = list_symbols(ctx, id);
+ defer idents_finish(list);
+
for (let i .. list) {
printident(i);
- ast::ident_free(i);
};
- free(list);
};
fn list_symbols (ctx: *module::context, id: ast::ident) []ast::ident = {
@@ -39,28 +39,28 @@ fn list_symbols (ctx: *module::context, id: ast::ident) []ast::ident = {
for (let s .. src.ha) {
let decls = scan(s)!;
+ defer decls_finish(decls);
+
for (let d .. decls) {
if (!d.exported) continue;
match (d.decl) {
- case let d: []ast::decl_const => void;
+ case let d: []ast::decl_const =>
for (let t .. d) {
append(res, ast::ident_dup(t.ident))!;
};
- case let d: []ast::decl_global => void;
+ case let d: []ast::decl_global =>
for (let t .. d) {
append(res, ast::ident_dup(t.ident))!;
};
- case let d: []ast::decl_type => void;
+ case let d: []ast::decl_type =>
for (let t .. d) {
append(res, ast::ident_dup(t.ident))!;
};
- case let d: ast::decl_func => void;
+ case let d: ast::decl_func =>
append(res, ast::ident_dup(d.ident))!;
case let d: ast::assert_expr => void;
};
- ast::decl_finish(d);
};
- free(decls);
};
return res;
diff --git a/cmd/harehelper/locate.ha b/cmd/harehelper/locate.ha
@@ -20,6 +20,7 @@ use hare::module;
use hare::parse;
use os;
use path;
+use strings;
fn locate (cmd: command, ctx: *module::context) void = {
@@ -31,12 +32,14 @@ fn locate (cmd: command, ctx: *module::context) void = {
case let id: ast::ident =>
yield id;
};
+ defer ast::ident_free(id);
match (locate_symbol(ctx, id)) {
case void =>
return void;
case let d: lex::location =>
fmt::printfln("{}:{}:{}", d.path, d.line, d.col)!;
+ free(d.path);
};
};
@@ -57,41 +60,41 @@ fn locate_symbol (
case let r: (str, module::srcset) =>
yield r;
};
+ defer module::finish_srcset(&src);
id = ident_last(id);
for (let s .. src.ha) {
let decls = scan(s)!;
- defer for (let d .. decls) {
- ast::decl_finish(d);
- };
+ defer decls_finish(decls);
+
+ let found = false;
for (let decl .. decls) {
match (decl.decl) {
case let d: []ast::decl_const =>
for (let t .. d) {
- if (ast::ident_eq(id, t.ident)) {
- return decl.start;
- };
+ found = ast::ident_eq(id, t.ident);
};
case let d: []ast::decl_global =>
for (let t .. d) {
- if (ast::ident_eq(id, t.ident)) {
- return decl.start;
- };
+ found = ast::ident_eq(id, t.ident);
};
case let d: []ast::decl_type =>
for (let t .. d) {
- if (ast::ident_eq(id, t.ident)) {
- return decl.start;
- };
+ found = ast::ident_eq(id, t.ident);
};
case let d: ast::decl_func =>
- if (ast::ident_eq(id, d.ident)) {
- return decl.start;
- };
+ found = ast::ident_eq(id, d.ident);
case => void;
};
+
+ if (found) return lex::location {
+ path = strings::dup(decl.start.path)!,
+ line =decl.start.line,
+ col = decl.start.col,
+ off = decl.start.off,
+ };
};
};
};
diff --git a/cmd/harehelper/resolve.ha b/cmd/harehelper/resolve.ha
@@ -34,6 +34,7 @@ fn resolve(cmd: getopt::command, ctx: *module::context) void = {
case let e: parse::error =>
return void;
};
+ defer ast::ident_free(id);
let file = if (len(cmd.args) > 1) {
yield cmd.args[1];
diff --git a/cmd/harehelper/util.ha b/cmd/harehelper/util.ha
@@ -61,3 +61,10 @@ fn idents_finish (ids: []ast::ident) void = {
};
free(ids);
};
+
+fn decls_finish (decls: []ast::decl) void = {
+ for (let i = 0z; i < len(decls); i += 1) {
+ ast::decl_finish(decls[i]);
+ };
+ free(decls);
+};