Security identifier “directory” crashes my program

It fails only at the system volumine.

#ifndef UNICODE 
#define UNICODE
#endif

#include <stdio.h>
#include <Windows.h>

void EndWithBackslash(TCHAR* string)
{
    if(string[wcslen(string)-1] != TEXT('\\')) wcscat(string,TEXT("\\"));
}


void Browse(const TCHAR* curdir)
{
    HANDLE hFoundFile;
    WIN32_FIND_DATA foundFileData;
    TCHAR buffer[MAX_PATH];

    wcscpy(buffer,curdir);

    EndWithBackslash(buffer);

    SetCurrentDirectory(buffer);

    hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , NULL);

    if(hFoundFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if ((foundFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(foundFileData.cFileName,TEXT(".")) && wcscmp(foundFileData.cFileName,TEXT("..")) )
            {   
                 TCHAR pszItemPath[MAX_PATH];
                 wcscpy(pszItemPath, buffer);


                EndWithBackslash(pszItemPath);
                wcscat(pszItemPath,foundFileData.cFileName);
                wprintf(TEXT("%s\n"),pszItemPath);

                Browse(pszItemPath);
            }
        }
        while(FindNextFile(hFoundFile,&foundFileData));     
    }
    FindClose(hFoundFile);
}

int main(void) 
{   
    _wsystem(TEXT("chcp 65001"));
    Browse(TEXT("C:\\"));

    _wsystem(TEXT("Pause"));
    return 0;
}

The last lines of output, just before run-time error:

C:\$Recycle.Bin\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5 \S-1-5-20 C:\$Recycle.Bin\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5 \S-1-5-20 C:\$Recycle.Bin\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5 \S-1-5-20

Is there any elegant way to fix it? I would like to avoid deleting this pseudo-directory(Super Users have explained it very well) or setting name filters inside my algorithm.

Answers


Te crash is caused by a stack overflow error because the SetCurrentDirectory failed to change the current directory. As Roman R. stated, you'll need to check the result of SetCurrentDirectory. Moreover, the . and .. subdirectories shouldn't be processed as they refer to the current directory, and the parent directory. Including them in the process would also cause a stack overflow error.

Here's the fixed Browse function.

void Browse(const TCHAR* curdir)
{
    HANDLE hFoundFile;
    WIN32_FIND_DATA foundFileData;
    TCHAR buffer[MAX_PATH];

    wcscpy(buffer,curdir);

    EndWithBackslash(buffer);

    if (!SetCurrentDirectory(buffer)) return;

    hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , NULL);

    if(hFoundFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if ((foundFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (!wcscmp(foundFileData.cFileName,TEXT("."))) && (!wcscmp(foundFileData.cFileName,TEXT(".."))) )
            {   
                 TCHAR pszItemPath[MAX_PATH];
                 wcscpy(pszItemPath, buffer);


                EndWithBackslash(pszItemPath);
                wcscat(pszItemPath,foundFileData.cFileName);
                wprintf(TEXT("%s\n"),pszItemPath);

                Browse(pszItemPath);
            }
        }
        while(FindNextFile(hFoundFile,&foundFileData));     
    }
    FindClose(hFoundFile);
}

Need Your Help

How to preserve the ModelMap between form submits?

java web-applications spring-mvc

I am adding to the ModelMap a Map which contains a list to go on a drop down an I am populating the ModelAttribute when the form initialises:

selectionArgs in SQLiteQueryBuilder doesn't work with integer values in columns

android sql sqlite android-sqlite

I'm trying to select some data from database and I have two slices of code to do it:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.