[gmime-devel] [Patch] multipart insert fix in current git master branch



Hello,
When you insert new mime part to GMimeMultipart mime part at some existing
index, the mime part at that index gets overwritten and later some strange
things may happen (because multipart->children array is resized and nothing
is placed at the new location). Here is the patch:

diff --git a/gmime/gmime-multipart.c b/gmime/gmime-multipart.c
index dae0e33..22b414f 100644
--- a/gmime/gmime-multipart.c
+++ b/gmime/gmime-multipart.c
@@ -497,7 +497,7 @@ ptr_array_insert (GPtrArray *array, guint index,
gpointer object)
        
        g_ptr_array_set_size (array, array->len + 1);
        
-       if (index == array->len) {
+       if (index < array->len-1) {
                /* need to move items down */
                dest = ((unsigned char *) array->pdata) + (sizeof (void *)
* (index + 1));
                src = ((unsigned char *) array->pdata) + (sizeof (void *)
* index);
@@ -529,9 +529,13 @@ multipart_insert (GMimeMultipart *multipart, int
index, GMimeObject *part)
 void
 g_mime_multipart_insert (GMimeMultipart *multipart, int index,
GMimeObject *part)
 {
+       int num_parts;
+
        g_return_if_fail (GMIME_IS_MULTIPART (multipart));
        g_return_if_fail (GMIME_IS_OBJECT (part));
        g_return_if_fail (index >= 0);
+       num_parts = multipart->children->len;
+       g_return_if_fail (index < num_parts+1);
        
        GMIME_MULTIPART_GET_CLASS (multipart)->insert (multipart, index,
part);
 }

As you can see it also adds new assertion so it doesn't crash when you
insert at some out of range index.

-- 
Mateusz Kwaśniewski


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