hare

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

commit f81955048c5ca066067ee6c864756c148ef5d5f5
parent 38a5c85d8d45c3ef7836990dd6e704305d7285f6
Author: Tom Regner <tomte@tomsdiner.org>
Date:   Fri, 18 Nov 2022 00:10:56 +0100

Fix memory leaks in time::chrono @init

free everything allocated during @init, so that valgrind reports no possible
leaks when using time.

Signed-off-by: Tom Regner <tomte@tomsdiner.org>

Diffstat:
Mtime/chrono/leapsec.ha | 5+++++
Mtime/chrono/timezone.ha | 5+++++
Mtime/chrono/tzdb.ha | 15++++++++-------
3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha @@ -56,6 +56,10 @@ let utc_isinitialized: bool = false; }; }; +@fini fn free_utc() void = { + free(utc_leapsecs); +}; + fn init_utc_leapsecs() (void | utciniterror) = { const file = os::open(UTC_LEAPSECS_FILE)?; defer io::close(file)!; @@ -82,6 +86,7 @@ fn parse_utc_leapsecs( continue; }; const pair = strings::splitn(line, "\t", 3); + defer free(pair); if (len(pair) < 2) { continue; }; diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha @@ -215,6 +215,11 @@ let TZ_LOCAL: timezone = timezone { posix_extend = "", }; +@fini fn free_tzdata() void = { + free(TZ_LOCAL.transitions); + free(TZ_LOCAL.zones); +}; + @init fn set_local_timezone() void = { match (os::getenv("TZ")) { case let zone: str => diff --git a/time/chrono/tzdb.ha b/time/chrono/tzdb.ha @@ -141,28 +141,29 @@ fn parse_tzif(h: io::handle, tz: *timezone) (void | invalidtzif | io::error) = { } else { readitems4(h, &transition_times, timecnt)?; }; - + defer free(transition_times); const zone_indicies: []u8 = []; readbytes(h, &zone_indicies, timecnt)?; - + defer free(zone_indicies); const zonedata: []u8 = []; readbytes(h, &zonedata, typecnt * 6)?; - + defer free(zonedata); const abbrdata: []u8 = []; readbytes(h, &abbrdata, charcnt)?; - + defer free(abbrdata); const leapdata: []u8 = []; readbytes(h, &leapdata, leapcnt * (timesz: u32 + 4))?; - + defer free(leapdata); const stdwalldata: []u8 = []; readbytes(h, &stdwalldata, isstdcnt)?; - + defer free(stdwalldata); const normlocaldata: []u8 = []; readbytes(h, &normlocaldata, isutcnt)?; - + defer free(normlocaldata); // read footer let footerdata: []u8 = []; + defer free(footerdata); mustread(h, buf1)?; if (buf1[0] != 0x0A) { // '\n' newline return invalidtzif;