From 0ad43426499bbd1073e538c983630926899d2979 Mon Sep 17 00:00:00 2001 From: sideshowbarker Date: Tue, 8 Aug 2023 11:32:44 +0900 Subject: [PATCH] Fail if Append()ing string to Rope in excess of TInlineString size Relates to #153. --- src/lib/ropes.pas | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/ropes.pas b/src/lib/ropes.pas index 4db8276..8cfa033 100644 --- a/src/lib/ropes.pas +++ b/src/lib/ropes.pas @@ -48,7 +48,7 @@ TFixedSizeRopeFragment = record TUTF8InlineIndexPlusOne = 0..UTF8InlineSize; TCodepointsIndex = 0..CodepointsSize-1; TCodepointsIndexPlusOne = 0..CodepointsSize; - TInlineString = String[UTF8InlineSize]; + TInlineString = String[UTF8InlineSize + 1]; PRopeFragment = ^TRopeFragment; TRopeFragment = record Next: PRopeFragment; @@ -963,6 +963,12 @@ procedure Rope.Append(const NewString: RopeInternals.TInlineString); {$IFDEF VERBOSE} if (DebugNow) then Writeln('Ropes: Append(ShortString) on rope @', IntToHex(PtrUInt(@Self), 16), ' with data @', IntToHex(PtrUInt(FValue), 16)); {$ENDIF} {$IFDEF VERBOSE} if (DebugNow) then Writeln('Ropes: Length(FValue)=', Length(FValue)); {$ENDIF} Assert(Length(NewString) <= RopeInternals.UTF8InlineSize, 'Maximum size of short string is ' + IntToStr(RopeInternals.UTF8InlineSize)); + if (Length(NewString) > RopeInternals.UTF8InlineSize) then + begin + Writeln('Error: Append() call with string length > UTF8InlineSize: "' + NewString + '"'); + Writeln('Call Append() with a string pointer, not a string: Append(@Foo), not Append(Foo)'); + Halt(1); + end; if ((not Assigned(FLast)) or (FLast^.Kind <> rfUTF8Inline) or (RopeInternals.UTF8InlineSize - FLast^.InlineLength < Length(NewString))) then begin EnsureSize(1, 1);