tags:

views:

294

answers:

2

While attempting to use the CopyFile() function I have encountered a strange error. It will write neither of the files to my destination.

Here is the code. The section when I send files has been commented. Keep in mind this code is a rough draft so ignore the function definitions.

/*


BrowserFind.c
Version 0.2
07/29/09

LogicKills

*/


#include <stdio.h>
#include <windows.h>
#include <dirent.h>

char* getPath();
char* combineStrings(char* profile, char* path);
char** findProfile(char* path);
void copyagain();




int main()
{ 
    int fileIndex;
    char* fileLocation     = getPath();  
    char* whereAmI         = _getcwd(NULL,0);
    char **files           = findProfile(fileLocation);
    char* filesToExport[3] = {"\\formhistory.sqlite","\\cookies.sqlite", "\\downloads.sqlite"};
    char* profileName      = files[2];
    char* partPath         = strncat(fileLocation,"\\",3);
    char* pathWoutFile     = strncat(fileLocation,profileName,strlen(profileName) + 1);
    char* fullPathWithFile;
    char* fullSendPath;

    char* downloads = "\\downloads.sqlite";
    char* cookies   = "\\cookies.sqlite";
    char* history   = "\\formhistory.sqlite";







     char* from1 = strncat(fileLocation,filesToExport[0],100);
     char* send1 = strncat(whereAmI,filesToExport[0],100);


      char* from2 = strncat(fileLocation,filesToExport[1],100);
     char* send2 = strncat(whereAmI,filesToExport[1],100);



// ***** This is where I try to send the files ***** 

CopyFile(from1,send1,TRUE);

//Fails when I add two calls to CopyFile();
CopyFile(from2,send2,TRUE);

    return 0;
}

char* getPath()
{

      char* appPath;
      char* usrPath;
      char* fullPath;
      char* drive     = getenv("SYSTEMDRIVE");
      char* user      = getenv("USERNAME");


      OSVERSIONINFO info;
      info.dwOSVersionInfoSize = sizeof(info);
      GetVersionEx(&info);

      if (info.dwMajorVersion >= 6)
      {
        appPath = "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles";
        usrPath = "\\Users\\";
      }

      else
      {
         appPath = "\\Application Data\\Mozilla\\Firefox\\Profiles";
         usrPath = "\\Documents and Settings\\";
      }

      strncat(drive,usrPath,strlen(usrPath) + 1);
      strncat(drive,user,strlen(user) + 1);
      strncat(drive,appPath,strlen(appPath) + 1);
      fullPath = drive;



      return (fullPath);
}

char** findProfile(char* path)
{

    DIR *dir = opendir (path);
    struct dirent *dp;          
    size_t filecount = 0;       
    size_t i = 0;
    char **files;

    if (dir == NULL) {

        return NULL;           
    }
    while ((dp = readdir (dir)) != NULL) {
        filecount++;
    }

    files = (char **) malloc (filecount * sizeof (*files));
    if (files == NULL) {
        return NULL;            
    }


    rewinddir (dir);
    while ((dp = readdir (dir)) != NULL) {
        files[i] = strdup (dp->d_name);
        if (files[i] == NULL) {

            while (i > 0) {
                free (files[--i]);
            }
            free (files);
            return NULL;
        }

        i++;
    }

    closedir (dir);
    return files;
}
+3  A: 

in your code:

char* whereAmI         = _getcwd(NULL,0);

allocates exactly enough memory for the path

char* send2 = strncat(whereAmI,filesToExport[1],100);

strncat then tries to add to that path, using memory that isn't there.

Result: undefined behaviour.

anon
So why does this work if I took out the second CopyFile call?
LogicKills
That's the wonder of undefined behaviour.
anon
Ah, I see what I did.
LogicKills
A: 

I think at least part of your problem is that from1 and from2 point to the same string (as does send1 and send2).

These lines both return a printer to fileLocation, so from1 and from2 point to the same thing. Similar for send1 and send2.

 char* from1 = strncat(fileLocation,filesToExport[0],100);
 char* from2 = strncat(fileLocation,filesToExport[1],100);

Another problem is that you are overwriting buffers (and/or writing to buffers that you don't own).

getcwd() returns a pointer to memory that you are not allowed to modify, while _getcwd() returns a pointer to malloc'ed memory (so you can modify it), it's only as large as the string that's returned (as far as you know) so you can't concatenate to it.

Michael Burr