From: Vincent Fatica on
#include <windows.h>
#include <stdio.h>

INT wmain ( INT argc, WCHAR **argv )
{
WCHAR szFile[MAX_PATH] = L"\"";

wsprintf(szFile + 1 + GetTempPath(MAX_PATH, szFile + 1), L"foobar.txt\"");

wprintf(L"%s\n", szFile);

return 0;
}

When I build the above (/MT, /MTd, VC9) it prints the correct string and then
crashes. The debug version attributes the crash to stack corruption near
szFile. What's up with that? Thanks.
--
- Vince
From: Vincent Fatica on
On 1 Jun 2010 13:18:45 -0400, Vincent Fatica <vince(a)blackholespam.net> wrote:

|#include <windows.h>
|#include <stdio.h>
|
|INT wmain ( INT argc, WCHAR **argv )
|{
| WCHAR szFile[MAX_PATH] = L"\"";
|
| wsprintf(szFile + 1 + GetTempPath(MAX_PATH, szFile + 1), L"foobar.txt\"");
|
| wprintf(L"%s\n", szFile);
|
| return 0;
|}
|
|When I build the above (/MT, /MTd, VC9) it prints the correct string and then
|crashes. The debug version attributes the crash to stack corruption near
|szFile. What's up with that? Thanks.

OK, I get it. That should be

... GetTempPath(MAX_PATH - 1, ...

but I'm a bit surprised. The temp path is short. Do you suppose GetTempPath()
is zeroing the whole buffer? I have been sloppy with buffer sizes before and
never ran into such a problem until today.
--
- Vince
From: Brian Muth on
> OK, I get it. That should be
>
> ... GetTempPath(MAX_PATH - 1, ...
>
> but I'm a bit surprised. The temp path is short. Do you suppose
> GetTempPath()
> is zeroing the whole buffer?

Yes. You informed GetTempPath that the buffer size was MAX_PATH, and it was
zeroed.


> I have been sloppy with buffer sizes before and
> never ran into such a problem until today.

Lesson learned, I trust.


From: Vincent Fatica on
On Tue, 1 Jun 2010 11:36:46 -0700, "Brian Muth" <bmuth(a)mvps.org> wrote:

|> OK, I get it. That should be
|>
|> ... GetTempPath(MAX_PATH - 1, ...
|>
|> but I'm a bit surprised. The temp path is short. Do you suppose
|> GetTempPath()
|> is zeroing the whole buffer?
|
|Yes. You informed GetTempPath that the buffer size was MAX_PATH, and it was
|zeroed.
|
|
|> I have been sloppy with buffer sizes before and
|> never ran into such a problem until today.
|
|Lesson learned, I trust.

Yes, indeed! Do you think many similar Win32 functions (put a string in a
buffer of stated size) zero the whole buffer? If they do, I think I'd have
learned this lesson a long time ago.
--
- Vince
From: Brian Muth on
> Yes, indeed! Do you think many similar Win32 functions (put a string in a
> buffer of stated size) zero the whole buffer?

I don't know. Probably not, but that's just pure speculation on my part.