From 12517aa3099bac9294cfb4fd0df02f6a3bb3af78 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Mon, 25 Jun 2018 09:53:45 +0900 Subject: [PATCH] [CMD] Create a DirNodeCleanup() helper for cleanup and simplify code (adapted from PR #592). --- base/shell/cmd/dir.c | 79 +++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/base/shell/cmd/dir.c b/base/shell/cmd/dir.c index 07d14d6f93f..a44f9aca68b 100644 --- a/base/shell/cmd/dir.c +++ b/base/shell/cmd/dir.c @@ -1314,6 +1314,27 @@ QsortFiles(PDIRFINDINFO ptrArray[], /* [IN/OUT] The array with file info } } +static VOID +DirNodeCleanup(PDIRFINDLISTNODE ptrStartNode, + PDWORD pdwCount) +{ + PDIRFINDLISTNODE ptrNextNode; + PDIRFINDSTREAMNODE ptrFreeNode; + while (ptrStartNode) + { + ptrNextNode = ptrStartNode->ptrNext; + while (ptrStartNode->stInfo.ptrHead) + { + ptrFreeNode = ptrStartNode->stInfo.ptrHead; + ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext; + cmd_free(ptrFreeNode); + } + cmd_free(ptrStartNode); + ptrStartNode = ptrNextNode; + --(*pdwCount); + } +} + /* * DirList * @@ -1341,7 +1362,6 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */ ULARGE_INTEGER u64Temp; /* A temporary counter */ WIN32_FIND_STREAM_DATA wfsdStreamInfo; PDIRFINDSTREAMNODE * ptrCurNode; /* The pointer to the first stream */ - PDIRFINDSTREAMNODE ptrFreeNode; /* The pointer used during cleanup */ static HANDLE (WINAPI *pFindFirstStreamW)(LPCWSTR, STREAM_INFO_LEVELS, LPVOID, DWORD); static BOOL (WINAPI *pFindNextStreamW)(HANDLE, LPVOID); @@ -1404,19 +1424,7 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */ if (ptrNextNode->ptrNext == NULL) { WARN("DEBUG: Cannot allocate memory for ptrNextNode->ptrNext!\n"); - while (ptrStartNode) - { - ptrNextNode = ptrStartNode->ptrNext; - while (ptrStartNode->stInfo.ptrHead) - { - ptrFreeNode = ptrStartNode->stInfo.ptrHead; - ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext; - cmd_free(ptrFreeNode); - } - cmd_free(ptrStartNode); - ptrStartNode = ptrNextNode; - dwCount--; - } + DirNodeCleanup(ptrStartNode, &dwCount); FindClose(hSearch); return 1; } @@ -1466,19 +1474,7 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */ if (*ptrCurNode == NULL) { WARN("DEBUG: Cannot allocate memory for *ptrCurNode!\n"); - while (ptrStartNode) - { - ptrNextNode = ptrStartNode->ptrNext; - while (ptrStartNode->stInfo.ptrHead) - { - ptrFreeNode = ptrStartNode->stInfo.ptrHead; - ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext; - cmd_free(ptrFreeNode); - } - cmd_free(ptrStartNode); - ptrStartNode = ptrNextNode; - dwCount--; - } + DirNodeCleanup(ptrStartNode, &dwCount); FindClose(hStreams); FindClose(hSearch); return 1; @@ -1532,19 +1528,7 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */ if (ptrFileArray == NULL) { WARN("DEBUG: Cannot allocate memory for ptrFileArray!\n"); - while (ptrStartNode) - { - ptrNextNode = ptrStartNode->ptrNext; - while (ptrStartNode->stInfo.ptrHead) - { - ptrFreeNode = ptrStartNode->stInfo.ptrHead; - ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext; - cmd_free(ptrFreeNode); - } - cmd_free(ptrStartNode); - ptrStartNode = ptrNextNode; - dwCount --; - } + DirNodeCleanup(ptrStartNode, &dwCount); return 1; } @@ -1582,20 +1566,9 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */ /* Free array */ cmd_free(ptrFileArray); + /* Free linked list */ - while (ptrStartNode) - { - ptrNextNode = ptrStartNode->ptrNext; - while (ptrStartNode->stInfo.ptrHead) - { - ptrFreeNode = ptrStartNode->stInfo.ptrHead; - ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext; - cmd_free(ptrFreeNode); - } - cmd_free(ptrStartNode); - ptrStartNode = ptrNextNode; - dwCount --; - } + DirNodeCleanup(ptrStartNode, &dwCount); if (CheckCtrlBreak(BREAK_INPUT)) return 1;