feat(cgo): normalize Errno for negative return codes

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 19:32:35 +00:00
parent f19b691112
commit 9f267f4c05
2 changed files with 13 additions and 1 deletions

View file

@ -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) {

View file

@ -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.