Index | Thread | Search

From:
Kenjiro Nakayama <nakayamakenjiro@gmail.com>
Subject:
[PATCH] libressl: Make lhash load test verify exact number of deleted entries
To:
tech@openbsd.org
Cc:
nakayamakenjiro@gmail.com
Date:
Mon, 21 Apr 2025 15:41:06 +0900

Download raw body.

Thread
Track successful deletions using a context struct, and compare the
remaining item count against the expected value. Replace magic numbers
with named constants for clarity.
---
 src/regress/lib/libcrypto/lhash/lhash_test.c | 39 +++++++++++++++-----
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/src/regress/lib/libcrypto/lhash/lhash_test.c b/src/regress/lib/libcrypto/lhash/lhash_test.c
index d6dcb33..f71045c 100644
--- a/src/regress/lib/libcrypto/lhash/lhash_test.c
+++ b/src/regress/lib/libcrypto/lhash/lhash_test.c
@@ -27,6 +27,15 @@
  *  - custom comparison function
  */
 
+#define LOAD_BATCHES     1024
+#define LOAD_PER_BATCH   1024
+#define LOAD_COUNT       (LOAD_BATCHES * LOAD_PER_BATCH)
+
+struct doall_delete_ctx {
+	_LHASH *lh;
+	int *deleted_count;
+};
+
 static void
 test_doall_count(void *arg1, void *arg2)
 {
@@ -226,12 +235,16 @@ static void
 test_doall_delete_some(void *arg1, void *arg2)
 {
 	void *data;
+	struct doall_delete_ctx *ctx = arg2;
 
 	if (arc4random_uniform(32) != 0)
 		return;
 
-	data = lh_delete(arg2, arg1);
-	free(data);
+	data = lh_delete(ctx->lh, arg1);
+	if (data != NULL) {
+		free(data);
+		(*ctx->deleted_count)++;
+	}
 }
 
 static void
@@ -251,12 +264,13 @@ test_lhash_load(void)
 	char *data = NULL;
 	int i, j;
 	int failed = 1;
+	long remaining, expected, deleted_count = 0;
 
 	if ((lh = lh_new(NULL, NULL)) == NULL)
 		goto failure;
 
-	for (i = 0; i < 1024; i++) {
-		for (j = 0; j < 1024; j++) {
+	for (i = 0; i < LOAD_BATCHES; i++) {
+		for (j = 0; j < LOAD_PER_BATCH; j++) {
 			if ((data = calloc(1, 128)) == NULL)
 				goto failure;
 
@@ -284,16 +298,23 @@ test_lhash_load(void)
 			}
 			data = NULL;
 		}
-		lh_doall_arg(lh, test_doall_delete_some, lh);
+
+		struct doall_delete_ctx ctx = { lh, &deleted_count };
+		lh_doall_arg(lh, test_doall_delete_some, &ctx);
 	}
 
-	/* We should have ~31,713 entries. */
-	if (lh_num_items(lh) < 31000 || lh_num_items(lh) > 33000) {
-		fprintf(stderr, "FAIL: unexpected number of entries (%ld)\n",
-		    lh_num_items(lh));
+	remaining = lh_num_items(lh);
+	expected = LOAD_COUNT - deleted_count;
+
+	if (remaining != expected) {
+		fprintf(stderr, "FAIL: item count mismatch: remaining = %ld, expected = %ld\n",
+		    remaining, expected);
 		goto failure;
 	}
 
+	fprintf(stdout, "PASS: inserted = %d, deleted = %ld, remaining = %ld\n",
+	    LOAD_COUNT, deleted_count, remaining);
+
 	failed = 0;
 
  failure:
-- 
2.39.5 (Apple Git-154)