diff options
-rw-r--r-- | doc/README.developer | 9 | ||||
-rw-r--r-- | epan/tvbuff.c | 26 | ||||
-rw-r--r-- | epan/tvbuff.h | 9 |
3 files changed, 44 insertions, 0 deletions
diff --git a/doc/README.developer b/doc/README.developer index d3a5258e9d..b1f1fcf61f 100644 --- a/doc/README.developer +++ b/doc/README.developer @@ -906,6 +906,7 @@ dissected. guint8 *tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); +guint8 *tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); Returns a null-terminated buffer, allocated with "g_malloc()", containing data from the specified tvbuff, starting with at the @@ -913,6 +914,14 @@ specified offset, and containing all characters from the tvbuff up to and including a terminating null character in the tvbuff. "*lengthp" will be set to the length of the string, including the terminating null. +tvb_get_stringz() returns a buffer allocated by g_malloc() so you must +g_free() it when you are finished with the string. Failure to g_free() this +buffer will lead to memory leaks. +tvb_get_ephemeral_stringz() returns a buffer allocated from a special heap +with a lifetime until the next packet is dissected. You do not need to +free() this buffer, it will happen automatically once the next packet is +dissected. + guint8 *tvb_fake_unicode(tvbuff_t*, gint offset, gint length); guint8 *tvb_get_ephemeral_faked_unicode(tvbuff_t*, gint offset, gint length); diff --git a/epan/tvbuff.c b/epan/tvbuff.c index dab173e9ae..f799e25c19 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -1839,6 +1839,32 @@ tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp) *lengthp = size; return strptr; } +/* + * Given a tvbuff and an offset, with the offset assumed to refer to + * a null-terminated string, find the length of that string (and throw + * an exception if the tvbuff ends before we find the null), allocate + * a buffer big enough to hold the string, copy the string into it, + * and return a pointer to the string. Also return the length of the + * string (including the terminating null) through a pointer. + * + * This function allocates memory from a buffer with packet lifetime. + * You do not have to free this buffer, it will be automatically freed + * when ethereal starts decoding the next packet. + * Do not use this function if you want the allocated memory to be persistent + * after the current packet has been dissected. + */ +guint8 * +tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp) +{ + guint size; + guint8 *strptr; + + size = tvb_strsize(tvb, offset); + strptr = ep_alloc(size); + tvb_memcpy(tvb, strptr, offset, size); + *lengthp = size; + return strptr; +} /* Looks for a stringz (NUL-terminated string) in tvbuff and copies * no more than bufsize number of bytes, including terminating NUL, to buffer. diff --git a/epan/tvbuff.h b/epan/tvbuff.h index d76f9b2395..29504ebd5f 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -444,8 +444,17 @@ extern guint8 *tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length) * a buffer big enough to hold the string, copy the string into it, * and return a pointer to the string. Also return the length of the * string (including the terminating null) through a pointer. + * + * tvb_get_stringz() returns a string allocated by g_malloc() and therefore + * MUST be g_free() by the caller in order not to leak + * memory. + * + * tvb_get_ephemeral_stringz() returns a string that does not need to be freed, + * instead it will automatically be freed once the next + * packet is dissected. */ extern guint8 *tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); +extern guint8 *tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); /** Looks for a stringz (NUL-terminated string) in tvbuff and copies * no more than bufsize number of bytes, including terminating NUL, to buffer. |