diff options
author | Guy Harris <guy@alum.mit.edu> | 2003-06-12 08:33:32 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2003-06-12 08:33:32 +0000 |
commit | ee97ce31966f61de148ad85cb229e76a88801b02 (patch) | |
tree | 22f7363da150c57eb593a2e5871033e8b8585437 /epan/proto.c | |
parent | 04a87185285865ae91f903662c4bc721f66c8d88 (diff) |
Add new routines:
tvb_get_string() - takes a tvbuff, an offset, and a length as
arguments, allocates a buffer big enough to hold a string with
the specified number of bytes plus an added null terminator
(i.e., length+1), copies the specified number of bytes from the
tvbuff, at the specified offset, to that buffer and puts in a
null terminator, and returns a pointer to that buffer (or throws
an exception before allocating the buffer if that many bytes
aren't available in the tvbuff);
tvb_get_stringz() - takes a tvbuff, an offset, and a pointer to
a "gint" as arguments, gets the size of the null-terminated
string starting at the specified offset in the tvbuff (throwing
an exception if the null terminator isn't found), allocates a
buffer big enough to hold that string, copies the string to that
buffer, and returns a pointer to that buffer and stores the
length of the string (including the terminating null) in the
variable pointed to by the "gint" pointer.
Replace many pieces of code allocating a buffer and copying a string
with calls to "tvb_get_string()" (for one thing, "tvb_get_string()"
doesn't require you to remember that the argument to
"tvb_get_nstringz0()" is the size of the buffer into which you're
copying the string, which might be the length of the string to be copied
*plus 1*).
Don't use fixed-length buffers for null-terminated strings (even if the
code that generates those packets has a #define to limit the length of
the string). Use "tvb_get_stringz()", instead.
In some cases where a value is fetched but is only used to pass an
argument to a "proto_tree_add_XXX" routine, use "proto_tree_add_item()"
instead.
svn path=/trunk/; revision=7859
Diffstat (limited to 'epan/proto.c')
-rw-r--r-- | epan/proto.c | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/epan/proto.c b/epan/proto.c index c04cc1d028..97050528a5 100644 --- a/epan/proto.c +++ b/epan/proto.c @@ -1,7 +1,7 @@ /* proto.c * Routines for protocol tree * - * $Id: proto.c,v 1.92 2003/06/11 21:48:39 guy Exp $ + * $Id: proto.c,v 1.93 2003/06/12 08:33:31 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@ethereal.com> @@ -711,7 +711,8 @@ proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb, /* This can throw an exception */ length = tvb_strsize(tvb, start); - /* This g_strdup'ed memory is freed in proto_tree_free_node() */ + /* This g_malloc'ed memory is freed + in proto_tree_free_node() */ string = g_malloc(length); tvb_memcpy(tvb, string, start, length); @@ -750,16 +751,10 @@ proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb, * rather than null-terminated.) */ - /* This g_strdup'ed memory is freed + /* This g_malloc'ed memory is freed * in proto_tree_free_node() */ - string = g_malloc(length + 1); - - CLEANUP_PUSH(g_free, string); - - tvb_memcpy(tvb, string, start, length); - string[length] = '\0'; - - CLEANUP_POP; + string = tvb_get_string(tvb, start, + length); new_fi->length = length; } proto_tree_set_string(new_fi, string, TRUE); |