vsprintf crashes (only) in MFC project VC6.0
-
Create a new dialog project in Visual C++ 6.0 (MFC) add following code: #include <stdio.h> #include <stdarg.h> #include <string.h> void LogMsg(char* szFormat, ...) { char szBuf[10000]; va_list argptr = NULL; va_start(argptr, szFormat); vsprintf(szBuf, szFormat, argptr); // <--------------------- CRASH!!! va_end(argptr); } void CTestDlg::OnOK() { LogMsg("%20200e"); }
-
Create a new dialog project in Visual C++ 6.0 (MFC) add following code: #include <stdio.h> #include <stdarg.h> #include <string.h> void LogMsg(char* szFormat, ...) { char szBuf[10000]; va_list argptr = NULL; va_start(argptr, szFormat); vsprintf(szBuf, szFormat, argptr); // <--------------------- CRASH!!! va_end(argptr); } void CTestDlg::OnOK() { LogMsg("%20200e"); }
I wonder it doesn't crashes on other envs. You're deliberately trying to crash it. Is that subtle?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
I wonder it doesn't crashes on other envs. You're deliberately trying to crash it. Is that subtle?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThis is a real bug from my logging system. There are more values that cause crash, e.g.: LogMsg("C:\\Prilohaa%20o%20LS%20-%202004.doc"); LogMsg("C:\\PRILOHAi%20LP%202005.doc"); It crashes also on ATL project. the solution that I found is to replace the function with: _vsnprintf(szBuf, sizeof(szBuf), szFormat, argptr);
-
This is a real bug from my logging system. There are more values that cause crash, e.g.: LogMsg("C:\\Prilohaa%20o%20LS%20-%202004.doc"); LogMsg("C:\\PRILOHAi%20LP%202005.doc"); It crashes also on ATL project. the solution that I found is to replace the function with: _vsnprintf(szBuf, sizeof(szBuf), szFormat, argptr);
Another solution would be using properly the
format
parameter of the function, i.e.:LogMsg("%s","C:\\Prilohaa%20o%20LS%20-%202004.doc");
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Create a new dialog project in Visual C++ 6.0 (MFC) add following code: #include <stdio.h> #include <stdarg.h> #include <string.h> void LogMsg(char* szFormat, ...) { char szBuf[10000]; va_list argptr = NULL; va_start(argptr, szFormat); vsprintf(szBuf, szFormat, argptr); // <--------------------- CRASH!!! va_end(argptr); } void CTestDlg::OnOK() { LogMsg("%20200e"); }
So does TextOut under various conditions during printing, but hey who cares we've got .net to wrap all functions up with.
-
Create a new dialog project in Visual C++ 6.0 (MFC) add following code: #include <stdio.h> #include <stdarg.h> #include <string.h> void LogMsg(char* szFormat, ...) { char szBuf[10000]; va_list argptr = NULL; va_start(argptr, szFormat); vsprintf(szBuf, szFormat, argptr); // <--------------------- CRASH!!! va_end(argptr); } void CTestDlg::OnOK() { LogMsg("%20200e"); }
GIGO
--Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen
-
Create a new dialog project in Visual C++ 6.0 (MFC) add following code: #include <stdio.h> #include <stdarg.h> #include <string.h> void LogMsg(char* szFormat, ...) { char szBuf[10000]; va_list argptr = NULL; va_start(argptr, szFormat); vsprintf(szBuf, szFormat, argptr); // <--------------------- CRASH!!! va_end(argptr); } void CTestDlg::OnOK() { LogMsg("%20200e"); }
Well of course it crashes, you've asked it to print a
double
in exponential format in a width of 20,200 characters, and you haven't supplied any arguments to format. So you get whatever junk is currently on top of the floating point stack. If unlucky, it'll be something that can't be formatted. It's a bit of a flaw of theprintf
family of functions that the argument list is based off the format parameter, not off the first actual argument. If you're calling a formatted print routine you should be required to supply at least one argument to format. Otherwise you get people passing a user buffer to the format parameter, and chaos ensues as soon as the user tries to print a string with a % character in it.DoEvents: Generating unexpected recursion since 1991
-
Well of course it crashes, you've asked it to print a
double
in exponential format in a width of 20,200 characters, and you haven't supplied any arguments to format. So you get whatever junk is currently on top of the floating point stack. If unlucky, it'll be something that can't be formatted. It's a bit of a flaw of theprintf
family of functions that the argument list is based off the format parameter, not off the first actual argument. If you're calling a formatted print routine you should be required to supply at least one argument to format. Otherwise you get people passing a user buffer to the format parameter, and chaos ensues as soon as the user tries to print a string with a % character in it.DoEvents: Generating unexpected recursion since 1991
He's right, you know, you can't just throw in a % sign any old where. To print a literal % sign, double it up, e.g.
LogMsg ("10%% of nothing = something");
or use the trick suggested above:LogMsg ("%s", "10% of nothing is still something");
I may be old fashioned, but I think the printf family of functions are wonderful. Soooooooo much more elegant than ostreams.Paul Sanders http://www.alpinesoft.co.uk
-
GIGO
--Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen
"Benefits of a classical education." - Hans Gruber (played by Alan Rickman), Die Hard
Software Zen:
delete this;
Fold With Us![^]