From 9f267f4c05b7440b724e56f8bc6d093ee97b5ad9 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 19:32:35 +0000 Subject: [PATCH] feat(cgo): normalize Errno for negative return codes Co-Authored-By: Virgil --- call_test.go | 6 ++++++ string_conversion.go | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/call_test.go b/call_test.go index 8b88b5e..42dc433 100644 --- a/call_test.go +++ b/call_test.go @@ -181,6 +181,12 @@ func TestErrnoMapping(t *testing.T) { } else if !errors.Is(err, syscall.Errno(2)) { t.Fatalf("expected error type %v, got %T %v", syscall.Errno(2), err, err) } + + if err := Errno(-2); err == nil { + t.Fatal("expected non-nil error for negative errno") + } else if !errors.Is(err, syscall.Errno(2)) { + t.Fatalf("expected positive errno mapping from -2, got %T %v", err, err) + } } func TestWithErrnoReturnsBothResultAndError(t *testing.T) { diff --git a/string_conversion.go b/string_conversion.go index 734ac1e..21a4747 100644 --- a/string_conversion.go +++ b/string_conversion.go @@ -847,7 +847,13 @@ func Errno(resultCode C.int) error { if resultCode == 0 { return nil } - return syscall.Errno(resultCode) + + recordedCode := resultCode + if recordedCode < 0 { + recordedCode = -recordedCode + } + + return syscall.Errno(recordedCode) } // WithErrno runs a function that returns C.int and maps the result to Go error. -- 2.45.3