convert one sprintf into g_snprintf()
authorsahlberg <sahlberg@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 1 Sep 2005 09:03:29 +0000 (09:03 +0000)
committersahlberg <sahlberg@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 1 Sep 2005 09:03:29 +0000 (09:03 +0000)
at the same time, make proto_construct_dfilter_string() return an emem allocated string.
This fixes a tiny memleak in print.c that never freed the string returned by this function.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@15651 f5534014-38df-0310-8fa8-9805f1628bb7

epan/proto.c
gtk/main.c
tap-protocolinfo.c

index cba96405ada4be3074c4960bdad50b6977993a05..537decc9846193e26436f34f8a5eb5a885c707b2 100644 (file)
@@ -41,6 +41,7 @@
 #include "epan_dissect.h"
 #include "slab.h"
 #include "tvbuff.h"
+#include "emem.h"
 
 #define cVALS(x) (const value_string*)(x)
 
@@ -4370,6 +4371,10 @@ proto_can_match_selected(field_info *finfo, epan_dissect_t *edt)
        }
 }
 
+/* This function returns a string allocated with packet lifetime scope.
+ * You do not need to [g_]free() this string since it willb e automatically 
+ * freed once the next packet is dissected.
+ */
 char*
 proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
 {
@@ -4436,7 +4441,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                         * 1 byte for the trailing '\0'.
                         */
                        dfilter_len = abbrev_len + 4 + 11 + 1;
-                       buf = g_malloc0(dfilter_len);
+                       buf = ep_alloc0(dfilter_len);
                        format = hfinfo_numeric_format(hfinfo);
                        g_snprintf(buf, dfilter_len, format, hfinfo->abbrev, fvalue_get_integer(&finfo->value));
                        break;
@@ -4460,7 +4465,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                         * 1 byte for the trailing '\0'.
                         */
                        dfilter_len = abbrev_len + 4 + 22 + 1;
-                       buf = g_malloc0(dfilter_len);
+                       buf = ep_alloc0(dfilter_len);
                        format = hfinfo_numeric_format(hfinfo);
                        g_snprintf(buf, dfilter_len, format, hfinfo->abbrev, fvalue_get_integer64(&finfo->value));
                        break;
@@ -4473,7 +4478,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                         * 1 byte for the trailing '\0'.
                         */
                        dfilter_len = abbrev_len + 4 + 2 + 8 + 1;
-                       buf = g_malloc0(dfilter_len);
+                       buf = ep_alloc0(dfilter_len);
                        g_snprintf(buf, dfilter_len, "%s == 0x%08x", hfinfo->abbrev,
                                        fvalue_get_integer(&finfo->value));
                        break;
@@ -4486,7 +4491,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                         */
                        stringified = ip6_to_str((struct e_in6_addr*) fvalue_get(&finfo->value));
                        dfilter_len = abbrev_len + 4 + strlen(stringified) + 1;
-                       buf = g_malloc0(dfilter_len);
+                       buf = ep_alloc0(dfilter_len);
                        g_snprintf(buf, dfilter_len, "%s == %s", hfinfo->abbrev,
                                        stringified);
                        break;
@@ -4511,7 +4516,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                        dfilter_len = fvalue_string_repr_len(&finfo->value,
                                        FTREPR_DFILTER);
                        dfilter_len += abbrev_len + 4 + 1;
-                       buf = g_malloc0(dfilter_len);
+                       buf = ep_alloc0(dfilter_len);
 
                        /* Create the string */
                        g_snprintf(buf, dfilter_len, "%s == ", hfinfo->abbrev);
@@ -4521,7 +4526,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                        break;
 
                case FT_PROTOCOL:
-                       buf = g_strdup(finfo->hfinfo->abbrev);
+                       buf = ep_strdup(finfo->hfinfo->abbrev);
                        break;
 
                default:
@@ -4533,7 +4538,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                         * If "edt" is NULL, the answer is "no".
                         */
                        if (edt == NULL)
-                               return FALSE;
+                               return NULL;
 
                        /*
                         * Is this field part of the raw frame tvbuff?
@@ -4569,7 +4574,7 @@ proto_construct_dfilter_string(field_info *finfo, epan_dissect_t *edt)
                                return NULL;
                        
                        start = finfo->start;
-                       buf = g_malloc0(32 + length * 3);
+                       buf = ep_alloc0(32 + length * 3);
                        ptr = buf;
 
                        sprintf(ptr, "frame[%d:%d] == ", finfo->start, length);
index 23201d2d8bce109ff8eb70f38ea90088e7dc9948..e2aa215c96fb4e5e6200e348cf125ad389dbe1d4 100644 (file)
@@ -65,6 +65,7 @@
 #include <epan/dfilter/dfilter.h>
 #include <epan/strutil.h>
 #include <epan/addr_resolv.h>
+#include <epan/emem.h>
 
 /* general (not GTK specific) */
 #include "file.h"
@@ -266,9 +267,6 @@ match_selected_cb_do(gpointer data, int action, gchar *text)
 
     /* Free up the new filter text. */
     g_free(new_filter);
-
-    /* Free up the generated text we were handed. */
-    g_free(text);
 }
 
 void
@@ -404,7 +402,7 @@ get_text_from_packet_list(gpointer data)
            strlen(cfile.cinfo.col_expr_val[column]) != 0) {
            len = strlen(cfile.cinfo.col_expr[column]) +
                  strlen(cfile.cinfo.col_expr_val[column]) + 5;
-           buf = g_malloc0(len);
+           buf = ep_alloc0(len);
            g_snprintf(buf, len, "%s == %s", cfile.cinfo.col_expr[column],
                     cfile.cinfo.col_expr_val[column]);
        }
index 701d0ffc14308c7c5ec5b781c086b8b3c3bc5911..909ac8c240d6c9d9a3f6a73fc91487e9eb3acb93 100644 (file)
@@ -79,7 +79,6 @@ protocolinfo_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, const vo
                str=proto_construct_dfilter_string(gp->pdata[i], NULL);
                if(str){
                        col_append_fstr(pinfo->cinfo, COL_INFO, "  %s",str);
-                       g_free(str);
                }
        }
        return 0;