diff options
author | Craig Jackson <cejackson51@gmail.com> | 2018-02-12 21:47:44 -0500 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2018-02-13 06:18:09 +0000 |
commit | d1ce1baf63c96f9bddfe8577a82568b21295fd96 (patch) | |
tree | e6704eab955cbc075c04e12104999778d2d333fb | |
parent | 2cf6517ec088cadf60dec1f1c93b8575e4a4f40a (diff) |
WMEM: Add strjoin routines.
Add wmem versions corresponding to g_strjoin() and g_strjoinv().
Modify packet-rtps.c to use wmem routines_ where it is now using g_ routines causing mallocs.
Change-Id: I92c890a8b8f29a973e103676d8e5a681ee5abd50
Reviewed-on: https://code.wireshark.org/review/25764
Reviewed-by: Anders Broman <a.broman58@gmail.com>
-rw-r--r-- | epan/dissectors/packet-rtps.c | 20 | ||||
-rw-r--r-- | epan/wmem/wmem_strutl.c | 81 | ||||
-rw-r--r-- | epan/wmem/wmem_strutl.h | 12 |
3 files changed, 99 insertions, 14 deletions
diff --git a/epan/dissectors/packet-rtps.c b/epan/dissectors/packet-rtps.c index 9a5a4c76b0..ed434ce4e8 100644 --- a/epan/dissectors/packet-rtps.c +++ b/epan/dissectors/packet-rtps.c @@ -3963,12 +3963,10 @@ static void rtps_util_format_typename(gchar * type_name, gchar ** output) { gchar * result_caps; /* The standard specifies that the max size of a type name can be 255 bytes */ - tokens = g_strsplit(type_name, "::", 255); - result_caps = g_strjoinv("_", tokens); - *output = g_ascii_strdown(result_caps, -1); + tokens = wmem_strsplit(wmem_packet_scope(), type_name, "::", 255); + result_caps = wmem_strjoinv(wmem_packet_scope(), "_", tokens); + *output = wmem_ascii_strdown(wmem_packet_scope(), result_caps, -1); - g_strfreev(tokens); - g_free(result_caps); } static void rtps_util_topic_info_add_tree(proto_tree *tree, tvbuff_t *tvb, @@ -4015,18 +4013,12 @@ static gboolean rtps_util_topic_info_add_column_info_and_try_dissector(proto_tre } /* This part tries to dissect the content using a dissector */ next_tvb = tvb_new_subset_remaining(tvb, offset); - /* After calling this API, we must call g_free in dissector_name */ + rtps_util_format_typename(type_mapping_object->type_name, &dissector_name); - if (dissector_try_string(rtps_type_name_table, dissector_name, - next_tvb, pinfo, tree, data)) { - g_free(dissector_name); - return TRUE; - } else { - g_free(dissector_name); - return FALSE; + return dissector_try_string(rtps_type_name_table, dissector_name, + next_tvb, pinfo, tree, data); } } - } /* Return false so the content is dissected by the codepath following this one */ return FALSE; } diff --git a/epan/wmem/wmem_strutl.c b/epan/wmem/wmem_strutl.c index 068271c2db..55c76c1b56 100644 --- a/epan/wmem/wmem_strutl.c +++ b/epan/wmem/wmem_strutl.c @@ -168,6 +168,87 @@ wmem_strconcat(wmem_allocator_t *allocator, const gchar *first, ...) return concat; } +gchar * +wmem_strjoin(wmem_allocator_t *allocator, + const gchar *separator, const gchar *first, ...) +{ + gsize len; + va_list args; + gsize separator_len; + gchar *s; + gchar *concat; + gchar *ptr; + + if (!first) + return NULL; + + if (separator == NULL) { + separator = ""; + } + + separator_len = strlen (separator); + + len = 1 + strlen(first); /* + 1 for null byte */ + va_start(args, first); + while ((s = va_arg(args, gchar*))) { + len += (separator_len + strlen(s)); + } + va_end(args); + + ptr = concat = (gchar *)wmem_alloc(allocator, len); + ptr = g_stpcpy(ptr, first); + va_start(args, first); + while ((s = va_arg(args, gchar*))) { + ptr = g_stpcpy(ptr, separator); + ptr = g_stpcpy(ptr, s); + } + va_end(args); + + return concat; + +} + +gchar * +wmem_strjoinv(wmem_allocator_t *allocator, + const gchar *separator, gchar **str_array) +{ + gchar *string = NULL; + + if (!str_array) + return NULL; + + if (separator == NULL) { + separator = ""; + } + + if (str_array[0]) { + gint i; + gchar *ptr; + gsize len, separator_len; + + separator_len = strlen(separator); + + /* Get first part of length. Plus one for null byte. */ + len = 1 + strlen(str_array[0]); + /* Get the full length, including the separators. */ + for (i = 1; str_array[i] != NULL; i++) { + len += separator_len; + len += strlen(str_array[i]); + } + + /* Allocate and build the string. */ + string = (gchar *)wmem_alloc(allocator, len); + ptr = g_stpcpy(string, str_array[0]); + for (i = 1; str_array[i] != NULL; i++) { + ptr = g_stpcpy(ptr, separator); + ptr = g_stpcpy(ptr, str_array[i]); + } + } + + return string; + +} + gchar ** wmem_strsplit(wmem_allocator_t *allocator, const gchar *src, const gchar *delimiter, int max_tokens) diff --git a/epan/wmem/wmem_strutl.h b/epan/wmem/wmem_strutl.h index a028f94daa..b109907b2a 100644 --- a/epan/wmem/wmem_strutl.h +++ b/epan/wmem/wmem_strutl.h @@ -54,6 +54,18 @@ gchar * wmem_strconcat(wmem_allocator_t *allocator, const gchar *first, ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED; +WS_DLL_PUBLIC +gchar * +wmem_strjoin(wmem_allocator_t *allocator, + const gchar *separator, const gchar *first, ...) +G_GNUC_MALLOC G_GNUC_NULL_TERMINATED; + +WS_DLL_PUBLIC +gchar * +wmem_strjoinv(wmem_allocator_t *allocator, + const gchar *separator, gchar **str_array) +G_GNUC_MALLOC; + /** * Splits a string into a maximum of max_tokens pieces, using the given * delimiter. If max_tokens is reached, the remainder of string is appended |