Contents
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
Search
Login
Navigation
Home Source Code C/C++ msnprintf - An snprintf function to terminate strings properly
Most Recent
Featured Articles
Joomla 1.5 Featured Articles
Navigation
Home Source Code C/C++ msnprintf - An snprintf function to terminate strings properly
Who Is Online?
We have 1734 guests and 2 members online
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
msnprintf - An snprintf function to terminate strings properly E-mail
User Rating: / 3
PoorBest 
Source Code - C/C++
Written by Thomas   
Saturday, 02 October 2010 00:00

 

Microsoft Visual Studio's _snprintf () function is not guaranteed to terminate the output string.

Excerpt from that MSDN page:

"Security Note

Ensure that format is not a user-defined string. Because this function does not guarantee NULL termination (in particular, when the return value is count), ensure that it is followed by code that adds the null terminator. For more information, see Avoiding Buffer Overruns."

The following function addresses this by just adding a terminator at the end of each string. I personally find this approach better than using different functions.

If you like, you can redifine _snprintf ():

  1. #define _snprintf msnprintf

Note that the function causes an issue if invoked with str == NULL and str_m != 0 at the same time. Don't do that. Smile

 

Open msnprintf.c
 

msnprintf.c:

/**************************************************************************
 
  http://www.dateiliste.com
 
  File:    msnprintf.c
  Why:    Wrapper function for _snprintf or snprintf that always
         terminates the given buffer with a '\0' character.
 
  OS:      -
 
  Autor:    Thomas
  Created:  2008-04-02
 
 
History
-------
 
When    Who        what
---------------------------------------------------------------------------
2008-04-02  Thomas      Created.
 
**************************************************************************/
 
#include <stdarg.h>
#include <stdio.h>
 
#ifndef vsnprintf
#define vsnprintf _vsnprintf
#endif
 
/*
  msnprintf
 
  Function works like snprintf or _snprintf with the exception that
   the buffer is always terminated with a '\0' character. If the
   arguments require a bigger buffer the result buffer str is
   truncated to str_m characters.
*/
 
int msnprintf (char *str, size_t str_m, const char *fmt, /*args*/ ...)
{
  va_list ap;
  int str_l;
 
  va_start(ap, fmt);
  str_l = vsnprintf (str, str_m, fmt, ap);
  va_end(ap);
  if (str_m)
    str [str_m - 1] = '\0';
  return str_l;
}
 
Open msnprintf.c

 

Open msnprintf.h
 

msnprintf.h:

/**************************************************************************
 
  http://www.dateiliste.com
 
  File:    msnprintf.h
  Why:    Wrapper function for _snprintf or snprintf that always
         terminates the given buffer with a '\0' character.
 
  OS:      -
 
  Autor:    Thomas
  Created:  2008-04-02
 
 
History
-------
 
When    Who        what
---------------------------------------------------------------------------
2008-04-02  Thomas      Created.
 
**************************************************************************/
 
#ifndef _have_msnprintf_h
#define _have_msnprintf_h
 
#ifdef __cplusplus
extern "C" {
#endif
 
int msnprintf (char *str, size_t str_m, const char *fmt, /*args*/ ...);
 
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif
 
#endif
 
Open msnprintf.h

 

Last Updated on Saturday, 02 October 2010 18:00
 
You need to login or register to post comments.
Discuss this item on the forums. (0 posts)
Discuss (0 posts)