Skip to content

Commit

Permalink
Merge pull request #3 from skamradt/main
Browse files Browse the repository at this point in the history
Corrected Integer Overflow decoding an encoded value of $FFFFFFFFFFFF…
  • Loading branch information
dokkie8844 authored May 24, 2024
2 parents a3efb87 + 1caa96b commit e57db6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sqids.pas
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function TSqids.ToNumber(AId: string; AAlphabet: string): TNumber;
L := Length(AAlphabet);

for C in AId do
Result := Result * L + TNumber(Pos(C, AAlphabet)) - 1;
Result := Result * L + (TNumber(Pos(C, AAlphabet)) - 1);
end;

function TSqids.IsBlocked(AId: string): Boolean;
Expand Down
15 changes: 15 additions & 0 deletions tests/delphi/encoding.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ TTestEncoding = class(TTestCase)
procedure IdWithInvalidCharacter;
// out-of-range test not implemented:
// compiler enforces that all numbers are within the range of a TNumber
procedure ExtremeValue;
end;

implementation
Expand Down Expand Up @@ -180,6 +181,20 @@ procedure TTestEncoding.EmptyString;
end;
end;

procedure TTestEncoding.ExtremeValue;
const
Number: TNumber = $FFFFFFFFFFFFFFFF;
Id = 'eIkvoXH40Lmd'; // from a previous EncodeSingle
begin
with TSqids.Create do
try
CheckEquals(EncodeSingle(Number), Id);
CheckTrue(DecodeSingle(id)=Number);
finally
Free;
end;
end;

procedure TTestEncoding.IdWithInvalidCharacter;
begin
with TSqids.Create do
Expand Down

0 comments on commit e57db6b

Please sign in to comment.