[2/9] golang/xenlight: Do proper nil / NULL conversions for builtin Bitmap type
diff mbox series

Message ID 20191227163224.4113837-2-george.dunlap@citrix.com
State New, archived
Headers show
Series
  • [1/9] golang/xenlight: Don't try to marshall zero-length arrays
Related show

Commit Message

George Dunlap Dec. 27, 2019, 4:32 p.m. UTC
Similar to the autogenerated types, but for `builtin` Bitmap type.

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

Comments

Nick Rosbrook Jan. 4, 2020, 6 p.m. UTC | #1
On Fri, Dec 27, 2019 at 11:33 AM George Dunlap <george.dunlap@citrix.com> wrote:
>
> Similar to the autogenerated types, but for `builtin` Bitmap type.
>
> Signed-off-by: George Dunlap <george.dunlap@citrix.com>
Reviewed-by: Nick Rosbrook <rosbrookn@ainfosec.com>

Patch
diff mbox series

diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index 237f26bce9..e18f0f35f8 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -408,15 +408,17 @@  type Bitmap struct {
 }
 
 func (bm *Bitmap) fromC(cbm *C.libxl_bitmap) error {
-	// Alloc a Go slice for the bytes
-	size := int(cbm.size)
-	bm.bitmap = make([]C.uint8_t, size)
+	bm.bitmap = nil
+	if size := int(cbm.size); size > 0 {
+		// Alloc a Go slice for the bytes
+		bm.bitmap = make([]C.uint8_t, size)
 
-	// Make a slice pointing to the C array
-	cs := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
+		// Make a slice pointing to the C array
+		cs := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
 
-	// And copy the C array into the Go array
-	copy(bm.bitmap, cs)
+		// And copy the C array into the Go array
+		copy(bm.bitmap, cs)
+	}
 
 	return nil
 }
@@ -426,10 +428,12 @@  func (bm *Bitmap) toC() (C.libxl_bitmap, error) {
 
 	size := len(bm.bitmap)
 	cbm.size = C.uint32_t(size)
-	cbm._map = (*C.uint8_t)(C.malloc(C.ulong(cbm.size) * C.sizeof_uint8_t))
-	cs := (*[1 << 31]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
+	if cbm.size > 0 {
+		cbm._map = (*C.uint8_t)(C.malloc(C.ulong(cbm.size) * C.sizeof_uint8_t))
+		cs := (*[1 << 31]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
 
-	copy(cs, bm.bitmap)
+		copy(cs, bm.bitmap)
+	}
 
 	return cbm, nil
 }