From 90982fbf1c887a3ed3454f9ab3ab8dfbd57a1383 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Thu, 26 May 2022 17:34:48 -0500 Subject: Add PathConcatRelativeToBase and use it in Burn. Fixes 6707 --- src/libs/dutil/WixToolset.DUtil/inc/pathutil.h | 11 +++++++ src/libs/dutil/WixToolset.DUtil/path2utl.cpp | 40 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) (limited to 'src/libs/dutil/WixToolset.DUtil') diff --git a/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h b/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h index fc6bb3bb..941793f8 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h @@ -236,6 +236,17 @@ DAPI_(HRESULT) PathConcatCch( __deref_out_z LPWSTR* psczCombined ); +/******************************************************************* + PathConcatRelativeToBase - canonicalizes a relative path before + concatenating it to the base path to ensure the resulting path + is inside the base path. +*******************************************************************/ +DAPI_(HRESULT) PathConcatRelativeToBase( + __in LPCWSTR wzBase, + __in_opt LPCWSTR wzRelative, + __deref_out_z LPWSTR* psczCombined + ); + /******************************************************************* PathCompare - compares the fully expanded path of the two paths using ::CompareStringW(). diff --git a/src/libs/dutil/WixToolset.DUtil/path2utl.cpp b/src/libs/dutil/WixToolset.DUtil/path2utl.cpp index 45157d0b..61c1803a 100644 --- a/src/libs/dutil/WixToolset.DUtil/path2utl.cpp +++ b/src/libs/dutil/WixToolset.DUtil/path2utl.cpp @@ -131,6 +131,46 @@ LExit: return hr; } +DAPI_(HRESULT) PathConcatRelativeToBase( + __in LPCWSTR wzBase, + __in_opt LPCWSTR wzRelative, + __deref_out_z LPWSTR* psczCombined + ) +{ + HRESULT hr = S_OK; + LPWSTR sczCanonicalizedRelative = NULL; + + if (!wzBase || !*wzBase) + { + PathExitWithRootFailure(hr, E_INVALIDARG, "wzBase is required."); + } + + if (PathIsRooted(wzRelative)) + { + PathExitWithRootFailure(hr, E_INVALIDARG, "wzRelative cannot be rooted."); + } + + hr = StrAllocString(psczCombined, wzBase, 0); + PathExitOnFailure(hr, "Failed to copy base to output."); + + if (wzRelative && *wzRelative) + { + hr = PathBackslashTerminate(psczCombined); + PathExitOnFailure(hr, "Failed to backslashify."); + + hr = PathCanonicalizeForComparison(wzRelative, 0, &sczCanonicalizedRelative); + PathExitOnFailure(hr, "Failed to canonicalize wzRelative."); + + hr = StrAllocConcat(psczCombined, sczCanonicalizedRelative, 0); + PathExitOnFailure(hr, "Failed to append relative to output."); + } + +LExit: + ReleaseStr(sczCanonicalizedRelative); + + return hr; +} + DAPI_(HRESULT) PathDirectoryContainsPath( __in_z LPCWSTR wzDirectory, __in_z LPCWSTR wzPath -- cgit v1.2.3-55-g6feb