From 1e499bf7136c92a201b6f15d9ec2a8e7cebaff75 Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Thu, 26 Sep 2024 14:41:58 +0800 Subject: [PATCH] chore: remove unsafe malloc in hostname (#171) --- src/hostname/set_hostname_nix.c.v | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/hostname/set_hostname_nix.c.v b/src/hostname/set_hostname_nix.c.v index 1e2131cb..0b7ebe10 100644 --- a/src/hostname/set_hostname_nix.c.v +++ b/src/hostname/set_hostname_nix.c.v @@ -8,16 +8,7 @@ fn set_hostname(hostname string) int { if hostname.len > 256 { return -1 } - - hostname_arr := unsafe { malloc(hostname.len) + 1 } - - unsafe { - for i in 0 .. hostname.len { - hostname_arr[i] = hostname[i] - } - hostname_arr[hostname.len] = 0 - } - return C.sethostname(hostname_arr, hostname.len) + return C.sethostname(&char(hostname.str), hostname.len) } // Fancy wrapper for error codes if set_hostname returns -1