From 9e7a1c444fdccaa26793f14728df7935964276e1 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Wed, 10 Jun 2020 10:38:27 +0200 Subject: [PATCH] rbd: add error checking in OpenImage*() functions Signed-off-by: Niels de Vos --- rbd/rbd.go | 28 ++++++++++++++++++++++++++++ rbd/rbd_test.go | 23 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/rbd/rbd.go b/rbd/rbd.go index 42decf2..61f9669 100644 --- a/rbd/rbd.go +++ b/rbd/rbd.go @@ -1028,6 +1028,13 @@ func TrashRestore(ioctx *rados.IOContext, id, name string) error { // int rbd_open(rados_ioctx_t io, const char *name, // rbd_image_t *image, const char *snap_name); func OpenImage(ioctx *rados.IOContext, name, snapName string) (*Image, error) { + if ioctx == nil { + return nil, ErrNoIOContext + } + if name == "" { + return nil, ErrNoName + } + cName := C.CString(name) defer C.free(unsafe.Pointer(cName)) @@ -1064,6 +1071,13 @@ func OpenImage(ioctx *rados.IOContext, name, snapName string) (*Image, error) { // int rbd_open_read_only(rados_ioctx_t io, const char *name, // rbd_image_t *image, const char *snap_name); func OpenImageReadOnly(ioctx *rados.IOContext, name, snapName string) (*Image, error) { + if ioctx == nil { + return nil, ErrNoIOContext + } + if name == "" { + return nil, ErrNoName + } + cName := C.CString(name) defer C.free(unsafe.Pointer(cName)) @@ -1101,6 +1115,13 @@ func OpenImageReadOnly(ioctx *rados.IOContext, name, snapName string) (*Image, e // int rbd_open_by_id(rados_ioctx_t io, const char *id, // rbd_image_t *image, const char *snap_name); func OpenImageById(ioctx *rados.IOContext, id, snapName string) (*Image, error) { + if ioctx == nil { + return nil, ErrNoIOContext + } + if id == "" { + return nil, ErrNoName + } + cid := C.CString(id) defer C.free(unsafe.Pointer(cid)) @@ -1138,6 +1159,13 @@ func OpenImageById(ioctx *rados.IOContext, id, snapName string) (*Image, error) // int rbd_open_by_id_read_only(rados_ioctx_t io, const char *id, // rbd_image_t *image, const char *snap_name); func OpenImageByIdReadOnly(ioctx *rados.IOContext, id, snapName string) (*Image, error) { + if ioctx == nil { + return nil, ErrNoIOContext + } + if id == "" { + return nil, ErrNoName + } + cid := C.CString(id) defer C.free(unsafe.Pointer(cid)) diff --git a/rbd/rbd_test.go b/rbd/rbd_test.go index 6315495..f34d752 100644 --- a/rbd/rbd_test.go +++ b/rbd/rbd_test.go @@ -1242,6 +1242,13 @@ func TestOpenImage(t *testing.T) { name := GetUUID() + // pass invalid arguments + _, err = OpenImage(nil, "some-image", NoSnapshot) + require.Error(t, err) + _, err = OpenImage(ioctx, "", NoSnapshot) + require.Error(t, err) + + // image does not exist yet _, err = OpenImage(ioctx, name, NoSnapshot) assert.Error(t, err) @@ -1255,6 +1262,12 @@ func TestOpenImage(t *testing.T) { assert.NoError(t, err) // open read-only + // pass invalid parameters + _, err = OpenImageReadOnly(nil, "some-image", NoSnapshot) + require.Error(t, err) + _, err = OpenImageReadOnly(ioctx, "", NoSnapshot) + require.Error(t, err) + oImage, err = OpenImageReadOnly(ioctx, name, NoSnapshot) assert.NoError(t, err) @@ -1495,6 +1508,16 @@ func TestOpenImageById(t *testing.T) { err = workingImage.Close() assert.NoError(t, err) + t.Run("InvalidArguments", func(t *testing.T) { + _, err = OpenImageById(nil, "some-id", NoSnapshot) + require.Error(t, err) + _, err = OpenImageById(ioctx, "", NoSnapshot) + require.Error(t, err) + _, err = OpenImageByIdReadOnly(nil, "some-id", NoSnapshot) + require.Error(t, err) + _, err = OpenImageByIdReadOnly(ioctx, "", NoSnapshot) + require.Error(t, err) + }) t.Run("ReadWriteBadId", func(t *testing.T) { t.Skip("segfaults due to https://tracker.ceph.com/issues/43178") // phony id