Improved error checking on osx USB detection

This commit is contained in:
Cristian Maglie
2016-12-25 18:26:18 +01:00
parent ecd31dc766
commit b1f15cfb64

View File

@@ -151,7 +151,17 @@ func (me *C.io_registry_entry_t) GetStringProperty(key string) (string, error) {
return "", errors.New("Property not found: " + key) return "", errors.New("Property not found: " + key)
} }
defer C.CFRelease(property) defer C.CFRelease(property)
return C.GoString(C.CFStringGetCStringPtr(property, 0)), nil
if ptr := C.CFStringGetCStringPtr(property, 0); ptr != nil {
return C.GoString(ptr), nil
}
// in certain circumstances CFStringGetCStringPtr may return NULL
// and we must retrieve the string by copy
buff := make([]C.char, 1024)
if C.CFStringGetCString(property, &buff[0], 1024, 0) != C.true {
return "", fmt.Errorf("Property '%s' can't be converted", key)
}
return C.GoString(&buff[0]), nil
} }
func (me *C.io_registry_entry_t) GetIntProperty(key string, intType C.CFNumberType) (int, error) { func (me *C.io_registry_entry_t) GetIntProperty(key string, intType C.CFNumberType) (int, error) {
@@ -163,7 +173,9 @@ func (me *C.io_registry_entry_t) GetIntProperty(key string, intType C.CFNumberTy
} }
defer C.CFRelease(property) defer C.CFRelease(property)
var res int var res int
C.CFNumberGetValue(property, intType, unsafe.Pointer(&res)) if C.CFNumberGetValue(property, intType, unsafe.Pointer(&res)) != C.true {
return res, fmt.Errorf("Property '%s' can't be converted or has been truncated", key)
}
return res, nil return res, nil
} }