hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 5729444a930ad7c90283e4cad29f221f322f7f33
parent e6b95a90c0c9c863a40b2d6201d18a99d79600cf
Author: Mykyta Holubakha <hilobakho@gmail.com>
Date:   Wed, 29 May 2024 19:16:29 +0300

tests: fix low-hanging memory leaks

Diffstat:
Mbufio/scanner_test+test.ha | 3+++
Mcrypto/bigint/+test/encoding_test.ha | 4++++
Mcrypto/ec/curves+test.ha | 1+
Mencoding/base64/base64.ha | 3+++
Mnet/ip/test+test.ha | 4+++-
Mstrings/tokenize.ha | 4++++
Munix/hosts/test+test.ha | 1+
7 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/bufio/scanner_test+test.ha b/bufio/scanner_test+test.ha @@ -72,6 +72,7 @@ use strings; @test fn scan_rune() void = { let in = memio::fixed(strings::toutf8("hello")); let scanner = newscanner(&in, 32); + defer finish(&scanner); const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [ 'h', 'e', 'l', 'l', 'o', io::EOF, @@ -95,6 +96,7 @@ use strings; 'a', 0xE3, ]); let scanner = newscanner(&in, 32); + defer finish(&scanner); const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [ 'a', utf8::invalid, @@ -118,6 +120,7 @@ use strings; @test fn scan_byte() void = { let in = memio::fixed([1, 2, 3]); let scanner = newscanner(&in, 3); + defer finish(&scanner); assert(scan_byte(&scanner) as u8 == 1); assert(scan_byte(&scanner) as u8 == 2); diff --git a/crypto/bigint/+test/encoding_test.ha b/crypto/bigint/+test/encoding_test.ha @@ -53,6 +53,7 @@ use bytes; let mod = fromhex("00190000"); defer free(mod); let resultbuf: []word = alloc([0...], encodelen(input)); + defer free(resultbuf); let result: [4]u8 = [0...]; let ret = encodemod(resultbuf[..], input, mod); @@ -79,6 +80,7 @@ use bytes; let mod = fromhex("190000"); defer free(mod); let resultbuf: []word = alloc([0...], encodelen(input)); + defer free(resultbuf); let result: [4]u8 = [0...]; encodereduce(resultbuf, input, mod); @@ -87,12 +89,14 @@ use bytes; const input: [4]u8 = [0, 0x19, 0, 0]; let resultbuf: []word = alloc([0...], encodelen(input)); + defer free(resultbuf); encodereduce(resultbuf, input, mod); decode(result, resultbuf); assert(iszero(resultbuf) == 1); const input: [4]u8 = [0x24, 0x17, 0x01, 0x05]; let resultbuf: []word = alloc([0...], encodelen(input)); + defer free(resultbuf); encodereduce(resultbuf, input, mod); decode(result, resultbuf); assert(bytes::equal(result, [0x00, 0x0e, 0x01, 0x05])); diff --git a/crypto/ec/curves+test.ha b/crypto/ec/curves+test.ha @@ -78,6 +78,7 @@ fn tmuladd(c: *curve, tcs: []multc) void = { ]; let g = alloc(P256_G); + defer free(g); assert(c.mul(g, priv) == 1); assert(bytes::equal(g, expected)); diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha @@ -198,6 +198,9 @@ fn clear(e: *encoder) void = { io::close(&e)!; assert(memio::string(&buf)! == expected); + + let encb = memio::buffer(&buf); + free(encb); }; // Encodes a byte slice in base 64, using the given encoding, returning a slice diff --git a/net/ip/test+test.ha b/net/ip/test+test.ha @@ -22,7 +22,9 @@ fn ip_test(s: str, expected: (addr|invalid)) void = { if (ip is addr4) { assert(fmted == s); } else { - assert(strings::dup(fmted) == string(ipr)); + const dup = strings::dup(fmted); + defer free(dup); + assert(dup == string(ipr)); }; }; diff --git a/strings/tokenize.ha b/strings/tokenize.ha @@ -162,6 +162,7 @@ export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX); for (let i = 0z; i < len(expected); i += 1) { assert(expected[i] == actual[i]); }; + free(actual); const expected2 = ["Hello,", "my", "name", "is", "Drew"]; const actual2 = split("Hello, my name is Drew", " "); @@ -169,6 +170,7 @@ export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX); for (let i = 0z; i < len(expected2); i += 1) { assert(expected2[i] == actual2[i]); }; + free(actual2); const expected3 = ["one"]; const actual3 = splitn("one", "=", 2z); @@ -176,6 +178,7 @@ export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX); for (let i = 0z; i < len(expected3); i += 1) { assert(expected3[i] == actual3[i]); }; + free(actual3); const expected4 = ["Hello, my", "name", "is", "Drew"]; const actual4 = rsplitn("Hello, my name is Drew", " ", 4z); @@ -183,6 +186,7 @@ export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX); for (let i = 0z; i < len(expected4); i += 1) { assert(expected4[i] == actual4[i]); }; + free(actual4); }; // Returns a string "cut" along the first instance of a delimiter, returning diff --git a/unix/hosts/test+test.ha b/unix/hosts/test+test.ha @@ -67,4 +67,5 @@ def HOSTS_FILE = ` assert(len(addrs) == 2); assert(ip::equal(addrs[0], [10, 10, 10, 10]: ip::addr4)); assert(ip::equal(addrs[1], [10, 10, 20, 20]: ip::addr4)); + free(addrs); };