hare

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

commit 57ac568e30c2258c9b06a6b624e207dfbad6394a
parent 01fb3245312704f0fad54c55f6edba22fefee509
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat, 30 Sep 2023 23:26:22 -0400

time::chrono: use static buffer in strerror

Instead of allocating, which caused a guaranteed memory leak since not
all error strings were heap-allocated.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mtime/chrono/error.ha | 8+++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/time/chrono/error.ha b/time/chrono/error.ha @@ -15,7 +15,8 @@ export type error = !( | analytical ); -// Converts an [[error]] into a human-friendly string. +// Converts an [[error]] into a human-friendly string. The result may be +// statically allocated. export fn strerror(err: error) const str = { match (err) { case invalid => @@ -23,14 +24,15 @@ export fn strerror(err: error) const str = { case invalidtzif => return "Invalid TZif data"; case let err: tzdberror => + static let buf: [1024]u8 = [0...]; match (err) { case let err: fs::error => - return fmt::asprintf( + return fmt::bsprintf(buf, "Timezone database error: {}", fs::strerror(err), ); case let err: io::error => - return fmt::asprintf( + return fmt::bsprintf(buf, "Timezone database error: {}", io::strerror(err), );