diff --git a/usb_darwin.go b/usb_darwin.go index 9b5c16c..fa63667 100644 --- a/usb_darwin.go +++ b/usb_darwin.go @@ -151,7 +151,17 @@ func (me *C.io_registry_entry_t) GetStringProperty(key string) (string, error) { return "", errors.New("Property not found: " + key) } 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) { @@ -163,7 +173,9 @@ func (me *C.io_registry_entry_t) GetIntProperty(key string, intType C.CFNumberTy } defer C.CFRelease(property) 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 }