Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. vsprintf crashes (only) in MFC project VC6.0

vsprintf crashes (only) in MFC project VC6.0

Scheduled Pinned Locked Moved Clever Code
c++
9 Posts 7 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Petr Stejskal
    wrote on last edited by
    #1

    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"); }

    CPalliniC N M M 4 Replies Last reply
    0
    • P Petr Stejskal

      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"); }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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

      In testa che avete, signor di Ceprano?

      P 1 Reply Last reply
      0
      • CPalliniC CPallini

        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

        P Offline
        P Offline
        Petr Stejskal
        wrote on last edited by
        #3

        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);

        CPalliniC 1 Reply Last reply
        0
        • P Petr Stejskal

          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);

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • P Petr Stejskal

            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"); }

            N Offline
            N Offline
            NormDroid
            wrote on last edited by
            #5

            So does TextOut under various conditions during printing, but hey who cares we've got .net to wrap all functions up with.

            www.software-kinetics.co.uk

            1 Reply Last reply
            0
            • P Petr Stejskal

              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"); }

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              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

              G 1 Reply Last reply
              0
              • P Petr Stejskal

                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"); }

                M Offline
                M Offline
                Mike Dimmick
                wrote on last edited by
                #7

                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 the printf 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

                P 1 Reply Last reply
                0
                • M Mike Dimmick

                  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 the printf 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

                  P Offline
                  P Offline
                  Paul Sanders the other one
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • M Michael Dunn

                    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

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    "Benefits of a classical education."     - Hans Gruber (played by Alan Rickman), Die Hard

                    Software Zen: delete this;
                    Fold With Us![^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups