summaryrefslogtreecommitdiffstats
path: root/xmalloc.c
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2020-12-16 22:53:48 +0100
committerpgen <p.gen.progs@gmail.com>2020-12-16 23:45:06 +0100
commit6e2417e3b26160fd4902a0ecbadc99e7a347218a (patch)
tree2bb80bd519f49fefc9ff558cea912f3dfad50194 /xmalloc.c
parent4e6305f56d80e4f934d8a7dc92e6986be2928d3f (diff)
Check availability of str(n)dup at compile time
Diffstat (limited to 'xmalloc.c')
-rw-r--r--xmalloc.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/xmalloc.c b/xmalloc.c
index 9f6caa1..0eeab68 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -16,6 +16,7 @@
#include <string.h>
#include <errno.h>
+#include "config.h"
#include "xmalloc.h"
/* ================== */
@@ -93,8 +94,19 @@ xstrdup(const char * p)
{
char * allocated;
+#ifdef HAVE_STRDUP
+ allocated = strdup(p);
+
+ if (allocated == NULL)
+ {
+ fprintf(stderr, "Error: Insufficient memory for strdup.\n");
+
+ exit(EXIT_FAILURE);
+ }
+#else
allocated = xmalloc(strlen(p) + 1);
strcpy(allocated, p);
+#endif
return allocated;
}
@@ -108,6 +120,16 @@ xstrndup(const char * str, size_t len)
{
char * p;
+#ifdef HAVE_STRNDUP
+ p = strndup(str, len);
+
+ if (p == NULL)
+ {
+ fprintf(stderr, "Error: Insufficient memory for strndup.\n");
+
+ exit(EXIT_FAILURE);
+ }
+#else
p = memchr(str, '\0', len);
if (p)
@@ -116,6 +138,7 @@ xstrndup(const char * str, size_t len)
p = xmalloc(len + 1);
memcpy(p, str, len);
p[len] = '\0';
+#endif
return p;
}