VirtualBox

Ignore:
Timestamp:
Jul 24, 2008 4:56:08 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
33676
Message:

VbglR3GuestPropReadValueAlloc: Fixed the same memory leak as in VBoxControl.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGuestProp.cpp

    r10834 r10860  
    243243 * @returns VBox status code.
    244244 * @retval  VINF_SUCCESS on success, *ppszValue containing valid data.
    245  * @retval  VERR_NOT_FOUND if the key wasn't found and *ppszValue set to NULL.
     245 * @retval  VERR_NOT_FOUND if the key wasn't found.
    246246 * @retval  VERR_TOO_MUCH_DATA if we were unable to determine the right size
    247247 *          to allocate for the buffer.  This can happen as the result of a
     
    250250 *
    251251 * @param   u32ClientId     The client id returned by VbglR3ClipboardConnect().
    252  * @param   pszName         The value to read.  Utf8
     252 * @param   pszName         The value to read. Must be valid UTF-8.
    253253 * @param   ppszValue       Where to store the pointer to the value returned.
     254 *                          This is always set to NULL or to the result, even
     255 *                          on failure.
    254256 */
    255257VBGLR3DECL(int) VbglR3GuestPropReadValueAlloc(uint32_t u32ClientId,
     
    257259                                              char **ppszValue)
    258260{
    259     int rc = VINF_SUCCESS;
    260     char *pszValue = NULL;
     261    /*
     262     * Quick input validation.
     263     */
     264    AssertPtr(ppszValue);
    261265    *ppszValue = NULL;
    262 
    263     /* There is a race here between our reading the property size and the
     266    AssertPtrReturn(pszName, VERR_INVALID_PARAMETER);
     267
     268    /*
     269     * There is a race here between our reading the property size and the
    264270     * host changing the value before we read it.  Try up to ten times and
    265      * report the problem if that fails. */
    266     bool finish = false;
    267     /* We leave a bit of space here in case the maximum value is raised. */
    268     uint32_t cchBuf = MAX_VALUE_LEN + 1024;
    269     void *pvBuf = NULL;
    270     for (unsigned i = 0; (i < 10) && !finish; ++i)
    271     {
     271     * report the problem if that fails.
     272     */
     273    char       *pszValue = NULL;
     274    void       *pvBuf    = NULL;
     275    uint32_t    cchBuf   = MAX_VALUE_LEN;
     276    int         rc       = VERR_BUFFER_OVERFLOW;
     277    for (unsigned i = 0; i < 10 && rc == VERR_BUFFER_OVERFLOW; ++i)
     278    {
     279        /* We leave a bit of space here in case the maximum value is raised. */
     280        cchBuf += 1024;
    272281        void *pvTmpBuf = RTMemRealloc(pvBuf, cchBuf);
    273         if (NULL == pvTmpBuf)
    274         {
    275             RTMemFree(pvBuf);
    276             rc = VERR_NO_MEMORY;
    277         }
    278         else
     282        if (pvTmpBuf)
    279283        {
    280284            pvBuf = pvTmpBuf;
     
    282286                                     &pszValue, NULL, NULL, &cchBuf);
    283287        }
    284         if (VERR_BUFFER_OVERFLOW == rc)
    285             /* Leave a bit of extra space to be safe */
    286             cchBuf += 1024;
    287288        else
    288             finish = true;
    289     }
    290     if (VERR_BUFFER_OVERFLOW == rc)
    291         /* VERR_BUFFER_OVERFLOW has a different meaning here as a
    292          * return code, but we need to report the race. */
    293         rc = VERR_TOO_MUCH_DATA;
    294     if (RT_SUCCESS(rc))
     289            rc = VERR_NO_MEMORY;
     290    }
     291    if (RT_SUCCESS(rc))
     292    {
     293        Assert(pszValue == (char *)pvBuf);
    295294        *ppszValue = pszValue;
     295    }
     296    else
     297    {
     298        RTMemFree(pvBuf);
     299        if (rc == VERR_BUFFER_OVERFLOW)
     300            /* VERR_BUFFER_OVERFLOW has a different meaning here as a
     301             * return code, but we need to report the race. */
     302            rc = VERR_TOO_MUCH_DATA;
     303    }
     304
    296305    return rc;
    297306}
     
    300309 * Free the memory used by VbglR3GuestPropReadValueAlloc for returning a
    301310 * value.
    302  * 
     311 *
    303312 * @param pszValue   the memory to be freed.  NULL pointers will be ignored.
    304313 */
Note: See TracChangeset for help on using the changeset viewer.

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