| Inhalt |
|---|
| Suchen |
|---|
| Login |
|---|
| Navigation |
|---|
| Aktuell |
|---|
| Featured Articles |
|---|
| Navigation |
|---|
| Wer ist online? |
|---|
| Wir haben 59 Gäste online |
| msnprintf - An snprintf function to terminate strings properly |
|
| Source Code - C/C++ | |||
| Geschrieben von: Thomas | |||
| Samstag, 02. Oktober 2010 um 00:00 Uhr | |||
|
Microsoft Visual Studio's _snprintf () function is not guaranteed to terminate the output string. Excerpt from that MSDN page: "Security NoteEnsure 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 ():
Note that the function causes an issue if invoked with str == NULL and str_m != 0 at the same time. Don't do that.
Open 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 /************************************************************************** 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 #endifOpen msnprintf.h
|
|||
| Zuletzt aktualisiert am Samstag, 02. Oktober 2010 um 18:00 Uhr |
Du musst dich anmelden oder registrieren, um einen Kommentar zu schreiben.
Im Forum diskutieren. (0 posts)




