Wireshark-dev: Re: [Wireshark-dev] RFD: Limiting scope of ep_ memory

From: Jakub Zawadzki <darkjames-ws@xxxxxxxxxxxx>
Date: Sun, 14 Oct 2012 23:20:23 +0200
On Fri, Oct 12, 2012 at 02:02:53PM +0000, Anders Broman wrote:
> Why not make a patch that developers can try out and start debugging?

Attached.

PS. I'd rather want to hear comments, rather than debugging...
diff --git a/epan/epan.c b/epan/epan.c
index 6437bad..6880005 100644
--- a/epan/epan.c
+++ b/epan/epan.c
@@ -192,10 +192,26 @@ void
 epan_dissect_run(epan_dissect_t *edt, void* pseudo_header,
         const guint8* data, frame_data *fd, column_info *cinfo)
 {
-	/* free all memory allocated during previous packet */
 	ep_free_all();
 
 	dissect_packet(edt, pseudo_header, data, fd, cinfo);
+
+	/* free all memory allocated */
+	ep_free_all();
+}
+
+void
+epan_dissect_run_with_taps(epan_dissect_t *edt, void* pseudo_header,
+        const guint8* data, frame_data *fd, column_info *cinfo)
+{
+	ep_free_all();
+
+	tap_queue_init(edt);
+	dissect_packet(edt, pseudo_header, data, fd, cinfo);
+	tap_push_tapped_queue(edt);
+
+	/* free all memory allocated */
+	ep_free_all();
 }
 
 void
diff --git a/epan/epan.h b/epan/epan.h
index 00ec257..fa2a4e4 100644
--- a/epan/epan.h
+++ b/epan/epan.h
@@ -154,6 +154,11 @@ void
 epan_dissect_run(epan_dissect_t *edt, void* pseudo_header,
         const guint8* data, frame_data *fd, column_info *cinfo);
 
+void
+epan_dissect_run_with_taps(epan_dissect_t *edt, void* pseudo_header,
+        const guint8* data, frame_data *fd, column_info *cinfo);
+
+
 /** Prime a proto_tree using the fields/protocols used in a dfilter. */
 void
 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t *dfcode);
