[MLton] Bug Report -- Stack corruption

Stephen Weeks MLton@mlton.org
Sat, 26 Feb 2005 18:15:28 -0800


> Function "mkstemp" in mlton/runtime/platform/mingw.c corrupts its own stack.
...
> The last line above writes at templ[4], one past the end since the char array
> only has size 4.

Thanks for the bug report.  I've checked in a fix.  Here's the new
code.

int mkstemp (char *template) {
	char file_path[255];
	char file_name[255];
	char templ[4];

	if (0 == GetTempPath (sizeof (file_path), file_path))
		diee ("unable to make temporary file");
	strncpy (templ, template, sizeof (templ) - 1);
	templ[sizeof (templ) - 1] = 0x00;
	if (0 == GetTempFileName (file_path, templ, 0, file_name))
		diee ("unable to make temporary file");
	return _open (file_name, _O_CREAT | _O_RDWR, _S_IREAD | _S_IWRITE);
}