Changeset 438 for trunk/CmdList.pas


Ignore:
Timestamp:
May 18, 2022, 11:12:29 AM (2 years ago)
Author:
chronos
Message:
  • Fixed: Data size was not correctly stored in server commands. Introduced in rev 435.
  • Fixed: Check data size for its maximum. Limit maximum length of unit and city name so it can fit into data block.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CmdList.pas

    r355 r438  
    55
    66uses
    7   Classes;
     7  Classes, SysUtils, Math;
    88
    99const
    1010  MaxDataSize = 1024;
     11  CommandDataElementSize = 4;
     12  CommandDataElementCountMask = $f;
     13  CommandDataMaxSize = CommandDataElementSize * CommandDataElementCountMask;
    1114
    1215type
     
    4447  end;
    4548
     49  function CommandWithData(Command: Integer; DataSize: Byte): Integer;
     50
     51resourcestring
     52  SCommandDataSizeError = 'Command data size %d out of range (0-%d).';
     53
     54
    4655implementation
    4756
     
    5564  TData = array [0 .. MaxDataSize - 1] of Cardinal;
    5665  PData = ^TData;
     66
     67function CommandWithData(Command: Integer; DataSize: Byte): Integer;
     68var
     69  DataElementCount: Byte;
     70begin
     71  if DataSize > CommandDataMaxSize then
     72    raise Exception.Create(Format(SCommandDataSizeError, [DataSize, CommandDataMaxSize]));
     73  DataElementCount := Ceil(DataSize / CommandDataElementSize);
     74  Result := Command or (DataElementCount and CommandDataElementCountMask);
     75end;
    5776
    5877constructor TCmdList.Create;
     
    139158    end;
    140159
    141     if Command and $F = 0 then
     160    if Command and CommandDataElementCountMask = 0 then
    142161      Data := nil
    143162    else
    144163    begin
    145164      Data := @LogData[FState.LoadPos];
    146       inc(FState.LoadPos, Command and $F * 4);
     165      inc(FState.LoadPos, Command and CommandDataElementCountMask * CommandDataElementSize);
    147166    end;
    148167  end;
     
    232251    end;
    233252  end;
    234   if Command and $F <> 0 then
    235     PutData(Data, Command and $F * 4);
     253  if Command and CommandDataElementCountMask <> 0 then
     254    PutData(Data, Command and CommandDataElementCountMask * CommandDataElementSize);
    236255end;
    237256
Note: See TracChangeset for help on using the changeset viewer.