Skip to content

Commit

Permalink
Update the 7-Zip sanity checking to check for PathExpand failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnlaan committed Nov 2, 2024
1 parent 4420840 commit bb0a079
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 11 additions & 5 deletions Components/PathFunc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function PathCompare(const S1, S2: String): Integer;
function PathDrivePartLength(const Filename: String): Integer;
function PathDrivePartLengthEx(const Filename: String;
const IncludeSignificantSlash: Boolean): Integer;
function PathExpand(const Filename: String): String;
function PathExpand(const Filename: String): String; overload;
function PathExpand(const Filename: String; out ExpandedFilename: String): Boolean; overload;
function PathExtensionPos(const Filename: String): Integer;
function PathExtractDir(const Filename: String): String;
function PathExtractDrive(const Filename: String): String;
Expand Down Expand Up @@ -278,7 +279,7 @@ function PathPathPartLength(const Filename: String;
end;
end;

function PathExpand(const Filename: String): String;
function PathExpand(const Filename: String; out ExpandedFilename: String): Boolean;
{ Like Delphi's ExpandFileName, but does proper error checking. }
var
Res: Integer;
Expand All @@ -287,9 +288,14 @@ function PathExpand(const Filename: String): String;
begin
DWORD(Res) := GetFullPathName(PChar(Filename), SizeOf(Buf) div SizeOf(Buf[0]),
Buf, FilePart);
if (Res > 0) and (Res < SizeOf(Buf) div SizeOf(Buf[0])) then
SetString(Result, Buf, Res)
else
Result := (Res > 0) and (Res < SizeOf(Buf) div SizeOf(Buf[0]));
if Result then
SetString(ExpandedFilename, Buf, Res)
end;

function PathExpand(const Filename: String): String;
begin
if not PathExpand(Filename, Result) then
Result := Filename;
end;

Expand Down
8 changes: 4 additions & 4 deletions Projects/Src/Compression.SevenZipDecoder.pas
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ function IS_7zDec(const fileName: PChar; const fullPaths: Bool): Integer; cdecl;
function __CreateDirectoryW(lpPathName: LPCWSTR;
lpSecurityAttributes: PSecurityAttributes): BOOL; cdecl;
begin
var ExpandedDir := PathExpand(lpPathName);
if PathStartsWith(ExpandedDir, ExpandedDestDir) then
var ExpandedDir: String;
if PathExpand(lpPathName, ExpandedDir) and PathStartsWith(ExpandedDir, ExpandedDestDir) then
Result := CreateDirectoryW(PChar(ExpandedDir), lpSecurityAttributes)
else begin
Result := False;
Expand All @@ -58,8 +58,8 @@ function __CreateFileW(lpFileName: LPCWSTR; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle; cdecl;
begin
var ExpandedFileName := PathExpand(lpFileName);
if PathStartsWith(ExpandedFileName, ExpandedDestDir) then
var ExpandedFileName: String;
if PathExpand(lpFileName, ExpandedFileName) and PathStartsWith(ExpandedFileName, ExpandedDestDir) then
Result := CreateFileW(PChar(ExpandedFileName), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile)
else begin
Result := INVALID_HANDLE_VALUE;
Expand Down

0 comments on commit bb0a079

Please sign in to comment.