CVS: winex/files drive.c,1.44,1.45

[email protected]
Newsgroups gmane.comp.emulators.winex.cvs
Message-ID <[email protected]>
Subject: winex/files drive.c,1.44,1.45Update of /var/lib/cvsd/cvsroot/winex/files
In directory agravaine:/tmp/cvs-serv13215/files

Modified Files:
	drive.c 
Log Message:

Fix a variety of issues in the Apple dynamic path expansion noticed when working on the above ticket. In particular:
- indent code so it's legible with regular-sized editor
- use the correct memory allocator
- fix 1k memory leak
- check for allocation failures
- fix always-occurring 1-byte stack and heap corruption
- fix potential 1k heap overflow on pathological paths
- check for failure of some CFURL functions
- actually check the set return value to determine whether expanding the path succeeded or failed

This code could still use some more cleanup, but this fixes the worst offenders.


Index: drive.c
===================================================================
RCS file: /var/lib/cvsd/cvsroot/winex/files/drive.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- drive.c	30 Mar 2007 20:05:47 -0000	1.44
+++ drive.c	30 Mar 2007 20:09:02 -0000	1.45
@@ -574,116 +574,131 @@
  */
 static int DRIVE_CheckForDynamicPath( char* path, size_t path_size )
 {
-       int return_value = 0;
+   if (strstr(path, "@WORKINGFOLDER@"))
+      return (getcwd (path, path_size) != NULL);
+   else if (strstr (path, "@USERPREFS@"))
+   {
+      char *pNewPath;
+      char *pEnvPtr;
+      const char *pRestOfPath;
 
-       if (strstr(path, "@WORKINGFOLDER@"))
-       {
-               if (getcwd(path, path_size))
-               {
-                       return_value = 1;
-               }
-       }
-       else if (strstr (path, "@USERPREFS@"))
-       {
-          char *pNewPath;
-          char *pEnvPtr;
-          const char *pRestOfPath;
+      /* On the Apple, this contains the path to the user's individual
+         Cider game directory:
+         /Users/foo/Library/Preferences/Cider <game> Preferences/ */
+      pEnvPtr = getenv ("WINEPREFIX");
+      if (!pEnvPtr)
+         return 0;
 
-          /* On the Apple, this contains the path to the user's individual
-             Cider game directory:
-             /Users/foo/Library/Preferences/Cider <game> Preferences/ */
-          pEnvPtr = getenv ("WINEPREFIX");
-          if (!pEnvPtr)
-             return 0;
+      pNewPath = HeapAlloc (GetProcessHeap (), 0,
+                            sizeof (char *) * path_size);
+      if (!pNewPath)
+         return 0;
 
-          pNewPath = HeapAlloc (GetProcessHeap (), 0,
-                                sizeof (char *) * path_size);
-          if (!pNewPath)
-             return 0;
+      pRestOfPath = strrchr (path, '@') + 1;
 
-          pRestOfPath = strrchr (path, '@') + 1;
+      snprintf (pNewPath, path_size, "%s%s", pEnvPtr,
+                pRestOfPath);
 
-          snprintf (pNewPath, path_size, "%s%s", pEnvPtr,
-                    pRestOfPath);
+      strncpy (path, pNewPath, path_size);
+      path[path_size - 1] = '\0';
+      HeapFree (GetProcessHeap (), 0, pNewPath);
+   }
+#ifdef __APPLE__
+   else 
+   {
+      CFURLRef bundle_url = NULL;
+      CFBundleRef mainbundle = CFBundleGetMainBundle();
+      if (strstr(path,"@BUNDLEPATH@"))
+      {
+         CFURLRef bundle_url = CFBundleCopyBundleURL(mainbundle);
+         if (!CFURLGetFileSystemRepresentation(bundle_url, true, path,
+                                               path_size))
+         {
+            CFRelease (bundle_url);
+            return 0;
+         }
+      }
+      else if (strstr(path,"@BUNDLEPATHRESOURCE@"))
+      {
+         int path_space_remaining = path_size;
+         char* temp_string;
+         char* saved_path = HeapAlloc (GetProcessHeap (), 0,
+                                       sizeof (char *) * path_size);
+         if (!saved_path)
+            return 0;
 
-          strncpy (path, pNewPath, path_size);
-          path[path_size - 1] = '\0';
-          HeapFree (GetProcessHeap (), 0, pNewPath);
+         strncpy(saved_path, path, path_size);
+         saved_path[path_size - 1] = '\0';
 
-          return_value = 1;
-       }
-#ifdef __APPLE__
-       else 
-       {
-	       CFURLRef bundle_url = NULL;
-	       CFBundleRef mainbundle = CFBundleGetMainBundle();
-	       if (strstr(path,"@BUNDLEPATH@"))
-	       {
-		       CFURLRef bundle_url = CFBundleCopyBundleURL(mainbundle);
-		       if (CFURLGetFileSystemRepresentation(bundle_url, true, path, path_size))
-		       {
-			       return_value = 1;
-		       }
-	       }
-	       else if (strstr(path,"@BUNDLEPATHRESOURCE@"))
-	       {
-		       int path_space_remaining = path_size;
-		       char* temp_string;
-		       char* saved_path = malloc(sizeof(char*)*path_size);
-		       strncpy(saved_path, path, path_size);
-		       saved_path[path_size] = '\0';
+         CFURLRef bundle_url = CFBundleCopyResourcesDirectoryURL(mainbundle);
+         if (!CFURLGetFileSystemRepresentation(bundle_url, true, path,
+                                               path_size))
+         {
+            CFRelease (bundle_url);
+            strcpy (path, saved_path);
+            HeapFree (GetProcessHeap (), 0, saved_path);
+            return 0;
+         }
 
-		       CFURLRef bundle_url = CFBundleCopyResourcesDirectoryURL(mainbundle);
-		       if (CFURLGetFileSystemRepresentation(bundle_url, true, path, path_size))
-		       {
-			       return_value = 1;
-		       }
-		       /* now let's see if we have a path on the end of the string */
-		       temp_string = strrchr(saved_path, '@');
-		       if ((temp_string - saved_path + 1) < strlen(saved_path))
-		       {
-			       temp_string++;
-			       strncat(path, temp_string, path_space_remaining);
-			       path[path_size] = '\0';
-		       }
-	       }
-	       else if (strstr(path, "@BUNDLEPATHPARENT@"))
-	       {
-		       int path_space_remaining = path_size;
-		       char* temp_string;
-		       char* saved_path = malloc(sizeof(char*)*path_size);
-		       strncpy(saved_path, path, path_size);
-		       saved_path[path_size] = '\0';
-		
-		       CFURLRef bundle_url = CFBundleCopyBundleURL(mainbundle);
-		       if (CFURLGetFileSystemRepresentation(bundle_url, true, path, path_size))
-		       {
-			       return_value = 1;
-		       }
-		       temp_string = strrchr(path, '/');
-		       if ((temp_string - path + 1) == strlen(path))
-		       {
-			       temp_string[0] = '\0';
-			       temp_string = strrchr(path, '/');
-		       }
-		       temp_string[0] = '\0';
+         path_space_remaining -= strlen(path);
 
-		       path_space_remaining -= strlen(path);
+         /* now let's see if we have a path on the end of the string */
+         temp_string = strrchr(saved_path, '@');
+         if ((temp_string - saved_path + 1) < strlen(saved_path))
+         {
+            temp_string++;
+            strncat(path, temp_string, path_space_remaining);
+            path[path_size - 1] = '\0';
+         }
 
-		       /* now let's see if we have a path on the end of the string */
-		       temp_string = strrchr(saved_path, '@');
-		       if ((temp_string - saved_path + 1) < strlen(saved_path))
-		       {
-			       temp_string++;
-			       strncat(path, temp_string, path_space_remaining);
-			       path[path_size] = '\0';
-		       }
-		       free(saved_path);
-	       }
-               if (bundle_url) CFRelease(bundle_url);
-       }
+         HeapFree (GetProcessHeap (), 0, saved_path);
+      }
+      else if (strstr(path, "@BUNDLEPATHPARENT@"))
+      {
+         int path_space_remaining = path_size;
+         char* temp_string;
+         char* saved_path = HeapAlloc (GetProcessHeap (), 0,
+                                       sizeof (char *) * path_size);
+         if (!saved_path)
+            return 0;
+
+         strncpy(saved_path, path, path_size);
+         saved_path[path_size - 1] = '\0';
+
+         CFURLRef bundle_url = CFBundleCopyBundleURL(mainbundle);
+         if (!CFURLGetFileSystemRepresentation(bundle_url, true, path,
+                                               path_size))
+         {
+            CFRelease (bundle_url);
+            strcpy (path, saved_path);
+            HeapFree (GetProcessHeap (), 0, saved_path);
+            return 0;
+         }
+
+         temp_string = strrchr(path, '/');
+         if ((temp_string - path + 1) == strlen(path))
+         {
+            temp_string[0] = '\0';
+            temp_string = strrchr(path, '/');
+         }
+         temp_string[0] = '\0';
+
+         path_space_remaining -= strlen(path);
+
+         /* now let's see if we have a path on the end of the string */
+         temp_string = strrchr(saved_path, '@');
+         if ((temp_string - saved_path + 1) < strlen(saved_path))
+         {
+            temp_string++;
+            strncat(path, temp_string, path_space_remaining);
+            path[path_size - 1] = '\0';
+         }
+         HeapFree (GetProcessHeap (), 0, saved_path);
+      }
+      if (bundle_url) CFRelease(bundle_url);
+   }
 #endif  /* __APPLE__ */
-       return return_value;
+   return 1;
 }
 
 /***********************************************************************
@@ -705,7 +720,13 @@
         PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
         if (path[0])
         {
-            DRIVE_CheckForDynamicPath(path, MAX_PATHNAME_LEN);
+            if (!DRIVE_CheckForDynamicPath (path, sizeof (path)))
+            {
+               MESSAGE ("Unable to retrieve dynamic path portion of drive %c\n",
+                        'A' + i);
+               continue;
+            }
+
             p = path + strlen(path) - 1;
             while ((p > path) && (*p == '/')) *p-- = '\0';
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.