From 739da84c7de34418513b72d6cba26a9bc4944d4d Mon Sep 17 00:00:00 2001 From: Troy Karan Harrison Date: Mon, 7 Aug 2023 11:53:21 +0200 Subject: [PATCH] [DCT-4]: Swap to `ddsrt_malloc_s` in hopscotch --- src/ddsrt/src/hopscotch.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ddsrt/src/hopscotch.c b/src/ddsrt/src/hopscotch.c index b2010816e3..feaab42ba3 100644 --- a/src/ddsrt/src/hopscotch.c +++ b/src/ddsrt/src/hopscotch.c @@ -313,7 +313,7 @@ static bool ddsrt_chh_data_valid_p (void *data) return data != NULL && data != CHH_BUSY; } -static int ddsrt_chh_init (struct ddsrt_chh *rt, uint32_t init_size, ddsrt_hh_hash_fn hash, ddsrt_hh_equals_fn equals, ddsrt_hh_buckets_gc_fn gc_buckets, void *gc_buckets_arg) +static dds_return_t ddsrt_chh_init (struct ddsrt_chh *rt, uint32_t init_size, ddsrt_hh_hash_fn hash, ddsrt_hh_equals_fn equals, ddsrt_hh_buckets_gc_fn gc_buckets, void *gc_buckets_arg) { uint32_t size; uint32_t i; @@ -328,7 +328,10 @@ static int ddsrt_chh_init (struct ddsrt_chh *rt, uint32_t init_size, ddsrt_hh_ha rt->gc_buckets = gc_buckets; rt->gc_buckets_arg = gc_buckets_arg; - buckets = ddsrt_malloc (offsetof (struct ddsrt_chh_bucket_array, bs) + size * sizeof (*buckets->bs)); + buckets = ddsrt_malloc_s (offsetof (struct ddsrt_chh_bucket_array, bs) + size * sizeof (*buckets->bs)); + if (buckets == NULL) { + return DDS_RETCODE_OUT_OF_RESOURCES; + } ddsrt_atomic_stvoidp (&rt->buckets, buckets); buckets->size = size; for (i = 0; i < size; i++) { @@ -338,7 +341,7 @@ static int ddsrt_chh_init (struct ddsrt_chh *rt, uint32_t init_size, ddsrt_hh_ha ddsrt_atomic_stvoidp (&b->data, NULL); } ddsrt_mutex_init (&rt->change_lock); - return 0; + return DDS_RETCODE_OK; } static void ddsrt_chh_fini (struct ddsrt_chh *rt) @@ -349,8 +352,8 @@ static void ddsrt_chh_fini (struct ddsrt_chh *rt) struct ddsrt_chh *ddsrt_chh_new (uint32_t init_size, ddsrt_hh_hash_fn hash, ddsrt_hh_equals_fn equals, ddsrt_hh_buckets_gc_fn gc_buckets, void *gc_buckets_arg) { - struct ddsrt_chh *hh = ddsrt_malloc (sizeof (*hh)); - if (ddsrt_chh_init (hh, init_size, hash, equals, gc_buckets, gc_buckets_arg) < 0) { + struct ddsrt_chh *hh = ddsrt_malloc_s (sizeof (*hh)); + if (hh == NULL || ddsrt_chh_init (hh, init_size, hash, equals, gc_buckets, gc_buckets_arg) < 0) { ddsrt_free (hh); return NULL; } else {