From 21e60122262b00d26593c6bfb548e328d5dbefec Mon Sep 17 00:00:00 2001 From: Norbert Federa Date: Tue, 2 Jun 2015 21:36:31 +0200 Subject: [PATCH] winpr: fix PathCchAddBackslash The HRESULT S_FALSE does not indicate an error: - return E_INVALIDARG instead of S_FALSE - return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) instead of S_FALSE Also extended/fixed the corresponding ctest --- .../path/include/PathCchAddSeparator.c | 8 ++-- .../path/test/TestPathCchAddBackslash.c | 44 ++++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/winpr/libwinpr/path/include/PathCchAddSeparator.c b/winpr/libwinpr/path/include/PathCchAddSeparator.c index fb5670606..cf2b269f7 100644 --- a/winpr/libwinpr/path/include/PathCchAddSeparator.c +++ b/winpr/libwinpr/path/include/PathCchAddSeparator.c @@ -12,7 +12,7 @@ HRESULT PATH_CCH_ADD_SEPARATOR(PWSTR pszPath, size_t cchPath) size_t pszPathLength; if (!pszPath) - return S_FALSE; + return E_INVALIDARG; pszPathLength = lstrlenW(pszPath); @@ -27,7 +27,7 @@ HRESULT PATH_CCH_ADD_SEPARATOR(PWSTR pszPath, size_t cchPath) return S_OK; } - return S_FALSE; + return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); } #else @@ -37,7 +37,7 @@ HRESULT PATH_CCH_ADD_SEPARATOR(PSTR pszPath, size_t cchPath) size_t pszPathLength; if (!pszPath) - return S_FALSE; + return E_INVALIDARG; pszPathLength = lstrlenA(pszPath); @@ -52,7 +52,7 @@ HRESULT PATH_CCH_ADD_SEPARATOR(PSTR pszPath, size_t cchPath) return S_OK; } - return S_FALSE; + return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); } #endif diff --git a/winpr/libwinpr/path/test/TestPathCchAddBackslash.c b/winpr/libwinpr/path/test/TestPathCchAddBackslash.c index a4e4a6df8..42f8f74d8 100644 --- a/winpr/libwinpr/path/test/TestPathCchAddBackslash.c +++ b/winpr/libwinpr/path/test/TestPathCchAddBackslash.c @@ -13,6 +13,12 @@ int TestPathCchAddBackslash(int argc, char* argv[]) HRESULT status; TCHAR Path[PATHCCH_MAX_CCH]; + /** + * PathCchAddBackslash returns S_OK if the function was successful, + * S_FALSE if the path string already ends in a backslash, + * or an error code otherwise. + */ + _tcscpy(Path, testPathNoBackslash); /* Add a backslash to a path without a trailing backslash, expect S_OK */ @@ -21,7 +27,7 @@ int TestPathCchAddBackslash(int argc, char* argv[]) if (status != S_OK) { - _tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), (int) status); + _tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), (unsigned) status); return -1; } @@ -39,7 +45,7 @@ int TestPathCchAddBackslash(int argc, char* argv[]) if (status != S_FALSE) { - _tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), (int) status); + _tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), (unsigned) status); return -1; } @@ -49,6 +55,40 @@ int TestPathCchAddBackslash(int argc, char* argv[]) return -1; } + /* Use NULL PSTR, expect FAILED(status) */ + + status = PathCchAddBackslash(NULL, PATHCCH_MAX_CCH); + + if (SUCCEEDED(status)) + { + _tprintf(_T("PathCchAddBackslash unexpectedly succeded with null buffer. Status: 0x%08X\n"), (unsigned) status); + return -1; + } + + /* Use insufficient size value, expect FAILED(status) */ + + _tcscpy(Path, _T("C:\\tmp")); + + status = PathCchAddBackslash(Path, 7); + + if (SUCCEEDED(status)) + { + _tprintf(_T("PathCchAddBackslash unexpectedly succeded with insufficient buffer size. Status: 0x%08X\n"), (unsigned) status); + return -1; + } + + /* Use minimum required size value, expect S_OK */ + + _tcscpy(Path, _T("C:\\tmp")); + + status = PathCchAddBackslash(Path, 8); + + if (status != S_OK) + { + _tprintf(_T("PathCchAddBackslash failed with status: 0x%08X\n"), (unsigned) status); + return -1; + } + return 0; }