From: Witold Kręcicki Date: Mon, 26 Oct 2020 13:17:05 +0000 (+0100) Subject: Add isc_mem_strndup() function X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba72d6f01568214fab18af290d5e5712896dc2da;p=thirdparty%2Fbind9.git Add isc_mem_strndup() function This commit adds an implementation of strndup() function which allocates memory from the supplied isc_mem_t memory context. --- diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 97b249cc653..2aee5fb00c9 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -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); diff --git a/lib/isc/mem.c b/lib/isc/mem.c index af3d57f6f8d..879b6232f9c 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -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));