From 840389dad3b79d180297422d7c208950f4ecdc4e Mon Sep 17 00:00:00 2001 From: Bart <9132501-flyingsheep@users.noreply.gitlab.com> Date: Fri, 20 Aug 2021 18:43:19 +0200 Subject: [PATCH] LazFileUtils: make ResolveDots a little more consitent w.r.t. having the result ending with a PathDelim. Issue #37188. Do not merge to Fixes 2.2. --- components/lazutils/lazfileutils.inc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/components/lazutils/lazfileutils.inc b/components/lazutils/lazfileutils.inc index 698fe69fe1..d7471823fe 100644 --- a/components/lazutils/lazfileutils.inc +++ b/components/lazutils/lazfileutils.inc @@ -24,6 +24,7 @@ function ResolveDots(const AFilename: string): string; var SrcPos, DestPos, Len, DirStart: integer; c: char; MacroPos: LongInt; + ResultMustEndWithPathDelim: Boolean; begin Len:=length(AFilename); if Len=0 then exit(''); @@ -37,6 +38,13 @@ begin exit; {$endif} + //To add some consistency to the outcomes + //Depending on the path the algorithm takes it may remove the trailing PathDelim, so we restore it later if needed + //Issue #37188 + //It's a workaround, fee free to implement a better fix + ResultMustEndWithPathDelim := ((Len>2) and (AFilename[Len]='.') and (AFilename[Len-1]='.') and (AFilename[Len-2] in AllowDirectorySeparators)) or + ((Len>1) and (AFilename[Len]='.') and (AFilename[Len-1] in AllowDirectorySeparators)); + SrcPos:=1; DestPos:=1; @@ -169,7 +177,7 @@ begin // foo/. -> foo // C:foo\. -> C:foo // C:\. -> C:\ - dec(DestPos); + {dec(DestPos); } //Part of issue #37188 end; break; end; @@ -194,6 +202,8 @@ begin Result:='.' else SetLength(Result,DestPos-1); + if ResultMustEndWithPathDelim and (Result<>'.') and (Result[Length(Result)]<>PathDelim) then + Result := Result + PathDelim; end; function FilenameIsWinAbsolute(const TheFilename: string): boolean;