VirtualBox

Changeset 9355 in vbox for trunk/src


Ignore:
Timestamp:
Jun 3, 2008 3:32:05 PM (17 years ago)
Author:
vboxsync
Message:

d_name can be defined as <= MAX_PATH (solaris does this). Clearified some of the RTDIR allocation code.

Location:
trunk/src/VBox/Runtime
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/include/internal/dir.h

    r8245 r9355  
    101101    /** What opendir() returned. */
    102102    DIR                *pDir;
     103    /** The max size of the d_name member.
     104     * This includes the 0 terminator of course.*/
     105    size_t              cbMaxName;
    103106    /** Find data buffer.
    104107     * fDataUnread indicates valid data. */
  • trunk/src/VBox/Runtime/r3/dir.cpp

    r8245 r9355  
    3434*******************************************************************************/
    3535#define LOG_GROUP RTLOGGROUP_DIR
    36 #ifdef RT_OS_WINDOWS
     36#ifdef RT_OS_WINDOWS /* PORTME: Assumes everyone else is using dir-posix.cpp */
    3737# include <Windows.h>
    3838#else
    3939# include <dirent.h>
     40# include <unistd.h>
    4041#endif
    4142
     
    528529    char szRealPath[RTPATH_MAX + 1];
    529530    int rc;
    530     size_t cchFilter;                   /* includes '\0'. */
    531     size_t cucFilter;                   /* includes U+0. */
     531    size_t cbFilter;                    /* includes '\0' (thus cb and not cch). */
     532    size_t cucFilter0;                  /* includes U+0. */
    532533    if (!pszFilter)
    533534    {
    534         cchFilter = cucFilter = 0;
     535        cbFilter = cucFilter0 = 0;
    535536        rc = RTPathReal(pszPath, szRealPath, sizeof(szRealPath) - 1);
    536537    }
    537538    else
    538539    {
    539         cchFilter = strlen(pszFilter) + 1;
    540         cucFilter = RTStrUniLen(pszFilter) + 1;
     540        cbFilter = strlen(pszFilter) + 1;
     541        cucFilter0 = RTStrUniLen(pszFilter) + 1;
    541542
    542543        if (pszFilter != pszPath)
     
    566567    /*
    567568     * Allocate and initialize the directory handle.
    568      */
    569     PRTDIR pDir = (PRTDIR)RTMemAlloc(sizeof(RTDIR) + cchRealPath + 1 + 4 + cchFilter + cucFilter * sizeof(RTUNICP));
     569     *
     570     * The posix definition of Data.d_name allows it to be < NAME_MAX + 1,
     571     * thus the horrible uglyness here. Solaris uses d_name[1] for instance.
     572     */
     573#ifndef RT_OS_WINDOWS
     574    long cbNameMax = pathconf(szRealPath, _PC_NAME_MAX);
     575    if (cbNameMax < NAME_MAX)           /* This is plain paranoia, but it doesn't hurt. */
     576        cbNameMax = NAME_MAX;
     577    size_t cbDir = RT_OFFSETOF(RTDIR, Data.d_name[cbNameMax + 1]);
     578    if (cbDir < sizeof(RTDIR))          /* Ditto. */
     579        cbDir = sizeof(RTDIR);
     580    cbDir = RT_ALIGN_Z(cbDir, 8);
     581#else
     582    size_t cbDir = sizeof(RTDIR);
     583#endif
     584    size_t const cbAllocated = cbDir
     585                             + cucFilter0 * sizeof(RTUNICP)
     586                             + cbFilter
     587                             + cchRealPath + 1 + 4;
     588    PRTDIR pDir = (PRTDIR)RTMemAlloc(cbAllocated);
    570589    if (!pDir)
    571590        return VERR_NO_MEMORY;
     591    uint8_t *pb = (uint8_t *)pDir + cbDir;
    572592
    573593    /* initialize it */
    574594    pDir->u32Magic = RTDIR_MAGIC;
    575     if (cchFilter)
    576     {
    577         pDir->puszFilter = (PRTUNICP)(pDir + 1);
    578         rc = RTStrToUniEx(pszFilter, RTSTR_MAX, &pDir->puszFilter, cucFilter, &pDir->cucFilter);
     595    if (cbFilter)
     596    {
     597        pDir->puszFilter = (PRTUNICP)pb;
     598        rc = RTStrToUniEx(pszFilter, RTSTR_MAX, &pDir->puszFilter, cucFilter0, &pDir->cucFilter);
    579599        AssertRC(rc);
    580         pDir->pszFilter = (char *)memcpy(pDir->puszFilter + cucFilter, pszFilter, cchFilter);
    581         pDir->cchFilter = cchFilter - 1;
     600        pb += cucFilter0 * sizeof(RTUNICP);
     601        pDir->pszFilter = (char *)memcpy(pb, pszFilter, cbFilter);
     602        pDir->cchFilter = cbFilter - 1;
     603        pb += cbFilter;
    582604    }
    583605    else
     
    606628    }
    607629    pDir->cchPath = cchRealPath;
    608     pDir->pszPath = (char *)memcpy((char *)(pDir + 1) + cucFilter * sizeof(RTUNICP) + cchFilter,
    609                                    szRealPath, cchRealPath + 1);
     630    pDir->pszPath = (char *)memcpy(pb, szRealPath, cchRealPath + 1);
     631    Assert(pb - (uint8_t *)pDir + cchRealPath + 1 <= cbAllocated);
    610632    pDir->fDataUnread = false;
    611633#ifndef RT_DONT_CONVERT_FILENAMES
    612634    pDir->pszName = NULL;
    613635    pDir->cchName = 0;
     636#endif
     637#ifndef RT_OS_WINDOWS
     638    pDir->cbMaxName = cbDir - RT_OFFSETOF(RTDIR, Data.d_name);
    614639#endif
    615640
  • trunk/src/VBox/Runtime/r3/posix/dir-posix.cpp

    r8245 r9355  
    143143             */
    144144            pDir->fDataUnread = false;
    145             memset(&pDir->Data, 0, sizeof(pDir->Data)); /* not strictly necessary */
     145            memset(&pDir->Data, 0, RT_OFFSETOF(RTDIR, Data.d_name)); /* not strictly necessary */
     146            memset(&pDir->Data.d_name[0], 0, pDir->cbMaxName);
    146147        }
    147148        else
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