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. General Programming
  3. C / C++ / MFC
  4. problem with sprintf_s() on win 7

problem with sprintf_s() on win 7

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiodebugginghelp
10 Posts 4 Posters 0 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.
  • U Offline
    U Offline
    User 9307625
    wrote on last edited by
    #1

    char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

    S C S U 4 Replies Last reply
    0
    • U User 9307625

      char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

      S Offline
      S Offline
      Santhosh G_
      wrote on last edited by
      #2

      Second parameter of sprintf_s is the size of destination buffer. If length of formatted string is greater than the size of source buffer, sprintf_s will create debug assertion. If length of string in Buffer is less than 3, then it will not create an debug assertion. Here you can change the second parameter of sprintf_s to 47,by considering offset in buffer.

      U 1 Reply Last reply
      0
      • U User 9307625

        char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Member 9353776 wrote:

        sprintf_s(&buffer[offset],offset,"%s",Buffer);

        Here you are stating your destination buffer has size 3 (parameter 2 of sprintf_s (see MSDN[^]). If the source buffer (namely Buffer) contains a string with more than 3 characters then you get the assertion.

        Veni, vidi, vici.

        U 1 Reply Last reply
        0
        • S Santhosh G_

          Second parameter of sprintf_s is the size of destination buffer. If length of formatted string is greater than the size of source buffer, sprintf_s will create debug assertion. If length of string in Buffer is less than 3, then it will not create an debug assertion. Here you can change the second parameter of sprintf_s to 47,by considering offset in buffer.

          U Offline
          U Offline
          User 9307625
          wrote on last edited by
          #4

          char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.

          S 2 Replies Last reply
          0
          • C CPallini

            Member 9353776 wrote:

            sprintf_s(&buffer[offset],offset,"%s",Buffer);

            Here you are stating your destination buffer has size 3 (parameter 2 of sprintf_s (see MSDN[^]). If the source buffer (namely Buffer) contains a string with more than 3 characters then you get the assertion.

            Veni, vidi, vici.

            U Offline
            U Offline
            User 9307625
            wrote on last edited by
            #5

            char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.

            C 1 Reply Last reply
            0
            • U User 9307625

              char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.

              S Offline
              S Offline
              Santhosh G_
              wrote on last edited by
              #6

              Which function is used in VC6.0 ? Is it sprintf() or sprintf_s() ?
              If it is sprintf(), then nothing to wonder, becausee sprintfdoesnot check the output buffer size.

              1 Reply Last reply
              0
              • U User 9307625

                char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.

                S Offline
                S Offline
                Santhosh G_
                wrote on last edited by
                #7

                which function is used in VC6.0 ? Is it sprintf_s() ?
                sprintf() is not checking the output buffer size. Sprintf_s ensures the output buffer overwritinng is not happening.

                1 Reply Last reply
                0
                • U User 9307625

                  char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Short answer: You sprintf statement is plain wrong if 'it is working fine' maybe by accident. Somewhat longer answer: If you have a 50 bytes buffer, why don't you pass 50 instead of 3? Why are you using the misleading name offset for passing a size?

                  Veni, vidi, vici.

                  1 Reply Last reply
                  0
                  • U User 9307625

                    char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

                    S Offline
                    S Offline
                    Sarath C
                    wrote on last edited by
                    #9

                    First thing is that, trust the implementation made by Microsoft. This is a very primitive function and you will have to read the documentation first. Second you can your strcpy_s or strcat_s (if you're concatenating). sprint_s is not really a choice string copy. Third your strings must be null terminated. To understand this better, I am illustrating this with a code example

                    char buffer\[12\]= {0};
                    int offset =3;
                    char Buffer\[50\] = "Hello World";
                    sprintf\_s(buffer,sizeof(buffer),  "%s", Buffer);
                    

                    in the above example buffer has 12 character in legnth and Hello World String contains 11 characters + null terminated. Anything less than 12 character size will raise assertion buffer too small. for sprintf_s only the number of characters being copied only matters. not really the original size of the buffers.

                    -Sarath.

                    My blog - iSpeak code

                    Rate the answers and close your posts if it's answered

                    1 Reply Last reply
                    0
                    • U User 9307625

                      char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

                      U Offline
                      U Offline
                      User 9307625
                      wrote on last edited by
                      #10

                      I have fixed the issue, I am very much thankful to all your replies

                      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