diff --git a/epan/frame_data.h b/epan/frame_data.h
index 87994ca..282364e 100644
--- a/epan/frame_data.h
+++ b/epan/frame_data.h
@@ -85,8 +85,7 @@ typedef struct {
  */
 typedef struct {
   tvbuff_t *tvb;
-  gboolean name_initialized;
-  const char *name;
+  char *name;
 } data_source;
 
 /* Utility routines used by packet*.c */
diff --git a/epan/libwireshark.def b/epan/libwireshark.def
index 3b82f64..0264f5e 100644
--- a/epan/libwireshark.def
+++ b/epan/libwireshark.def
@@ -411,6 +411,7 @@ epan_dissect_init
 epan_dissect_new
 epan_dissect_prime_dfilter
 epan_dissect_run
+epan_dissect_run_with_taps
 epan_get_compiled_version_info
 epan_get_runtime_version_info
 epan_get_version
@@ -1049,9 +1050,7 @@ t38_T30_data_vals               DATA
 t38_T30_indicator_vals          DATA
 t38_add_address
 T_h323_message_body_vals        DATA
-tap_push_tapped_queue
 tap_build_interesting
-tap_queue_init
 tap_queue_packet
 tcp_dissect_pdus
 tap_listeners_require_dissection
diff --git a/epan/packet.c b/epan/packet.c
index cd0f6f1..b7b176a 100644
--- a/epan/packet.c
+++ b/epan/packet.c
@@ -220,22 +220,16 @@ add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
 {
 	data_source *src;
 
-	src = ep_alloc(sizeof (data_source));
+	src = g_malloc(sizeof(data_source));
 	src->tvb = tvb;
-	src->name_initialized = FALSE;
-	src->name = name;
+	src->name = g_strdup(name);
 	pinfo->data_src = g_slist_append(pinfo->data_src, src);
 }
 
 const char*
 get_data_source_name(data_source *src)
 {
-	if (!src->name_initialized) {
-		src->name = ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
-		src->name_initialized = TRUE;
-	}
-
-	return src->name;
+	return ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
 }
 
 /*
@@ -245,6 +239,14 @@ void
 free_data_sources(packet_info *pinfo)
 {
 	if (pinfo->data_src) {
+		GSList *l;
+
+		for (l = pinfo->data_src; l; l = l->next) {
+			data_source *src = l->data;
+
+			g_free(src->name);
+			g_free(src);
+		}
 		g_slist_free(pinfo->data_src);
 		pinfo->data_src = NULL;
 	}
diff --git a/file.c b/file.c
index 46c4888..d1442c6 100644
--- a/file.c
+++ b/file.c
@@ -1123,9 +1123,7 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
       epan_dissect_prime_dfilter(&edt, dfcode);
   }
 
-  tap_queue_init(&edt);
-  epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
-  tap_push_tapped_queue(&edt);
+  epan_dissect_run_with_taps(&edt, pseudo_header, buf, fdata, cinfo);
 
   /* If we have a display filter, apply it if we're refiltering, otherwise
      leave the "passed_dfilter" flag alone.
@@ -2297,9 +2295,7 @@ retap_packet(capture_file *cf _U_, frame_data *fdata,
   epan_dissect_t         edt;
 
   epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
-  tap_queue_init(&edt);
-  epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
-  tap_push_tapped_queue(&edt);
+  epan_dissect_run_with_taps(&edt, pseudo_header, pd, fdata, args->cinfo);
   epan_dissect_cleanup(&edt);
 
   return TRUE;
diff --git a/rawshark.c b/rawshark.c
index d6f319a..d59da3c 100644
--- a/rawshark.c
+++ b/rawshark.c
@@ -1060,8 +1060,6 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
         }
     }
 
-    tap_queue_init(&edt);
-
     printf("%lu", (unsigned long int) cf->count);
 
     frame_data_set_before_dissect(&fdata, &cf->elapsed_time,
@@ -1070,9 +1068,7 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
     /* We only need the columns if we're printing packet info but we're
      *not* verbose; in verbose mode, we print the protocol tree, not
      the protocol summary. */
-    epan_dissect_run(&edt, &pseudo_header, pd, &fdata, &cf->cinfo);
-
-    tap_push_tapped_queue(&edt);
+    epan_dissect_run_with_taps(&edt, &pseudo_header, pd, &fdata, &cf->cinfo);
 
     frame_data_set_after_dissect(&fdata, &cum_bytes);
     prev_dis_frame = fdata;
diff --git a/tshark.c b/tshark.c
index b3f75e9..208195f 100644
--- a/tshark.c
+++ b/tshark.c
@@ -2632,8 +2632,6 @@ process_packet_second_pass(capture_file *cf, frame_data *fdata,
 
     col_custom_prime_edt(&edt, &cf->cinfo);
 
-    tap_queue_init(&edt);
-
     /* We only need the columns if either
 
          1) some tap needs the columns
@@ -2647,9 +2645,7 @@ process_packet_second_pass(capture_file *cf, frame_data *fdata,
     else
       cinfo = NULL;
 
-    epan_dissect_run(&edt, pseudo_header, pd, fdata, cinfo);
-
-    tap_push_tapped_queue(&edt);
+    epan_dissect_run_with_taps(&edt, pseudo_header, pd, fdata, cinfo);
 
     /* Run the read filter if we have one. */
     if (cf->rfcode)
@@ -3087,8 +3083,6 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
 
     col_custom_prime_edt(&edt, &cf->cinfo);
 
-    tap_queue_init(&edt);
-
     /* We only need the columns if either
 
          1) some tap needs the columns
@@ -3105,9 +3099,7 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
     frame_data_set_before_dissect(&fdata, &cf->elapsed_time,
                                   &first_ts, prev_dis, prev_cap);
 
-    epan_dissect_run(&edt, pseudo_header, pd, &fdata, cinfo);
-
-    tap_push_tapped_queue(&edt);
+    epan_dissect_run_with_taps(&edt, pseudo_header, pd, &fdata, cinfo);
 
     /* Run the read filter if we have one. */
     if (cf->rfcode)
diff --git a/ui/gtk/rlc_lte_graph.c b/ui/gtk/rlc_lte_graph.c
index d9f8280..66bd348 100644
--- a/ui/gtk/rlc_lte_graph.c
+++ b/ui/gtk/rlc_lte_graph.c
@@ -888,9 +888,7 @@ static rlc_lte_tap_info *select_rlc_lte_session(capture_file *cf, struct segment
 
     epan_dissect_init(&edt, TRUE, FALSE);
     epan_dissect_prime_dfilter(&edt, sfcode);
-    tap_queue_init(&edt);
-    epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
-    tap_push_tapped_queue(&edt);
+    epan_dissect_run_with_taps(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
     epan_dissect_cleanup(&edt);
     remove_tap_listener(&th);
 
diff --git a/ui/gtk/tcp_graph.c b/ui/gtk/tcp_graph.c
index 3240701..b745e46 100644
--- a/ui/gtk/tcp_graph.c
+++ b/ui/gtk/tcp_graph.c
@@ -1935,9 +1935,7 @@ static struct tcpheader *select_tcpip_session (capture_file *cf, struct segment
 
 	epan_dissect_init(&edt, TRUE, FALSE);
 	epan_dissect_prime_dfilter(&edt, sfcode);
-	tap_queue_init(&edt);
-	epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
-	tap_push_tapped_queue(&edt);
+	epan_dissect_run_with_taps(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
 	epan_dissect_cleanup(&edt);
 	remove_tap_listener(&th);