t/stest: confirm that scalloc clears the buffer
authorVincent Fu <vincent.fu@samsung.com>
Tue, 11 Jun 2024 17:54:56 +0000 (17:54 +0000)
committerVincent Fu <vincent.fu@samsung.com>
Tue, 11 Jun 2024 18:04:19 +0000 (18:04 +0000)
Change smalloc calls to scalloc and make sure buffers are zeroed out.

Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
t/stest.c

index c6bf2d1efa8e86899c3a4e4f54b442d801d53533..396fa46e8f899ca9661fe5053617a062233402d2 100644 (file)
--- a/t/stest.c
+++ b/t/stest.c
@@ -25,10 +25,11 @@ static FLIST_HEAD(list);
 
 static int do_rand_allocs(void)
 {
-       unsigned int size, nr, rounds = 0, ret = 0;
+       unsigned int i, size, nr, rounds = 0, ret = 0;
        unsigned long total;
        struct elem *e;
        bool error;
+       char *c;
 
        while (rounds++ < LOOPS) {
 #ifdef STEST_SEED
@@ -38,12 +39,26 @@ static int do_rand_allocs(void)
                nr = total = 0;
                while (total < MAXSMALLOC) {
                        size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0)));
-                       e = smalloc(size);
+                       e = scalloc(1, size);
                        if (!e) {
                                printf("fail at %lu, size %u\n", total, size);
                                ret++;
                                break;
                        }
+
+                       c = (char *)e;
+                       for (i = 0; i < size; i++) {
+                               if (*(c+i) != 0) {
+                                       printf("buffer not cleared at %lu, size %u\n", total, size);
+                                       ret++;
+                                       break;
+                               }
+                       }
+
+                       /* stop the while loop if buffer was not cleared */
+                       if (i < size)
+                               break;
+
                        e->magic1 = MAGIC1;
                        e->magic2 = MAGIC2;
                        e->size = size;
@@ -63,15 +78,26 @@ static int do_rand_allocs(void)
                        sfree(e);
 
                        if (!error) {
-                               e = smalloc(LARGESMALLOC);
+                               e = scalloc(1, LARGESMALLOC);
                                if (!e) {
                                        error = true;
                                        ret++;
                                        printf("failure allocating %u bytes at %lu allocated during sfree phase\n",
                                                LARGESMALLOC, total);
+                                       break;
                                }
-                               else
-                                       sfree(e);
+
+                               c = (char *)e;
+                               for (i = 0; i < LARGESMALLOC; i++) {
+                                       if (*(c+i) != 0) {
+                                               error = true;
+                                               ret++;
+                                               printf("large buffer not cleared at %lu, size %u\n", total, size);
+                                               break;
+                                       }
+                               }
+
+                               sfree(e);
                        }
                }
        }