aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/pathutil.cpp
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2021-04-28 16:36:56 -0500
committerSean Hall <r.sean.hall@gmail.com>2021-04-29 13:58:14 -0500
commitbcd3ee7ab858d62beb36af9f5986544b68a3dd35 (patch)
tree424c4e61a580b7c4b7481712f69ab0193d76b9c4 /src/dutil/pathutil.cpp
parentd73c29407fe5ec6a0207af7d9c2547457ae0854c (diff)
downloadwix-bcd3ee7ab858d62beb36af9f5986544b68a3dd35.tar.gz
wix-bcd3ee7ab858d62beb36af9f5986544b68a3dd35.tar.bz2
wix-bcd3ee7ab858d62beb36af9f5986544b68a3dd35.zip
Clean up more 32-bit assumptions.
Diffstat (limited to 'src/dutil/pathutil.cpp')
-rw-r--r--src/dutil/pathutil.cpp58
1 files changed, 42 insertions, 16 deletions
diff --git a/src/dutil/pathutil.cpp b/src/dutil/pathutil.cpp
index 183849ac..7c3cfe06 100644
--- a/src/dutil/pathutil.cpp
+++ b/src/dutil/pathutil.cpp
@@ -181,7 +181,7 @@ DAPI_(HRESULT) PathGetDirectory(
181 ) 181 )
182{ 182{
183 HRESULT hr = S_OK; 183 HRESULT hr = S_OK;
184 DWORD cchDirectory = DWORD_MAX; 184 size_t cchDirectory = SIZE_T_MAX;
185 185
186 for (LPCWSTR wz = wzPath; *wz; ++wz) 186 for (LPCWSTR wz = wzPath; *wz; ++wz)
187 { 187 {
@@ -191,11 +191,11 @@ DAPI_(HRESULT) PathGetDirectory(
191 // : => relative path from mapped root 191 // : => relative path from mapped root
192 if (L'\\' == *wz || L'/' == *wz || (L':' == *wz && wz == wzPath + 1)) 192 if (L'\\' == *wz || L'/' == *wz || (L':' == *wz && wz == wzPath + 1))
193 { 193 {
194 cchDirectory = static_cast<DWORD>(wz - wzPath) + 1; 194 cchDirectory = static_cast<size_t>(wz - wzPath) + 1;
195 } 195 }
196 } 196 }
197 197
198 if (DWORD_MAX == cchDirectory) 198 if (SIZE_T_MAX == cchDirectory)
199 { 199 {
200 // we were given just a file name, so there's no directory available 200 // we were given just a file name, so there's no directory available
201 return S_FALSE; 201 return S_FALSE;
@@ -233,7 +233,7 @@ DAPI_(HRESULT) PathGetParentPath(
233 233
234 if (wzParent) 234 if (wzParent)
235 { 235 {
236 DWORD cchPath = static_cast<DWORD>(wzParent - wzPath) + 1; 236 size_t cchPath = static_cast<size_t>(wzParent - wzPath) + 1;
237 237
238 hr = StrAllocString(psczParent, wzPath, cchPath); 238 hr = StrAllocString(psczParent, wzPath, cchPath);
239 PathExitOnFailure(hr, "Failed to copy directory."); 239 PathExitOnFailure(hr, "Failed to copy directory.");
@@ -260,6 +260,7 @@ DAPI_(HRESULT) PathExpand(
260 DWORD cch = 0; 260 DWORD cch = 0;
261 LPWSTR sczExpandedPath = NULL; 261 LPWSTR sczExpandedPath = NULL;
262 DWORD cchExpandedPath = 0; 262 DWORD cchExpandedPath = 0;
263 SIZE_T cbSize = 0;
263 264
264 LPWSTR sczFullPath = NULL; 265 LPWSTR sczFullPath = NULL;
265 266
@@ -305,8 +306,10 @@ DAPI_(HRESULT) PathExpand(
305 } 306 }
306 PathExitOnFailure(hr, "Failed to prefix long path after expanding environment variables."); 307 PathExitOnFailure(hr, "Failed to prefix long path after expanding environment variables.");
307 308
308 hr = StrMaxLength(sczExpandedPath, reinterpret_cast<DWORD_PTR *>(&cchExpandedPath)); 309 hr = StrMaxLength(sczExpandedPath, &cbSize);
309 PathExitOnFailure(hr, "Failed to get max length of expanded path."); 310 PathExitOnFailure(hr, "Failed to get max length of expanded path.");
311
312 cchExpandedPath = (DWORD)min(DWORD_MAX, cbSize);
310 } 313 }
311 } 314 }
312 315
@@ -317,7 +320,7 @@ DAPI_(HRESULT) PathExpand(
317 { 320 {
318 LPWSTR wzFileName = NULL; 321 LPWSTR wzFileName = NULL;
319 LPCWSTR wzPath = sczExpandedPath ? sczExpandedPath : wzRelativePath; 322 LPCWSTR wzPath = sczExpandedPath ? sczExpandedPath : wzRelativePath;
320 DWORD cchFullPath = PATH_GOOD_ENOUGH < cchExpandedPath ? cchExpandedPath : PATH_GOOD_ENOUGH; 323 DWORD cchFullPath = max(PATH_GOOD_ENOUGH, cchExpandedPath);
321 324
322 hr = StrAlloc(&sczFullPath, cchFullPath); 325 hr = StrAlloc(&sczFullPath, cchFullPath);
323 PathExitOnFailure(hr, "Failed to allocate space for full path."); 326 PathExitOnFailure(hr, "Failed to allocate space for full path.");
@@ -836,8 +839,7 @@ DAPI_(BOOL) PathIsAbsolute(
836 __in_z LPCWSTR wzPath 839 __in_z LPCWSTR wzPath
837 ) 840 )
838{ 841{
839 DWORD dwLength = lstrlenW(wzPath); 842 return wzPath && wzPath[0] && wzPath[1] && (wzPath[0] == L'\\') || (wzPath[1] == L':');
840 return (1 < dwLength) && (wzPath[0] == L'\\') || (wzPath[1] == L':');
841} 843}
842 844
843 845
@@ -847,27 +849,39 @@ DAPI_(HRESULT) PathConcat(
847 __deref_out_z LPWSTR* psczCombined 849 __deref_out_z LPWSTR* psczCombined
848 ) 850 )
849{ 851{
852 return PathConcatCch(wzPath1, 0, wzPath2, 0, psczCombined);
853}
854
855
856DAPI_(HRESULT) PathConcatCch(
857 __in_opt LPCWSTR wzPath1,
858 __in SIZE_T cchPath1,
859 __in_opt LPCWSTR wzPath2,
860 __in SIZE_T cchPath2,
861 __deref_out_z LPWSTR* psczCombined
862 )
863{
850 HRESULT hr = S_OK; 864 HRESULT hr = S_OK;
851 865
852 if (!wzPath2 || !*wzPath2) 866 if (!wzPath2 || !*wzPath2)
853 { 867 {
854 hr = StrAllocString(psczCombined, wzPath1, 0); 868 hr = StrAllocString(psczCombined, wzPath1, cchPath1);
855 PathExitOnFailure(hr, "Failed to copy just path1 to output."); 869 PathExitOnFailure(hr, "Failed to copy just path1 to output.");
856 } 870 }
857 else if (!wzPath1 || !*wzPath1 || PathIsAbsolute(wzPath2)) 871 else if (!wzPath1 || !*wzPath1 || PathIsAbsolute(wzPath2))
858 { 872 {
859 hr = StrAllocString(psczCombined, wzPath2, 0); 873 hr = StrAllocString(psczCombined, wzPath2, cchPath2);
860 PathExitOnFailure(hr, "Failed to copy just path2 to output."); 874 PathExitOnFailure(hr, "Failed to copy just path2 to output.");
861 } 875 }
862 else 876 else
863 { 877 {
864 hr = StrAllocString(psczCombined, wzPath1, 0); 878 hr = StrAllocString(psczCombined, wzPath1, cchPath1);
865 PathExitOnFailure(hr, "Failed to copy path1 to output."); 879 PathExitOnFailure(hr, "Failed to copy path1 to output.");
866 880
867 hr = PathBackslashTerminate(psczCombined); 881 hr = PathBackslashTerminate(psczCombined);
868 PathExitOnFailure(hr, "Failed to backslashify."); 882 PathExitOnFailure(hr, "Failed to backslashify.");
869 883
870 hr = StrAllocConcat(psczCombined, wzPath2, 0); 884 hr = StrAllocConcat(psczCombined, wzPath2, cchPath2);
871 PathExitOnFailure(hr, "Failed to append path2 to output."); 885 PathExitOnFailure(hr, "Failed to append path2 to output.");
872 } 886 }
873 887
@@ -1001,15 +1015,25 @@ DAPI_(HRESULT) PathGetHierarchyArray(
1001 LPWSTR sczPathCopy = NULL; 1015 LPWSTR sczPathCopy = NULL;
1002 LPWSTR sczNewPathCopy = NULL; 1016 LPWSTR sczNewPathCopy = NULL;
1003 DWORD cArraySpacesNeeded = 0; 1017 DWORD cArraySpacesNeeded = 0;
1018 size_t cchPath = 0;
1019
1020 hr = ::StringCchLengthW(wzPath, STRSAFE_MAX_LENGTH, &cchPath);
1021 PathExitOnRootFailure(hr, "Failed to get string length of path: %ls", wzPath);
1022
1023 if (!cchPath)
1024 {
1025 ExitFunction1(hr = E_INVALIDARG);
1026 }
1004 1027
1005 for (int i = 0; i < lstrlenW(wzPath); ++i) 1028 for (size_t i = 0; i < cchPath; ++i)
1006 { 1029 {
1007 if (wzPath[i] == L'\\') 1030 if (wzPath[i] == L'\\')
1008 { 1031 {
1009 ++cArraySpacesNeeded; 1032 ++cArraySpacesNeeded;
1010 } 1033 }
1011 } 1034 }
1012 if (wzPath[lstrlenW(wzPath) - 1] != L'\\') 1035
1036 if (wzPath[cchPath - 1] != L'\\')
1013 { 1037 {
1014 ++cArraySpacesNeeded; 1038 ++cArraySpacesNeeded;
1015 } 1039 }
@@ -1034,10 +1058,12 @@ DAPI_(HRESULT) PathGetHierarchyArray(
1034 hr = StrAllocString((*prgsczPathArray) + cArraySpacesNeeded - 1 - i, sczPathCopy, 0); 1058 hr = StrAllocString((*prgsczPathArray) + cArraySpacesNeeded - 1 - i, sczPathCopy, 0);
1035 PathExitOnFailure(hr, "Failed to copy path"); 1059 PathExitOnFailure(hr, "Failed to copy path");
1036 1060
1061 DWORD cchPathCopy = lstrlenW(sczPathCopy);
1062
1037 // If it ends in a backslash, it's a directory path, so cut off everything the last backslash before we get the directory portion of the path 1063 // If it ends in a backslash, it's a directory path, so cut off everything the last backslash before we get the directory portion of the path
1038 if (wzPath[lstrlenW(sczPathCopy) - 1] == L'\\') 1064 if (wzPath[cchPathCopy - 1] == L'\\')
1039 { 1065 {
1040 sczPathCopy[lstrlenW(sczPathCopy) - 1] = L'\0'; 1066 sczPathCopy[cchPathCopy - 1] = L'\0';
1041 } 1067 }
1042 1068
1043 hr = PathGetDirectory(sczPathCopy, &sczNewPathCopy); 1069 hr = PathGetDirectory(sczPathCopy, &sczNewPathCopy);