1
0
mirror of https://github.com/golang/go synced 2024-11-22 10:44:41 -07:00

os/signal: check int type of signal using reflection to fix windows notification

This commit is contained in:
wineandchord 2024-11-15 17:34:27 +08:00
parent f99f5da18f
commit 3a8a7cac38

View File

@ -8,6 +8,7 @@ package signal
import ( import (
"os" "os"
"reflect"
"syscall" "syscall"
) )
@ -41,7 +42,24 @@ func signum(sig os.Signal) int {
} }
return i return i
default: default:
return -1 // Use reflection to determine if sig has an underlying integer type.
// In some systems like Windows, os.Signal may have implementations
// with underlying integer types that are not directly accessible,
// as they might be defined in external packages like golang.org/x/sys.
// Since we cannot import those platform-specific signal types,
// reflection allows us to handle them in a generic way.
kind := reflect.TypeOf(sig).Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// Extract the integer value from sig and validate it.
i := int(reflect.ValueOf(sig).Int())
if i < 0 || i >= numSig {
return -1
}
return i
default:
return -1
}
} }
} }