VirtualBox

Changeset 78153 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Apr 17, 2019 12:30:08 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
130068
Message:

IPRT: Added RTPATHABS_F_ENSURE_TRAILING_SLASH to RTPathAbsEx and fixed a couple of issues related to VERR_BUFFER_OVERFLOW code paths. bugref:9172

Location:
trunk/src/VBox/Runtime/r3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/dir.cpp

    r78050 r78153  
    543543
    544544        cbFilter = cucFilter0 = 0;
    545         pszAbsPath = RTPathAbsDup(pszPath);
     545        pszAbsPath = RTPathAbsExDup(NULL, pszPath, RTPATHABS_F_ENSURE_TRAILING_SLASH);
    546546    }
    547547    else
     
    557557                return VERR_NO_MEMORY;
    558558            pszTmp[pszFilter - pszPath] = '\0';
    559             pszAbsPath = RTPathAbsDup(pszTmp);
     559            pszAbsPath = RTPathAbsExDup(NULL, pszTmp, RTPATHABS_F_ENSURE_TRAILING_SLASH);
    560560            RTStrFree(pszTmp);
    561561        }
    562562        else
    563             pszAbsPath = RTPathAbsDup(".");
     563            pszAbsPath = RTPathAbsExDup(NULL, ".", RTPATHABS_F_ENSURE_TRAILING_SLASH);
    564564        fDirSlash = true;
    565565    }
    566566    if (!pszAbsPath)
    567567        return VERR_NO_MEMORY;
    568 
    569 
     568    Assert(strchr(pszAbsPath, '\0')[-1] == RTPATH_SLASH);
    570569
    571570    /*
     
    576575     */
    577576    size_t const cchAbsPath      = strlen(pszAbsPath);
    578     size_t const cchAbsPathExtra = !RTPATH_IS_SEP(pszAbsPath[cchAbsPath - 1]) ? 1 : 0; /* add trailing '/' if missing */
    579577    size_t const cbDir           = rtDirNativeGetStructSize(pszAbsPath);
    580578    size_t const cbAllocated     = cbDir
    581579                                 + cucFilter0 * sizeof(RTUNICP)
    582580                                 + cbFilter
    583                                  + cchAbsPath + cchAbsPathExtra + 1 + 4;
     581                                 + cchAbsPath + 1 + 4;
    584582    PRTDIRINTERNAL pDir = (PRTDIRINTERNAL)RTMemAllocZ(cbAllocated);
    585583    if (!pDir)
     
    627625            break;
    628626    }
    629     pDir->cchPath       = cchAbsPath + cchAbsPathExtra;
     627    pDir->cchPath       = cchAbsPath;
    630628    pDir->pszPath       = (char *)memcpy(pb, pszAbsPath, cchAbsPath);
    631     pb[cchAbsPath]                   = RTPATH_SLASH;
    632     pb[cchAbsPath + cchAbsPathExtra] = '\0';
    633     Assert(pb - (uint8_t *)pDir + cchAbsPath + cchAbsPathExtra + 1 <= cbAllocated);
     629    pb[cchAbsPath]      = '\0';
     630    Assert(pb - (uint8_t *)pDir + cchAbsPath + 1 <= cbAllocated);
    634631    pDir->pszName       = NULL;
    635632    pDir->cchName       = 0;
  • trunk/src/VBox/Runtime/r3/win/path-win.cpp

    r76553 r78153  
    671671    int rc;
    672672
    673     /*
    674      * GetCurrentDirectory may in some cases omit the drive letter, according
    675      * to MSDN, thus the GetFullPathName call.
    676      */
    677     RTUTF16 wszCurPath[RTPATH_MAX];
    678     if (GetCurrentDirectoryW(RTPATH_MAX, wszCurPath))
    679     {
    680         RTUTF16 wszFullPath[RTPATH_MAX];
    681         if (GetFullPathNameW(wszCurPath, RTPATH_MAX, wszFullPath, NULL))
    682             rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
     673    if (cchPath > 0)
     674    {
     675        /*
     676         * GetCurrentDirectory may in some cases omit the drive letter, according
     677         * to MSDN, thus the GetFullPathName call.
     678         */
     679        RTUTF16 wszCurPath[RTPATH_MAX];
     680        if (GetCurrentDirectoryW(RTPATH_MAX, wszCurPath))
     681        {
     682            RTUTF16 wszFullPath[RTPATH_MAX];
     683            if (GetFullPathNameW(wszCurPath, RTPATH_MAX, wszFullPath, NULL))
     684                rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
     685            else
     686                rc = RTErrConvertFromWin32(GetLastError());
     687        }
    683688        else
    684689            rc = RTErrConvertFromWin32(GetLastError());
    685690    }
    686691    else
    687         rc = RTErrConvertFromWin32(GetLastError());
     692        rc = VERR_BUFFER_OVERFLOW;
    688693    return rc;
    689694}
     
    724729RTDECL(int) RTPathGetCurrentOnDrive(char chDrive, char *pszPath, size_t cbPath)
    725730{
    726     WCHAR wszInput[4];
    727     wszInput[0] = chDrive;
    728     wszInput[1] = ':';
    729     wszInput[2] = '\0';
    730 
    731731    int rc;
    732     RTUTF16 wszFullPath[RTPATH_MAX];
    733     if (GetFullPathNameW(wszInput, RTPATH_MAX, wszFullPath, NULL))
    734         rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cbPath, NULL);
     732    if (cbPath > 0)
     733    {
     734        WCHAR wszInput[4];
     735        wszInput[0] = chDrive;
     736        wszInput[1] = ':';
     737        wszInput[2] = '\0';
     738        RTUTF16 wszFullPath[RTPATH_MAX];
     739        if (GetFullPathNameW(wszInput, RTPATH_MAX, wszFullPath, NULL))
     740            rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cbPath, NULL);
     741        else
     742            rc = RTErrConvertFromWin32(GetLastError());
     743    }
    735744    else
    736         rc = RTErrConvertFromWin32(GetLastError());
     745        rc = VERR_BUFFER_OVERFLOW;
    737746    return rc;
    738747}
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