Fix segfault in core:sys/info on WSL2

This commit is contained in:
Jeroen van Rijn
2025-04-05 19:23:58 +02:00
parent 8d283cefa0
commit 4b203d0c78

View File

@@ -5,6 +5,7 @@ import "base:intrinsics"
import "core:strconv"
import "core:strings"
import "core:sys/linux"
import "core:fmt"
@(private)
version_string_buf: [1024]u8
@@ -16,9 +17,13 @@ init_os_version :: proc () {
b := strings.builder_from_bytes(version_string_buf[:])
// Try to parse `/etc/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
{
pretty_parse: {
fd, errno := linux.open("/etc/os-release", {})
assert(errno == .NONE, "Failed to read /etc/os-release")
if errno != .NONE {
strings.write_string(&b, "Unknown Linux Distro")
break pretty_parse
}
defer {
cerrno := linux.close(fd)
assert(cerrno == .NONE, "Failed to close the file descriptor")
@@ -26,7 +31,10 @@ init_os_version :: proc () {
os_release_buf: [2048]u8
n, read_errno := linux.read(fd, os_release_buf[:])
assert(read_errno == .NONE, "Failed to read data from /etc/os-release")
if read_errno != .NONE {
strings.write_string(&b, "Unknown Linux Distro")
break pretty_parse
}
release := string(os_release_buf[:n])
// Search the line in the file until we find "PRETTY_NAME="
@@ -59,7 +67,7 @@ init_os_version :: proc () {
os_version.as_string = strings.to_string(b)
// Parse the Linux version out of the release string
{
version_loop: {
version_num, _, version_suffix := strings.partition(release_str, "-")
os_version.version = version_suffix
@@ -72,11 +80,11 @@ init_os_version :: proc () {
case 0: dst = &os_version.major
case 1: dst = &os_version.minor
case 2: dst = &os_version.patch
case: break
case: break version_loop
}
num, ok := strconv.parse_int(part)
if !ok { break }
if !ok { break version_loop }
dst^ = num
}