Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strcpy causing my Win32 program to crash upon exit 1

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
Hello,
Here is my program in its entirety, with the exception of stdafx.cpp, which is not used.
[ccode]
#include "stdafx.h"
#include <string.h>

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

char * pbuff = &quot;C:/Documents and Settings/dynacom/Local Settings/Application Data/Microsoft/Outlook/&quot;;
char text[] = &quot;The Outlook file was not transferd because:&quot;;
char * errortext = &quot;ERROR&quot;;
BOOL ret;
ret = CopyFile(pbuff, &quot;\\Mainroom/Outlook/Dave/Dave.pst&quot;, FALSE);
if (!ret)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0,(LPTSTR) &lpMsgBuf,0,NULL);
//strcat(text, (LPCTSTR)lpMsgBuf);
strcat(text, errortext);
MessageBox(NULL, text, NULL, 0);
MessageBox(NULL, text, &quot;ERROR&quot;, 0);
}

return 0;
}
[/ccode]
The problem lies with the commented strcat line. When I uncommend the line, the program crashes on exit. I step through using the debugger, past the return 0;, to the last bracket, when it crashes with the &quot;Memory could not be &quot;read&quot;&quot; message. The strcat function does what I expect, in debug the variable text is appended as I wanted. Does anyone have any info about this? I have very little experience with LPVOID types, so I am sure my mistake lies there. I hope I explained this halfway decent, I've been wrestling with this for a while. I made the offending line red.

To me this seems very strange. If you want, you could copy the code right from here, past it into a new simple Win32 Application, and it should run. Uncomment the line, and it will crash not on the now uncommented line, but on exit.

Thank you for your help,

CJB
 
MinnisotaFreezing,

It appears that there are a few things worth correcting:

1) text[] is allocated exactly enough space for its initialization string. strcat() will write outside of this allocated space an possibly corrupt the stack, other local variables, or information necessary for a safe return. When you use the initiialization char text{} = &quot;xxxx&quot;. You should not use text for anything other than as a constant.

2) lpMsgBuf is also allocated on the stack as a local variable. The compiler may allocate its space at function entry as an optimization. Therefore the text[] overflow may affect it too.

The bottom line is that text[] is overflowing because of its allocation and the strcat. This corrupts the stack which can cause many weird errors.

Brudnakm
 
Brudnakm,
Thanks for the help. That explians why I was getting some other wierd errors before as well. But let me ask you, what would you suggest to fix this problem? I tried:
Code:
char   text[50] = &quot;The Outlook file was not transferd because:&quot;;
char * ptext = text;
strcat(ptext, (LPCTSTR)lpMsgBuf);
but I got the same error. Do I really have to find the length of both strings and then create a new buffer of that size to use? I'll admit, my pointers are a little rusty because of CString, but I didn't think it was that hard to cat a string.

It does work if I just create my first array size [100] and hope I don't go over it when I strcat, but I'd rather not.

Again, any help is appriciated.

CJB
 
Do I really have to find the length of both strings and then create a new buffer of that size to use?

To be 100% correct, yes you should. But most people simply allocate a larger buffer (like you suggested) and hope they don't overflow it. I usually use the MAX_PATH constant.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top