From ad4efab83364b335d6c4061d55e8a4a1f38e4772 Mon Sep 17 00:00:00 2001 From: Francisco Javier Honduvilla Coto Date: Wed, 3 Aug 2022 11:16:26 +0100 Subject: [PATCH] batch: Fix DeleteKeyBatch when dealing with partial deletion, more context on https://github.com/aquasecurity/libbpfgo/issues/162 Signed-off-by: Francisco Javier Honduvilla Coto --- selftest/map-batch/main.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/selftest/map-batch/main.go b/selftest/map-batch/main.go index d3abade6..e764b605 100644 --- a/selftest/map-batch/main.go +++ b/selftest/map-batch/main.go @@ -103,7 +103,12 @@ func main() { } // Test batch delete. - testerMap.DeleteKeyBatch(unsafe.Pointer(&keys[0]), uint32(len(keys)-1)) + // Trying to delete more keys than we have. + err = testerMap.DeleteKeyBatch(unsafe.Pointer(&keys[0]), uint32(len(keys)+100)) + if err != nil { + fmt.Fprintf(os.Stderr, "testerMap.DeleteKeyBatch was expected to not fail") + os.Exit(-1) + } // Ensure value is no longer there. _, err = testerMap.GetValue(unsafe.Pointer(&keys[0])) @@ -112,6 +117,20 @@ func main() { os.Exit(-1) } + // Re-add deleted entries. + if err := testerMap.UpdateBatch(unsafe.Pointer(&keys[0]), unsafe.Pointer(&values[0]), uint32(len(keys))); err != nil { + fmt.Fprintf(os.Stderr, "testerMap.UpdateBatch failed: %v", err) + os.Exit(-1) + } + + // Test batch delete. + // Trying to delete fewer or equal keys than we have. + err = testerMap.DeleteKeyBatch(unsafe.Pointer(&keys[0]), uint32(len(keys)-1)) + if err != nil { + fmt.Fprintf(os.Stderr, "testerMap.DeleteKeyBatch was expected to not fail") + os.Exit(-1) + } + // Test batch lookup and delete when requesting more elements than are in the map. deleteKeys = make([]uint32, 3) nextKey = uint32(0)