r497 - trunk

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Wed Feb 20 07:31:21 EST 2008


Author: icculus
Date: 2008-02-20 07:31:21 -0500 (Wed, 20 Feb 2008)
New Revision: 497

Modified:
   trunk/archive_zip.c
Log:
Valgrind caught this one: memcpy() overlap when we compare/swap a zip entry
 against itself during sorting. Minor optimization and portability fix.

I applied this change to PhysicsFS svn too, so I didn't mark it with
 __MOJOSETUP__ in this codebase.



Modified: trunk/archive_zip.c
===================================================================
--- trunk/archive_zip.c	2008-02-20 10:20:36 UTC (rev 496)
+++ trunk/archive_zip.c	2008-02-20 12:31:21 UTC (rev 497)
@@ -1143,19 +1143,27 @@
 
 static int zip_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
 {
-    ZIPentry *a = (ZIPentry *) _a;
-    return(strcmp(a[one].name, a[two].name));
+    if (one != two)
+    {
+        const ZIPentry *a = (const ZIPentry *) _a;
+        return(strcmp(a[one].name, a[two].name));
+    } /* if */
+
+    return 0;
 } /* zip_entry_cmp */
 
 
 static void zip_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
 {
-    ZIPentry tmp;
-    ZIPentry *first = &(((ZIPentry *) _a)[one]);
-    ZIPentry *second = &(((ZIPentry *) _a)[two]);
-    memcpy(&tmp, first, sizeof (ZIPentry));
-    memcpy(first, second, sizeof (ZIPentry));
-    memcpy(second, &tmp, sizeof (ZIPentry));
+    if (one != two)
+    {
+        ZIPentry tmp;
+        ZIPentry *first = &(((ZIPentry *) _a)[one]);
+        ZIPentry *second = &(((ZIPentry *) _a)[two]);
+        memcpy(&tmp, first, sizeof (ZIPentry));
+        memcpy(first, second, sizeof (ZIPentry));
+        memcpy(second, &tmp, sizeof (ZIPentry));
+    } /* if */
 } /* zip_entry_swap */
 
 




More information about the mojosetup-commits mailing list