]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add isc_mem_strndup() function
authorWitold Kręcicki <wpk@isc.org>
Mon, 26 Oct 2020 13:17:05 +0000 (14:17 +0100)
committerArtem Boldariev <artem@boldariev.com>
Tue, 2 Feb 2021 17:53:34 +0000 (19:53 +0200)
This commit adds an implementation of strndup() function which
allocates memory from the supplied isc_mem_t memory context.

lib/isc/include/isc/mem.h
lib/isc/mem.c

index 97b249cc6539b6447ece9566d40e03a6d8e2f08b..2aee5fb00c9fb9d153e5891164a157dfe794a699 100644 (file)
@@ -172,6 +172,8 @@ typedef struct isc_memmethods {
        void *(*memreallocate)(isc_mem_t *mctx, void *ptr,
                               size_t size _ISC_MEM_FLARG);
        char *(*memstrdup)(isc_mem_t *mctx, const char *s _ISC_MEM_FLARG);
+       char *(*memstrndup)(isc_mem_t *mctx, const char *s,
+                           size_t size _ISC_MEM_FLARG);
        void (*memfree)(isc_mem_t *mctx, void *ptr _ISC_MEM_FLARG);
 } isc_memmethods_t;
 
@@ -226,7 +228,9 @@ struct isc_mempool {
 #define isc_mem_reallocate(c, p, s) \
        ISCMEMFUNC(reallocate)((c), (p), (s)_ISC_MEM_FILELINE)
 #define isc_mem_strdup(c, p) ISCMEMFUNC(strdup)((c), (p)_ISC_MEM_FILELINE)
-#define isc_mempool_get(c)   ISCMEMPOOLFUNC(get)((c)_ISC_MEM_FILELINE)
+#define isc_mem_strndup(c, p, l) \
+       ISCMEMFUNC(strndup)((c), (p), (l)_ISC_MEM_FILELINE)
+#define isc_mempool_get(c) ISCMEMPOOLFUNC(get)((c)_ISC_MEM_FILELINE)
 
 #define isc_mem_put(c, p, s)                                     \
        do {                                                     \
@@ -596,6 +600,7 @@ void *ISCMEMFUNC(allocate)(isc_mem_t *, size_t _ISC_MEM_FLARG);
 void *ISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
 void  ISCMEMFUNC(free)(isc_mem_t *, void *_ISC_MEM_FLARG);
 char *ISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG);
+char *ISCMEMFUNC(strndup)(isc_mem_t *, const char *, size_t _ISC_MEM_FLARG);
 void *ISCMEMPOOLFUNC(get)(isc_mempool_t *_ISC_MEM_FLARG);
 void  ISCMEMPOOLFUNC(put)(isc_mempool_t *, void *_ISC_MEM_FLARG);
 
index af3d57f6f8d1282400b3af5cbfba67df01e7815e..879b6232f9c4edc260b31ed33d6927c6c66a79b4 100644 (file)
@@ -244,13 +244,15 @@ static void *
 isc___mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG);
 static char *
 isc___mem_strdup(isc_mem_t *mctx, const char *s FLARG);
+static char *
+isc___mem_strndup(isc_mem_t *mctx, const char *s, size_t size FLARG);
 static void
 isc___mem_free(isc_mem_t *ctx, void *ptr FLARG);
 
 static isc_memmethods_t memmethods = {
        isc___mem_get,      isc___mem_put,        isc___mem_putanddetach,
        isc___mem_allocate, isc___mem_reallocate, isc___mem_strdup,
-       isc___mem_free,
+       isc___mem_strndup,  isc___mem_free,
 };
 
 #if ISC_MEM_TRACKLINES
@@ -1439,6 +1441,29 @@ isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
        return (ns);
 }
 
+char *
+isc___mem_strndup(isc_mem_t *mctx0, const char *s, size_t size FLARG) {
+       REQUIRE(VALID_CONTEXT(mctx0));
+       REQUIRE(s != NULL);
+
+       isc__mem_t *mctx = (isc__mem_t *)mctx0;
+       size_t len;
+       char *ns;
+
+       len = strlen(s) + 1;
+       if (len > size) {
+               len = size;
+       }
+
+       ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
+
+       if (ns != NULL) {
+               strlcpy(ns, s, len);
+       }
+
+       return (ns);
+}
+
 void
 isc_mem_setdestroycheck(isc_mem_t *ctx0, bool flag) {
        REQUIRE(VALID_CONTEXT(ctx0));
@@ -2467,6 +2492,13 @@ isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
        return (mctx->methods->memstrdup(mctx, s FLARG_PASS));
 }
 
+char *
+isc__mem_strndup(isc_mem_t *mctx, const char *s, size_t size FLARG) {
+       REQUIRE(ISCAPI_MCTX_VALID(mctx));
+
+       return (mctx->methods->memstrndup(mctx, s, size FLARG_PASS));
+}
+
 void
 isc__mem_free(isc_mem_t *mctx, void *ptr FLARG) {
        REQUIRE(ISCAPI_MCTX_VALID(mctx));