Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Python/mysnprintf.c

    r2 r391  
    2121   Return value (rv):
    2222
    23         When 0 <= rv < size, the output conversion was unexceptional, and
    24         rv characters were written to str (excluding a trailing \0 byte at
    25         str[rv]).
     23    When 0 <= rv < size, the output conversion was unexceptional, and
     24    rv characters were written to str (excluding a trailing \0 byte at
     25    str[rv]).
    2626
    27         When rv >= size, output conversion was truncated, and a buffer of
    28         size rv+1 would have been needed to avoid truncation.  str[size-1]
    29         is \0 in this case.
     27    When rv >= size, output conversion was truncated, and a buffer of
     28    size rv+1 would have been needed to avoid truncation.  str[size-1]
     29    is \0 in this case.
    3030
    31         When rv < 0, "something bad happened".  str[size-1] is \0 in this
    32         case too, but the rest of str is unreliable.  It could be that
    33         an error in format codes was detected by libc, or on platforms
    34         with a non-C99 vsnprintf simply that the buffer wasn't big enough
    35         to avoid truncation, or on platforms without any vsnprintf that
    36         PyMem_Malloc couldn't obtain space for a temp buffer.
     31    When rv < 0, "something bad happened".  str[size-1] is \0 in this
     32    case too, but the rest of str is unreliable.  It could be that
     33    an error in format codes was detected by libc, or on platforms
     34    with a non-C99 vsnprintf simply that the buffer wasn't big enough
     35    to avoid truncation, or on platforms without any vsnprintf that
     36    PyMem_Malloc couldn't obtain space for a temp buffer.
    3737
    3838   CAUTION:  Unlike C99, str != NULL and size > 0 are required.
     
    4242PyOS_snprintf(char *str, size_t size, const  char  *format, ...)
    4343{
    44         int rc;
    45         va_list va;
     44    int rc;
     45    va_list va;
    4646
    47         va_start(va, format);
    48         rc = PyOS_vsnprintf(str, size, format, va);
    49         va_end(va);
    50         return rc;
     47    va_start(va, format);
     48    rc = PyOS_vsnprintf(str, size, format, va);
     49    va_end(va);
     50    return rc;
    5151}
    5252
     
    5454PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
    5555{
    56         int len;  /* # bytes written, excluding \0 */
     56    int len;  /* # bytes written, excluding \0 */
    5757#ifdef HAVE_SNPRINTF
    5858#define _PyOS_vsnprintf_EXTRA_SPACE 1
    5959#else
    6060#define _PyOS_vsnprintf_EXTRA_SPACE 512
    61         char *buffer;
     61    char *buffer;
    6262#endif
    63         assert(str != NULL);
    64         assert(size > 0);
    65         assert(format != NULL);
    66         /* We take a size_t as input but return an int.  Sanity check
    67         * our input so that it won't cause an overflow in the
    68          * vsnprintf return value or the buffer malloc size.  */
    69         if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
    70                 len = -666;
    71                 goto Done;
    72         }
     63    assert(str != NULL);
     64    assert(size > 0);
     65    assert(format != NULL);
     66    /* We take a size_t as input but return an int.  Sanity check
     67    * our input so that it won't cause an overflow in the
     68     * vsnprintf return value or the buffer malloc size.  */
     69    if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
     70        len = -666;
     71        goto Done;
     72    }
    7373
    7474#ifdef HAVE_SNPRINTF
    75         len = vsnprintf(str, size, format, va);
     75    len = vsnprintf(str, size, format, va);
    7676#else
    77         /* Emulate it. */
    78         buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
    79         if (buffer == NULL) {
    80                 len = -666;
    81                 goto Done;
    82         }
     77    /* Emulate it. */
     78    buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
     79    if (buffer == NULL) {
     80        len = -666;
     81        goto Done;
     82    }
    8383
    84         len = vsprintf(buffer, format, va);
    85         if (len < 0)
    86                 /* ignore the error */;
     84    len = vsprintf(buffer, format, va);
     85    if (len < 0)
     86        /* ignore the error */;
    8787
    88         else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
    89                 Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
     88    else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
     89        Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
    9090
    91         else {
    92                 const size_t to_copy = (size_t)len < size ?
    93                                         (size_t)len : size - 1;
    94                 assert(to_copy < size);
    95                 memcpy(str, buffer, to_copy);
    96                 str[to_copy] = '\0';
    97         }
    98         PyMem_FREE(buffer);
     91    else {
     92        const size_t to_copy = (size_t)len < size ?
     93                                (size_t)len : size - 1;
     94        assert(to_copy < size);
     95        memcpy(str, buffer, to_copy);
     96        str[to_copy] = '\0';
     97    }
     98    PyMem_FREE(buffer);
    9999#endif
    100100Done:
    101         if (size > 0)
    102                 str[size-1] = '\0';
    103         return len;
     101    if (size > 0)
     102        str[size-1] = '\0';
     103    return len;
    104104#undef _PyOS_vsnprintf_EXTRA_SPACE
    105105}
Note: See TracChangeset for help on using the changeset viewer.