Wireshark-dev: Re: [Wireshark-dev] [PATCH][Update] stats_tree.c fixup preventing segfault
From: Sebastien Tandel <[email protected]>
Date: Thu, 14 Dec 2006 03:35:32 +0100
Hi,


   reviewing the potential problems g_strsplit could cause in others
parts of wireshark. I noticed that my patch was not completely safe.
here is a patch which should be safe. (please also review)


Regards,
Sebastien Tandel
Index: epan/stats_tree.c
===================================================================
--- epan/stats_tree.c	(révision 20131)
+++ epan/stats_tree.c	(copie de travail)
@@ -526,16 +526,47 @@
 
 static range_pair_t* get_range(guint8* rngstr) {
 	gchar** split;
-	range_pair_t* rng = g_malloc(sizeof(range_pair_t));
+	range_pair_t* rng;
 	
-	split =  g_strsplit(rngstr,"-",2);
+	split = g_strsplit(rngstr,"-",0);
 
-	rng->floor = strtol(split[0],NULL,10);
-	rng->ceil  = strtol(split[1],NULL,10);
+	/* empty string */
+	if (split[0] == NULL) {
+	  printf("0==null, rng : %s(%x)\n", rngstr);
+	  g_strfreev(split);
+	  return NULL;
+	}
+
+#if GLIB_MAJOR_VERSION >= 2
+	/* means we have a non empty string 
+	 * which does not contain a delimiter */
+	if (split[1] == NULL) {
+	  printf("1==null, rng : %s\n", rngstr);
+	  g_strfreev(split);
+	  return NULL;
+	}
+#endif
+
+	rng = g_malloc(sizeof(range_pair_t));
+
+	/* string == "X-?" */
+	if (*(split[0]) != '\0') {
+	    rng->floor = strtol(split[0],NULL,10);
+	} else
+	  /* string == "-?" */
+	  rng->floor = G_MININT;
+
+	/* string != "?-" */
+#if GLIB_MAJOR_VERSION >= 2
+	if (*(split[1]) != '\0') {
+#else
+	if (split[1] != NULL) {
+#endif
+	  rng->ceil  = strtol(split[1],NULL,10);
+	} else
+	  /* string == "?-Y" */
+	  rng->ceil = G_MAXINT;
 	
-	if (rng->ceil == 0) rng->ceil = G_MAXINT;
-	if (rng->floor == 0) rng->floor = G_MININT;
-
 	g_strfreev(split);
 	
 	return rng;