From 2146f1af0c023aa8bb61458715dfc09843cb2a88 Mon Sep 17 00:00:00 2001 From: zxysilent Date: Sun, 31 Mar 2024 14:14:27 +0800 Subject: [PATCH] rbd: trigger an error on invalid max snaps value In file include from rbd.go:931 var cMaxSnaps C.int ret := C.rbd_snap_list(image.image, nil, &cMaxSnaps) cSnaps := make([]C.rbd_snap_info_t, cMaxSnaps) It is necessary to determine whether cMaxSnaps is a large 0. Otherwise, the following code will panic ret = C.rbd_snap_list(image.image,&cSnaps[0], &cMaxSnaps) Signed-off-by: zxysilent --- rbd/rbd.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rbd/rbd.go b/rbd/rbd.go index b05b143..60582fa 100644 --- a/rbd/rbd.go +++ b/rbd/rbd.go @@ -936,7 +936,10 @@ func (image *Image) GetSnapshotNames() (snaps []SnapInfo, err error) { var cMaxSnaps C.int ret := C.rbd_snap_list(image.image, nil, &cMaxSnaps) - + // bugfix index out of range(&cSnaps[0]) + if cMaxSnaps < 1 { + return nil, rbdError(ret) + } cSnaps := make([]C.rbd_snap_info_t, cMaxSnaps) snaps = make([]SnapInfo, cMaxSnaps)