diff mbox series

[4/4] reftable: avoid non portable compile time pointer to function

Message ID 20210930054032.16867-5-carenas@gmail.com (mailing list archive)
State New, archived
Headers show
Series [1/4] fixup! reftable: add a heap-based priority queue for reftable records | expand

Commit Message

Carlo Marcelo Arenas Belón Sept. 30, 2021, 5:40 a.m. UTC
While GNU compilers (and even MSVC) allow as an extension setting
at compile time the address of a dynamic function into a static
variables, it trigger C4232[1] with a warning for portability.

Avoid it by resolving at runtime the address that will be used
on each case, and not relying on the pointer initialization
to be honour at compile time.

[1] https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4232

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 reftable/publicbasics.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/reftable/publicbasics.c b/reftable/publicbasics.c
index bd0a02d3f6..b5767bccc5 100644
--- a/reftable/publicbasics.c
+++ b/reftable/publicbasics.c
@@ -11,23 +11,32 @@  license that can be found in the LICENSE file or at
 #include "basics.h"
 #include "system.h"
 
-static void *(*reftable_malloc_ptr)(size_t sz) = &malloc;
-static void *(*reftable_realloc_ptr)(void *, size_t) = &realloc;
-static void (*reftable_free_ptr)(void *) = &free;
+static void *(*reftable_malloc_ptr)(size_t sz);
+static void *(*reftable_realloc_ptr)(void *, size_t);
+static void (*reftable_free_ptr)(void *);
 
 void *reftable_malloc(size_t sz)
 {
-	return (*reftable_malloc_ptr)(sz);
+	if (reftable_malloc_ptr)
+		return reftable_malloc_ptr(sz);
+	else
+		return malloc(sz);
 }
 
 void *reftable_realloc(void *p, size_t sz)
 {
-	return (*reftable_realloc_ptr)(p, sz);
+	if (reftable_realloc_ptr)
+		return reftable_realloc_ptr(p, sz);
+	else
+		return realloc(p, sz);
 }
 
 void reftable_free(void *p)
 {
-	reftable_free_ptr(p);
+	if (reftable_free_ptr)
+		reftable_free_ptr(p);
+	else
+		free(p);
 }
 
 void *reftable_calloc(size_t sz)