diff mbox series

[4/9] go/xenlight: Fix CpuidPoliclyList conversion

Message ID 20191227163224.4113837-4-george.dunlap@citrix.com (mailing list archive)
State New, archived
Headers show
Series [1/9] golang/xenlight: Don't try to marshall zero-length arrays | expand

Commit Message

George Dunlap Dec. 27, 2019, 4:32 p.m. UTC
Empty Go strings should be converted to `nil` libxl_cpuid_policy_list;
otherwise libxl_cpuid_parse_config gets confused.

Also, libxl_cpuid_policy_list returns a weird error, not a "normal"
libxl error; if it returns one of these non-standard errors, convert
it to ErrorInval.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
---
CC: Nick Rosbrook <rosbrookn@ainfosec.com>
---
 tools/golang/xenlight/xenlight.go | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Nick Rosbrook Jan. 4, 2020, 6:42 p.m. UTC | #1
> Empty Go strings should be converted to `nil` libxl_cpuid_policy_list;
> otherwise libxl_cpuid_parse_config gets confused.
>
> Also, libxl_cpuid_policy_list returns a weird error, not a "normal"
> libxl error; if it returns one of these non-standard errors, convert
> it to ErrorInval.
>
> Signed-off-by: George Dunlap <george.dunlap@citrix.com>

Looks good to me. If "golang/xenlight: begin Go to C type marshaling"
is committed before this, the signature of toC will need to be updated
as per your suggestion.

Otherwise,

Reviewed-by: Nick Rosbrook <rosbrookn@ainfosec.com>
diff mbox series

Patch

diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index e18f0f35f8..99de68320b 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -317,6 +317,10 @@  type CpuidPolicyList string
 func (cpl CpuidPolicyList) fromC(ccpl *C.libxl_cpuid_policy_list) error { return nil }
 
 func (cpl CpuidPolicyList) toC() (C.libxl_cpuid_policy_list, error) {
+	if cpl == "" {
+		return nil, nil
+	}
+
 	var ccpl C.libxl_cpuid_policy_list
 
 	s := C.CString(string(cpl))
@@ -326,7 +330,8 @@  func (cpl CpuidPolicyList) toC() (C.libxl_cpuid_policy_list, error) {
 	if ret != 0 {
 		C.libxl_cpuid_dispose(&ccpl)
 
-		return ccpl, Error(-ret)
+		// libxl_cpuid_parse_config doesn't return a normal libxl error.
+		return ccpl, ErrorInval
 	}
 
 	return ccpl, nil