From 3ed71a09f4dccfd18511f42df60891ea4f19efde Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sun, 22 Oct 2023 02:25:25 +1100 Subject: [PATCH] [common] all: implement `strdup` directly Dr.Memory on Windows complains bitterly about invalid heap free as it doesn't seem to be able to track this function's allocations. As it's such a trivial function we can just implement it locally. --- common/include/common/stringutils.h | 3 +++ common/src/stringutils.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/common/include/common/stringutils.h b/common/include/common/stringutils.h index f16ff50d..b9aa0af8 100644 --- a/common/include/common/stringutils.h +++ b/common/include/common/stringutils.h @@ -34,4 +34,7 @@ int alloc_sprintf(char ** str, const char * format, ...) // Find value in a list separated by delimiter. bool str_containsValue(const char * list, char delimiter, const char * value); +// Local implementation of strdup +char * strdup(const char *s); + #endif diff --git a/common/src/stringutils.c b/common/src/stringutils.c index a724e0c4..b66a91ec 100644 --- a/common/src/stringutils.c +++ b/common/src/stringutils.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "common/stringutils.h" @@ -89,3 +90,23 @@ bool str_containsValue(const char * list, char delimiter, const char * value) } return false; } + +char * strdup(const char *s) +{ + if (!s) + { + errno = EINVAL; + return NULL; + } + + const ssize_t len = strlen(s) + 1; + char * out = malloc(len); + if (!out) + { + errno = ENOMEM; + return NULL; + } + + memcpy(out, s, len); + return out; +}