Patch: some minor memory fixes



	Hi,

	After valgrinding a bit, a found a lot of "uninitialized memory"
warnings reading pop3 folders. This patch just removes some of them.

Changelog entry:
2008-02-22  Jose Dapena Paz  <jdapena igalia com>

* libtinymail-camel/camel-lite/camel/providers/pop3/camel-pop3-folder.c:
	Use the new method camel_strstrcase_len for avoiding accessing
	non initialised memory areas.

* libtinymail-camel/camel-lite/camel/camel-string-utils.[ch]:
	New method camel_strstrcase_len, to limit the string search to 
	a range.

* libtinymail-camel/camel-lite/camel/camel-mime-parser.c:
	(folder_pull_part): after freeing the byte arrays, we assign 
	the pointers to NULL.

-- 
Jose Dapena Paz <jdapena igalia com>
Igalia
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 3429)
+++ ChangeLog	(working copy)
@@ -1,3 +1,16 @@
+2008-02-22  Jose Dapena Paz  <jdapena igalia com>
+
+	* libtinymail-camel/camel-lite/camel/providers/pop3/camel-pop3-folder.c:
+	Use the new method camel_strstrcase_len for avoiding accessing
+	non initialised memory areas.
+
+	* libtinymail-camel/camel-lite/camel/camel-string-utils.[ch]:
+	New method camel_strstrcase_len, to limit the string search to a range.
+
+	* libtinymail-camel/camel-lite/camel/camel-mime-parser.c:
+	(folder_pull_part): after freeing the byte arrays, we assign the pointers
+	to NULL.
+
 2008-02-21  Jose Dapena Paz  <jdapena igalia com>
 
 	* libtinymail-camel/tny-camel-mime-part.c:
Index: libtinymail-camel/camel-lite/camel/providers/pop3/camel-pop3-folder.c
===================================================================
--- libtinymail-camel/camel-lite/camel/providers/pop3/camel-pop3-folder.c	(revision 3429)
+++ libtinymail-camel/camel-lite/camel/providers/pop3/camel-pop3-folder.c	(working copy)
@@ -755,12 +755,12 @@
 			break;
 
 		/* heuristics */
-		if (camel_strstrcase (buffer, "Content-Disposition: attachment") != NULL)
+		if (camel_strstrcase_len (buffer, n, "Content-Disposition: attachment") != NULL)
 			fi->has_attachments = TRUE;
-		else if (camel_strstrcase (buffer, "filename=") != NULL &&
+		else if (camel_strstrcase_len (buffer, n, "filename=") != NULL &&
 			 strchr (buffer, '.'))
 			fi->has_attachments = TRUE;
-		else if (camel_strstrcase (buffer, "Content-Type: message/rfc822") != NULL)
+		else if (camel_strstrcase_len (buffer, n, "Content-Type: message/rfc822") != NULL)
 			fi->has_attachments = TRUE;
 
 		w += n;
@@ -816,19 +816,19 @@
 			continue;
 
 		/* heuristics */
-		if (camel_strstrcase ((const char *) buffer, "Content-Disposition: attachment") != NULL)
+		if (camel_strstrcase_len ((const char *) buffer, len, "Content-Disposition: attachment") != NULL)
 			fi->has_attachments = TRUE;
-		else if (camel_strstrcase ((const char *) buffer, "filename=") != NULL &&
+		else if (camel_strstrcase_len ((const char *) buffer, len, "filename=") != NULL &&
 			 strchr ((const char *) buffer, '.'))
 			fi->has_attachments = TRUE;
-		else if (camel_strstrcase ((const char *) buffer, "Content-Type: message/rfc822") != NULL)
+		else if (camel_strstrcase_len ((const char *) buffer, len, "Content-Type: message/rfc822") != NULL)
 			fi->has_attachments = TRUE;
 
 		if (boundary == NULL)
 		{
 			   CamelContentType *ct = NULL;
 			   const char *bound=NULL;
-			   char *pstr = (char*)camel_strstrcase ((const char *) buffer, "Content-Type:");
+			   char *pstr = (char*)camel_strstrcase_len ((const char *) buffer, len, "Content-Type:");
 
 			   if (pstr)
 			   {
Index: libtinymail-camel/camel-lite/camel/camel-string-utils.c
===================================================================
--- libtinymail-camel/camel-lite/camel/camel-string-utils.c	(revision 3429)
+++ libtinymail-camel/camel-lite/camel/camel-string-utils.c	(working copy)
@@ -170,6 +170,15 @@
 char *
 camel_strstrcase (const char *haystack, const char *needle)
 {
+	g_return_val_if_fail (haystack != NULL, NULL);
+	g_return_val_if_fail (needle != NULL, NULL);
+
+	return camel_strstrcase_len (haystack, strlen(haystack), needle);
+}
+
+char *
+camel_strstrcase_len (const char *haystack, guint haystack_len, const char *needle)
+{
 	/* find the needle in the haystack neglecting case */
 	const char *ptr;
 	guint len;
@@ -178,13 +187,13 @@
 	g_return_val_if_fail (needle != NULL, NULL);
 
 	len = strlen (needle);
-	if (len > strlen (haystack))
+	if (len > haystack_len)
 		return NULL;
 
 	if (len == 0)
 		return (char *) haystack;
 
-	for (ptr = haystack; *(ptr + len - 1) != '\0'; ptr++)
+	for (ptr = haystack; (*(ptr + len - 1) != '\0') && ((ptr - haystack + len)< haystack_len); ptr++)
 		if (!g_ascii_strncasecmp (ptr, needle, len))
 			return (char *) ptr;
 
Index: libtinymail-camel/camel-lite/camel/camel-string-utils.h
===================================================================
--- libtinymail-camel/camel-lite/camel/camel-string-utils.h	(revision 3429)
+++ libtinymail-camel/camel-lite/camel/camel-string-utils.h	(working copy)
@@ -37,6 +37,7 @@
 void camel_string_list_free (GList *string_list);
 
 char *camel_strstrcase (const char *haystack, const char *needle);
+char *camel_strstrcase_len (const char *haystack, guint haystack_len, const char *needle);
 
 const char *camel_strdown (char *str);
 char camel_tolower(char c);
Index: libtinymail-camel/camel-lite/camel/camel-mime-parser.c
===================================================================
--- libtinymail-camel/camel-lite/camel/camel-mime-parser.c	(revision 3429)
+++ libtinymail-camel/camel-lite/camel/camel-mime-parser.c	(working copy)
@@ -1004,12 +1004,18 @@
 		camel_header_raw_clear(&h->headers);
 #endif
 		camel_content_type_unref(h->content_type);
-		if (h->pretext)
+		if (h->pretext) {
 			g_byte_array_free(h->pretext, TRUE);
-		if (h->posttext)
+			h->pretext = NULL;
+		}
+		if (h->posttext) {
 			g_byte_array_free(h->posttext, TRUE);
-		if (h->from_line)
+			h->posttext = NULL;
+		}
+		if (h->from_line) {
 			g_byte_array_free(h->from_line, TRUE);
+			h->from_line = NULL;
+		}
 		g_free(h);
 	} else {
 		g_warning("Header stack underflow!\n");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]