diff --git a/qdf/inc/qdf_str.h b/qdf/inc/qdf_str.h index 89c765ec43..59861b35ed 100644 --- a/qdf/inc/qdf_str.h +++ b/qdf/inc/qdf_str.h @@ -40,6 +40,17 @@ static inline bool qdf_is_space(char c) return __qdf_is_space(c); } +/** + * qdf_str_dup() - duplicate null-terminated string @src + * @dest: double pointer to be populated + * @src: the null-terminated string to be duplicated + * + * @dest must be freed using qdf_mem_free() to avoid memory leaks. + * + * Return: QDF_STATUS; @dest set to NULL on failure, a valid address on success + */ +QDF_STATUS qdf_str_dup(char **dest, const char *src); + /** * qdf_str_left_trim() - Trim any leading whitespace from @str * @str: the string to trim diff --git a/qdf/src/qdf_str.c b/qdf/src/qdf_str.c index 7c0eabcf3a..f610397d6b 100644 --- a/qdf/src/qdf_str.c +++ b/qdf/src/qdf_str.c @@ -19,6 +19,31 @@ #include "qdf_mem.h" #include "qdf_module.h" #include "qdf_str.h" +#include "qdf_trace.h" + +QDF_STATUS qdf_str_dup(char **dest, const char *src) +{ + qdf_size_t size; + char *dup; + + *dest = NULL; + + QDF_BUG(src); + if (!src) + return QDF_STATUS_E_INVAL; + + /* size = length + null-terminator */ + size = qdf_str_len(src) + 1; + dup = qdf_mem_malloc(size); + if (!dup) + return QDF_STATUS_E_NOMEM; + + qdf_mem_copy(dup, src, size); + *dest = dup; + + return QDF_STATUS_SUCCESS; +} +qdf_export_symbol(qdf_str_dup); void qdf_str_right_trim(char *str) {