]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11621: add switch_core_strndup
authorMariah Yang <mariah@signalwire.com>
Mon, 21 Jan 2019 16:46:47 +0000 (00:46 +0800)
committerAndrey Volk <andywolk@gmail.com>
Wed, 17 Jul 2019 15:44:17 +0000 (19:44 +0400)
src/include/switch_core.h
src/switch_core_memory.c

index eb89a0682a3ce01c59a88fab19f6daf3500b0781..f8c65593547cd99b103d9bfff053d4845f61e508 100644 (file)
@@ -724,6 +724,8 @@ SWITCH_DECLARE(char *) switch_core_perform_session_strdup(_In_ switch_core_sessi
 
 SWITCH_DECLARE(char *) switch_core_perform_strdup(_In_ switch_memory_pool_t *pool, _In_z_ const char *todup, _In_z_ const char *file,
                                                                                                  _In_z_ const char *func, _In_ int line);
+SWITCH_DECLARE(char *) switch_core_perform_strndup(_In_ switch_memory_pool_t *pool, _In_z_ const char *todup, size_t len, _In_z_ const char *file,
+                                                                                                 _In_z_ const char *func, _In_ int line);
 
 /*!
   \brief Copy a string using memory allocation from a given pool
@@ -733,6 +735,8 @@ SWITCH_DECLARE(char *) switch_core_perform_strdup(_In_ switch_memory_pool_t *poo
 */
 #define switch_core_strdup(_pool, _todup)  switch_core_perform_strdup(_pool, _todup, __FILE__, __SWITCH_FUNC__, __LINE__)
 
+#define switch_core_strndup(_pool, _todup, _len)  switch_core_perform_strndup(_pool, _todup, _len, __FILE__, __SWITCH_FUNC__, __LINE__)
+
 /*!
   \brief printf-style style printing routine.  The data is output to a string allocated from the session
   \param session a session to use for allocation
index 9e4bc12964a17bfe93472d828de0fecd5b08cf7b..288e96d85bf6e438fcf4ce34409e16f52213d67d 100644 (file)
@@ -269,9 +269,13 @@ SWITCH_DECLARE(char *) switch_core_perform_session_strdup(switch_core_session_t
 }
 
 SWITCH_DECLARE(char *) switch_core_perform_strdup(switch_memory_pool_t *pool, const char *todup, const char *file, const char *func, int line)
+{
+       return switch_core_perform_strndup(pool, todup, todup ? strlen(todup) + 1 : 0, file, func, line);
+}
+
+SWITCH_DECLARE(char *) switch_core_perform_strndup(switch_memory_pool_t *pool, const char *todup, size_t len, const char *file, const char *func, int line)
 {
        char *duped = NULL;
-       switch_size_t len;
        switch_assert(pool != NULL);
 
        if (!todup) {
@@ -287,8 +291,6 @@ SWITCH_DECLARE(char *) switch_core_perform_strdup(switch_memory_pool_t *pool, co
 #endif
 #endif
 
-       len = strlen(todup) + 1;
-
 #ifdef DEBUG_ALLOC
        if (len > DEBUG_ALLOC_CUTOFF)
                switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "%p Core Strdup Allocate %s %d\n",