Thread: snprintf for Linux ok for Windows a problem!!

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    snprintf for Linux ok for Windows a problem!!

    I got an issue with snprintf
    I am porting a software (from Linux to Windows) which makes massive use of snprintf and I found out that in Windows the terminating null char is not put at the end in case of string excessive length..
    please suggest a fast porting for this!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Which compiler are you using on windows?


    Security Note Ensure that format is not a user-defined string. This function does not guarantee NULL termination, so ensure it is followed by sz[ ARRAYSIZE(sz) - 1] = 0. For more information, see Avoiding Buffer Overruns.

    from http://msdn.microsoft.com/en-us/libr...93(VS.71).aspx
    Last edited by Bayint Naung; 06-15-2010 at 06:02 AM.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    that's very annoying
    under Linux the NUL at the end is guaranteed

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can't you just write your own wrapper?
    Code:
        int my_snprintf(char *str, size_t size, const char *format, ...)
        {
               int ret;
               ret = sprintf( ... );
               
               str[size-1] = 0;      // i do it myself if it doesn't do it for me lol!
              ...
         }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mynickmynick View Post
    that's very annoying
    under Linux the NUL at the end is guaranteed
    Hmmm. But snprintf is not a standard function AFAIK.
    So you cannot rely on it being the same for all operating systems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The snprintf() and vsnprintf() functions conform to C99.
    To OP, Why don't you use gcc ? like mingw?
    Last edited by Bayint Naung; 06-17-2010 at 05:04 AM. Reason: ps

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Bayint Naung View Post
    The snprintf() and vsnprintf() functions conform to C99.
    To OP, Why don't you use gcc ? like mingw?
    i have to use MS Vis. cause i was inserted into a project started by some one else. I think it also uses special dlls for interfacing with PLC software running on the same Windows machine , which I suppose are not available with mingw

  8. #8
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Bayint Naung View Post
    Can't you just write your own wrapper?
    Code:
        int my_snprintf(char *str, size_t size, const char *format, ...)
        {
               int ret;
               ret = sprintf( ... );
               
               str[size-1] = 0;      // i do it myself if it doesn't do it for me lol!
              ...
         }
    yeah i should but i am not able to write a function with a variable number of arguments (input parameters).. do you have a simple but fully written example (of the sintax for a variable number of arguments function)? I was said to use a va_list some days ago but i didn't figure out in detail how to use it..

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    stdarg example

    Code:
    int my_snprintf(char *str,size_t size,const char *fmt,...)
    {
       int ret;
       va_list ap;
       
       va_start(ap,fmt); 
       ret = vsnprintf(str,size,fmt,ap);
       // Whatever happen in vsnprintf, what i'll do is just to null terminate it 
       // and everything else s mynickmynick's problem
       str[size-1] = '\0';       
        va_end(ap);    
       return ret;    
    }
    After reading from msdn *nprintf documentation, there is another difference from standard *nprintf function.

    msdn
    1. If the number of characters to write exceeds count, then count characters are written and –1 is returned.
    2. If buffer is a null pointer and count is nonzero, or format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

    ansi
    1. If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available.
    2. C99 allows str to be NULL in this case, and gives the return value (as always) as the number of characters that would have been written in case the output string has been large enough.
    Last edited by Bayint Naung; 06-17-2010 at 06:46 AM.

  10. #10
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM