diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-15 19:59:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-15 19:59:47 (GMT) |
commit | e822e37946f27c09953bb5733acf3b07c2db690f (patch) | |
tree | d0bb8d8769be9f469aa6675fb820bc41acfd65c5 /Python | |
parent | 5f79f46612c351bde78a41c5264c42db21008868 (diff) | |
download | cpython-e822e37946f27c09953bb5733acf3b07c2db690f.zip cpython-e822e37946f27c09953bb5733acf3b07c2db690f.tar.gz cpython-e822e37946f27c09953bb5733acf3b07c2db690f.tar.bz2 |
bpo-36020: Remove snprintf macro in pyerrors.h (GH-20889)
On Windows, #include "pyerrors.h" no longer defines "snprintf" and
"vsnprintf" macros.
PyOS_snprintf() and PyOS_vsnprintf() should be used to get portable
behavior.
Replace snprintf() calls with PyOS_snprintf() and replace vsnprintf()
calls with PyOS_vsnprintf().
Diffstat (limited to 'Python')
-rw-r--r-- | Python/mysnprintf.c | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/Python/mysnprintf.c b/Python/mysnprintf.c index 945a81a..458ca14 100644 --- a/Python/mysnprintf.c +++ b/Python/mysnprintf.c @@ -1,6 +1,8 @@ #include "Python.h" -/* snprintf() wrappers. If the platform has vsnprintf, we use it, else we +/* snprintf() and vsnprintf() wrappers. + + If the platform has vsnprintf, we use it, else we emulate it in a half-hearted way. Even if the platform has it, we wrap it because platforms differ in what vsnprintf does in case the buffer is too small: C99 behavior is to return the number of characters that @@ -52,16 +54,17 @@ PyOS_snprintf(char *str, size_t size, const char *format, ...) int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) { + assert(str != NULL); + assert(size > 0); + assert(format != NULL); + int len; /* # bytes written, excluding \0 */ -#ifdef HAVE_SNPRINTF -#define _PyOS_vsnprintf_EXTRA_SPACE 1 +#if defined(_MSC_VER) || defined(HAVE_SNPRINTF) +# define _PyOS_vsnprintf_EXTRA_SPACE 1 #else -#define _PyOS_vsnprintf_EXTRA_SPACE 512 +# define _PyOS_vsnprintf_EXTRA_SPACE 512 char *buffer; #endif - assert(str != NULL); - assert(size > 0); - assert(format != NULL); /* We take a size_t as input but return an int. Sanity check * our input so that it won't cause an overflow in the * vsnprintf return value or the buffer malloc size. */ @@ -70,10 +73,12 @@ PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) goto Done; } -#ifdef HAVE_SNPRINTF +#if defined(_MSC_VER) + len = _vsnprintf(str, size, format, va); +#elif defined(HAVE_SNPRINTF) len = vsnprintf(str, size, format, va); #else - /* Emulate it. */ + /* Emulate vsnprintf(). */ buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE); if (buffer == NULL) { len = -666; @@ -96,9 +101,11 @@ PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) } PyMem_FREE(buffer); #endif + Done: - if (size > 0) + if (size > 0) { str[size-1] = '\0'; + } return len; #undef _PyOS_vsnprintf_EXTRA_SPACE } |