Back to index

python3.2  3.2.2
Defines | Functions
mysnprintf.c File Reference
#include "Python.h"

Go to the source code of this file.

Defines

#define _PyOS_vsnprintf_EXTRA_SPACE   512

Functions

int PyOS_snprintf (char *str, size_t size, const char *format,...)
int PyOS_vsnprintf (char *str, size_t size, const char *format, va_list va)

Define Documentation

#define _PyOS_vsnprintf_EXTRA_SPACE   512

Function Documentation

int PyOS_snprintf ( char *  str,
size_t  size,
const char *  format,
  ... 
)

Definition at line 41 of file mysnprintf.c.

{
    int rc;
    va_list va;

    va_start(va, format);
    rc = PyOS_vsnprintf(str, size, format, va);
    va_end(va);
    return rc;
}

Here is the call graph for this function:

int PyOS_vsnprintf ( char *  str,
size_t  size,
const char *  format,
va_list  va 
)

Definition at line 53 of file mysnprintf.c.

{
    int len;  /* # bytes written, excluding \0 */
#ifdef HAVE_SNPRINTF
#define _PyOS_vsnprintf_EXTRA_SPACE 1
#else
#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.  */
    if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
        len = -666;
        goto Done;
    }

#ifdef HAVE_SNPRINTF
    len = vsnprintf(str, size, format, va);
#else
    /* Emulate it. */
    buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
    if (buffer == NULL) {
        len = -666;
        goto Done;
    }

    len = vsprintf(buffer, format, va);
    if (len < 0)
        /* ignore the error */;

    else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
        Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");

    else {
        const size_t to_copy = (size_t)len < size ?
                                (size_t)len : size - 1;
        assert(to_copy < size);
        memcpy(str, buffer, to_copy);
        str[to_copy] = '\0';
    }
    PyMem_FREE(buffer);
#endif
Done:
    if (size > 0)
        str[size-1] = '\0';
    return len;
#undef _PyOS_vsnprintf_EXTRA_SPACE
}

Here is the call graph for this function:

Here is the caller graph for this function: