Wireshark-dev: Re: [Wireshark-dev] g_snprintf
From: Gerald Combs <[email protected]>
Date: Wed, 26 Aug 2009 12:51:33 -0700
wsgd wrote:
> soumya damodaran a écrit :
>> Hi
>>  
>>     Is it possible to display variable length data using g_snprintf? 
>> For the 802.11k AP channel report, the channel list is dependent on 
>> the tag_len which is variable.
>>  
>>
>> tag_data_ptr = tvb_get_ptr (tvb, offset, tag_len);
>>
> strcpy(print_buff, "");
>> for (i = 0, n = 0; i < tag_len; i++) {
> char    print_buff_idx[10];
>> ret = g_snprintf (print_buff_idx , SHORT_STR ,
>>
>> "%02X ", tag_data_ptr[i]);
> strcat(print_buff, print_buff_idx);
>> }
>>
>> proto_tree_add_string (tree, hf_tag_channel_list, tvb, offset, 
>> tag_len, print_buff);

The 802.11 dissector uses tvb_get_ptr in places it shouldn't. There
isn't a need for it here. Also, print_buff looks like a good candidate
for a buffer overflow. Is there any reason you wouldn't use a string
buffer for this?

emem_strbuf_t *tag_strbuf = ep_strbuf_new_label("<None>");

for (i = 0, i < tag_len, i++) {
    if (i == 0) {
        ep_strbuf_truncate(tag_strbuf, 0);
    }
    ep_strbuf_append_printf(tag_strbuf, "%s%02X",
                            tag_strbuf->len ? ", " : "",
                            tvb_get_guint8(tvb, offset+i);
}

proto_tree_add_string (tree, hf_tag_channel_list, tvb,
                       offset, tag_len, tag_strbuf->str);