VirtualBox

Changeset 103434 in vbox


Ignore:
Timestamp:
Feb 19, 2024 12:35:55 PM (14 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
161763
Message:

tstShflCase: You cannot use RTTESTI_CHECK_RC_OK w/o first calling RTTestCreate, and besides if stuff overflows there is no point in calling testCase() as it won't do what you expect. The first changes to vbsfCorrectCasing didn't take into account that pDirEntry was allocated oversized. The second changes completely ignored that the length of the component that is being case corrected cannot change length, so there is actually no reason for passing the buffer size along. bugref:3409

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/SharedFolders/testcase/tstShflCase.cpp

    r103285 r103434  
    209209}
    210210
    211 static int vbsfCorrectCasing(char *pszFullPath, char *pszStartComponent, size_t cbStartComponent)
    212 {
    213     PRTDIRENTRYEX  pDirEntry = NULL;
    214     uint32_t       cbDirEntry;
    215     size_t         cbComponent;
    216     int            rc = VERR_FILE_NOT_FOUND;
    217     RTDIR          hSearch = NIL_RTDIR;
    218     char           szWildCard[4];
    219 
    220     Log2(("vbsfCorrectCasing: %s %s\n", pszFullPath, pszStartComponent));
    221 
    222     cbComponent = strlen(pszStartComponent);
    223 
    224     cbDirEntry = 4096;
    225     pDirEntry  = (PRTDIRENTRYEX)RTMemAlloc(cbDirEntry);
    226     if (pDirEntry == 0)
    227     {
    228         AssertFailed();
    229         return VERR_NO_MEMORY;
    230     }
     211static int vbsfCorrectCasing(char *pszFullPath, char *pszStartComponent)
     212{
     213    size_t const   cchComponent   = strlen(pszStartComponent);
     214
     215    Log2(("vbsfCorrectCasing: '%s' '%s' (%zu)\n", pszFullPath, pszStartComponent, cchComponent));
     216
     217    uint32_t const cbDirEntry     = 4096;
     218    uint32_t const cbDirEntryName = cbDirEntry - RT_UOFFSETOF(RTDIRENTRYEX, szName);
     219    PRTDIRENTRYEX  pDirEntry  = (PRTDIRENTRYEX)RTMemAlloc(cbDirEntry);
     220    AssertReturn(pDirEntry, VERR_NO_MEMORY);
    231221
    232222    /** @todo this is quite inefficient, especially for directories with many files */
    233     Assert(pszFullPath < pszStartComponent-1);
    234     Assert(*(pszStartComponent-1) == RTPATH_DELIMITER);
    235     *(pszStartComponent-1) = 0;
    236     rc = RTStrCopy(pDirEntry->szName, sizeof(pDirEntry->szName), pszFullPath);
    237     if (RT_FAILURE(rc))
    238         goto end;
    239     szWildCard[0] = RTPATH_DELIMITER;
    240     szWildCard[1] = '*';
    241     szWildCard[2] = 0;
    242     rc = RTStrCat(pDirEntry->szName, sizeof(pDirEntry->szName), szWildCard);
    243     if (RT_FAILURE(rc))
    244         goto end;
    245     rc = RTDirOpenFiltered(&hSearch, pDirEntry->szName, RTDIRFILTER_WINNT, 0 /*fFlags*/);
    246     *(pszStartComponent-1) = RTPATH_DELIMITER;
    247     if (RT_FAILURE(rc))
    248         goto end;
    249 
    250     for(;;)
    251     {
    252         size_t cbDirEntrySize = cbDirEntry;
    253 
    254         rc = RTDirReadEx(hSearch, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
    255         if (rc == VERR_NO_MORE_FILES)
    256             break;
    257 
    258         if (VINF_SUCCESS != rc && rc != VWRN_NO_DIRENT_INFO)
     223    Assert(pszFullPath < pszStartComponent - 1);
     224    Assert(pszStartComponent[-1] == RTPATH_DELIMITER);
     225    pszStartComponent[-1] = '\0';
     226    int rc = RTStrCopy(pDirEntry->szName, cbDirEntryName - sizeof(RTPATH_SLASH_STR "*"), pszFullPath);
     227    pszStartComponent[-1] = RTPATH_DELIMITER;
     228    if (RT_SUCCESS(rc))
     229    {
     230        char szWildCard[4];
     231        szWildCard[0] = RTPATH_DELIMITER;
     232        szWildCard[1] = '*';
     233        szWildCard[2] = '\0';
     234        rc = RTStrCat(pDirEntry->szName, cbDirEntryName, szWildCard);
     235    }
     236    if (RT_SUCCESS(rc))
     237    {
     238        RTDIR hSearch = NIL_RTDIR;
     239        rc = RTDirOpenFiltered(&hSearch, pDirEntry->szName, RTDIRFILTER_WINNT, 0 /*fFlags*/);
     240        if (RT_SUCCESS(rc))
    259241        {
    260             AssertFailed();
    261             if (rc != VERR_NO_TRANSLATION)
    262                 break;
    263             else
    264                 continue;
     242            for(;;)
     243            {
     244                size_t cbDirEntryRet = cbDirEntry;
     245                rc = RTDirReadEx(hSearch, pDirEntry, &cbDirEntryRet, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
     246                if (rc == VERR_NO_MORE_FILES)
     247                    break;
     248
     249                if (VINF_SUCCESS != rc && rc != VWRN_NO_DIRENT_INFO)
     250                {
     251                    AssertFailed();
     252                    if (rc != VERR_NO_TRANSLATION)
     253                        break;
     254                    continue;
     255                }
     256
     257                Log2(("vbsfCorrectCasing: found %s\n", &pDirEntry->szName[0]));
     258                if (   pDirEntry->cbName == cchComponent
     259                    && !RTStrICmp(pszStartComponent, &pDirEntry->szName[0]))
     260                {
     261                    Log(("Found original name %s (%s)\n", &pDirEntry->szName[0], pszStartComponent));
     262                    memcpy(pszStartComponent, &pDirEntry->szName[0], cchComponent);
     263                    pszStartComponent[cchComponent] = '\0'; /* paranoia */
     264                    rc = VINF_SUCCESS;
     265                    break;
     266                }
     267            }
     268            if (RT_FAILURE(rc))
     269                Log(("vbsfCorrectCasing %s failed with %d\n", pszStartComponent, rc));
     270
     271            RTDirClose(hSearch);
    265272        }
    266 
    267         Log2(("vbsfCorrectCasing: found %s\n", &pDirEntry->szName[0]));
    268         if (    pDirEntry->cbName == cbComponent
    269             &&  !RTStrICmp(pszStartComponent, &pDirEntry->szName[0]))
    270         {
    271             Log(("Found original name %s (%s)\n", &pDirEntry->szName[0], pszStartComponent));
    272             rc = RTStrCopy(pszStartComponent, cbStartComponent, &pDirEntry->szName[0]);
    273             break;
    274         }
    275     }
    276     if (RT_FAILURE(rc))
    277         Log(("vbsfCorrectCasing %s failed with %d\n", pszStartComponent, rc));
    278 
    279 end:
    280     if (pDirEntry)
    281         RTMemFree(pDirEntry);
    282 
    283     if (hSearch)
    284         RTDirClose(hSearch);
     273    }
     274
     275    RTMemFree(pDirEntry);
    285276    return rc;
    286277}
     
    288279
    289280
    290 static int testCase(char *pszFullPath, size_t cbFullPath, bool fWildCard = false)
     281static int testCase(char *pszFullPath, bool fWildCard = false)
    291282{
    292283    int rc;
     
    334325    if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
    335326    {
    336         size_t cchLen = strlen(pszFullPath);
    337         char  *pszSrc = pszFullPath + cchLen - 1;
     327        char *pszSrc = &pszFullPath[strlen(pszFullPath) - 1];
    338328
    339329        Log(("Handle case insensitive guest fs on top of host case sensitive fs for %s\n", pszFullPath));
    340330
    341331        /* Find partial path that's valid */
    342         while(pszSrc > pszFullPath)
     332        while (pszSrc > pszFullPath)
    343333        {
    344334            if (*pszSrc == RTPATH_DELIMITER)
    345335            {
    346                 *pszSrc = 0;
     336                *pszSrc = '\0';
    347337                rc = RTPathQueryInfo (pszFullPath, &info, RTFSOBJATTRADD_NOTHING);
    348338                *pszSrc = RTPATH_DELIMITER;
     
    350340                {
    351341#ifdef DEBUG
    352                     *pszSrc = 0;
     342                    *pszSrc = '\0';
    353343                    Log(("Found valid partial path %s\n", pszFullPath));
    354344                    *pszSrc = RTPATH_DELIMITER;
     
    384374                    Assert(rc == VINF_SUCCESS || rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND);
    385375                }
    386                 else
    387                 if (end == pszSrc)
     376                else if (end == pszSrc)
    388377                    rc = VINF_SUCCESS;  /* trailing delimiter */
    389378                else
     
    393382                {
    394383                    /* path component is invalid; try to correct the casing */
    395                     rc = vbsfCorrectCasing(pszFullPath, pszSrc, cbFullPath - (pszFullPath - pszSrc));
     384                    rc = vbsfCorrectCasing(pszFullPath, pszSrc);
    396385                    if (RT_FAILURE(rc))
    397386                    {
    398387                        if (!fEndOfString)
    399                               *end = RTPATH_DELIMITER;
     388                            *end = RTPATH_DELIMITER;
    400389                        break;
    401390                    }
     
    428417int main()
    429418{
    430     char szTest[128];
    431 
    432419    RTR3InitExeNoArguments(0);
    433420    RTLogFlush(NULL);
     
    436423    RTLogFlags(NULL, "unbuffered");
    437424
    438     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\test Dir\\z.bAt"));
    439     testCase(szTest, sizeof(szTest));
    440     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\test dir\\z.bAt"));
    441     testCase(szTest, sizeof(szTest));
    442     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\test dir\\SUBDIR\\z.bAt"));
    443     testCase(szTest, sizeof(szTest));
    444     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\test dir\\SUBDiR\\atestje.bat"));
    445     testCase(szTest, sizeof(szTest));
    446     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\TEST dir\\subDiR\\aTestje.baT"));
    447     testCase(szTest, sizeof(szTest));
    448     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\TEST dir\\subDiR\\*"));
    449     testCase(szTest, sizeof(szTest), true);
    450     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\TEST dir\\subDiR\\"));
    451     testCase(szTest ,sizeof(szTest), true);
    452     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\test dir\\SUBDIR\\"));
    453     testCase(szTest, sizeof(szTest));
    454     RTTESTI_CHECK_RC_OK(RTStrCopy(szTest, sizeof(szTest), "c:\\test dir\\invalid\\SUBDIR\\test.bat"));
    455     testCase(szTest, sizeof(szTest));
     425    char szTest[128];
     426#define SET_SZ_TEST(a_szStr) do { \
     427        AssertCompile(sizeof(a_szStr) < sizeof(szTest)); \
     428        memcpy(szTest, a_szStr, sizeof(a_szStr)); \
     429    } while (0)
     430
     431    SET_SZ_TEST("c:\\test Dir\\z.bAt");
     432    testCase(szTest);
     433    SET_SZ_TEST("c:\\test dir\\z.bAt");
     434    testCase(szTest);
     435    SET_SZ_TEST("c:\\test dir\\SUBDIR\\z.bAt");
     436    testCase(szTest);
     437    SET_SZ_TEST("c:\\test dir\\SUBDiR\\atestje.bat");
     438    testCase(szTest);
     439    SET_SZ_TEST("c:\\TEST dir\\subDiR\\aTestje.baT");
     440    testCase(szTest);
     441    SET_SZ_TEST("c:\\TEST dir\\subDiR\\*");
     442    testCase(szTest, true);
     443    SET_SZ_TEST("c:\\TEST dir\\subDiR\\");
     444    testCase(szTest, true);
     445    SET_SZ_TEST("c:\\test dir\\SUBDIR\\");
     446    testCase(szTest);
     447    SET_SZ_TEST("c:\\test dir\\invalid\\SUBDIR\\test.bat");
     448    testCase(szTest);
    456449    return 0;
    457450}
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