VirtualBox

Changeset 36598 in vbox


Ignore:
Timestamp:
Apr 6, 2011 8:03:53 PM (14 years ago)
Author:
vboxsync
Message:

RTDirCreateUniqueNumbered: RTStrFormatNumber is not safe.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/generic/RTDirCreateUniqueNumbered-generic.cpp

    r36582 r36598  
    3737#include <iprt/string.h>
    3838
     39
    3940RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, signed int cchDigits, char chSep)
    4041{
     
    4344     */
    4445    AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
    45     AssertReturn(cbSize, VERR_INVALID_PARAMETER);
    46     AssertReturn(cchDigits, VERR_INVALID_PARAMETER);
    47     /* Check for enough space. */
    48     char *pszEnd = strchr(pszPath, '\0');
     46    AssertReturn(cbSize, VERR_BUFFER_OVERFLOW);
     47    AssertReturn(cchDigits > 0, VERR_INVALID_PARAMETER);
     48
     49    /* Check that there is sufficient space. */
     50    char *pszEnd = RTStrEnd(pszPath, cbSize);
     51    AssertReturn(pszEnd, VERR_BUFFER_OVERFLOW);
    4952    AssertReturn(cbSize - 1 - (pszEnd - pszPath) >= (size_t)cchDigits + (chSep ? 1 : 0), VERR_BUFFER_OVERFLOW);
     53    size_t cbLeft = cbSize - (pszEnd - pszPath);
    5054
    5155    /* First try is to create the path without any numbers. */
     
    5862    if (chSep != '\0')
    5963    {
     64        cbLeft--;
    6065        *pszEnd++ = chSep;
    6166        *pszEnd   = '\0';
    6267    }
    6368
    64     /* How many tries? */
    65     size_t cMaxTries = 10;
    66     for (size_t a = 0; a < (size_t)cchDigits - 1; ++a)
    67         cMaxTries *= 10;
     69    /* How many tries? Stay within somewhat sane limits. */
     70    uint32_t cMaxTries;
     71    if (cchDigits >= 8)
     72        cMaxTries = 100 * _1M;
     73    else
     74    {
     75        cMaxTries = 10;
     76        for (int a = 0; a < cchDigits - 1; ++a)
     77            cMaxTries *= 10;
     78    }
    6879
    69     /* Try cMaxTries - 1 counts to create a directory with the appended number. */
    70     size_t i = 1;
     80    /* Try cMaxTries - 1 times to create a directory with appended numbers. */
     81    uint32_t i = 1;
    7182    while (i < cMaxTries)
    7283    {
    7384        /* Format the number with leading zero's. */
    74         rc = RTStrFormatNumber(pszEnd, i, 10, cchDigits, 0, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD);
     85        rc = RTStrFormatU32(pszEnd, cbLeft, i, 10, cchDigits, 0, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD);
    7586        if (RT_FAILURE(rc))
    7687        {
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette