Skip to content

TMemoryStream object leak in UBlockChain.pas: class function TPCOperation.GetOperationFromStreamData #240

Description

@SkybuckFlying

Potential TMemoryStream object leak found. See ***.

TMemoryStream may not be freed if exited.

See line 3879 in UBlockChain.pas

class function TPCOperation.GetOperationFromStreamData(AUseV5EncodeStyle : Boolean; ACurrentProtocol: word; StreamData : TBytes): TPCOperation;
  // Loads an TPCOperation saved using "GetOperationStreamData"
  // For compatiblity will allow V4..V5 encode stype
  // Old V4: 1 byte for OpType
  // New V5: 2 bytes for OpType and 2 bytes for ProtocolVersion
  // N bytes for Operation specific data (saved at SaveOpToStream)
  //
  // NOTE:
  // AFTER V5 activation, all nodes must use new AUseV5EcnodeStyle = TRUE
Var stream : TStream;
  b : Byte;
  j: Integer;
  OpClass: TPCOperationClass;
  auxOp: TPCOperation;
  LOpType, LOperationProtocolVersion : Word;
begin
  Result := Nil;
  stream := TMemoryStream.Create;
  Try
    stream.WriteBuffer(StreamData[0],Length(StreamData)); // Fixed bug 4.0.0
    stream.Position := 0;

    if (AUseV5EncodeStyle) then begin
      // 2 bytes (UInt16) for OpType
      // 2 bytes (UInt16) for ProtocolVersion
      Stream.Read(LOpType,2);
      Stream.Read(LOperationProtocolVersion,2);
      if (LOperationProtocolVersion<=0) or (LOperationProtocolVersion>CT_BUILD_PROTOCOL) then Exit;
    end else begin
      // 1 bytes (UInt8) for OpType
      // Fixed ProtocolVersion = 4
      stream.Read(b,1);
      LOpType := b;
      LOperationProtocolVersion:=ACurrentProtocol;
    end;

    j := TPCOperationsComp.IndexOfOperationClassByOpType(LOpType);
    if j >= 0 then
      OpClass := _OperationsClass[j]
    else Exit;  // *** POTENTIAL OBJECT LEAK ***
    auxOp := OpClass.Create(LOperationProtocolVersion);
    if auxOp.LoadOpFromStream(stream,False) then Result := auxOp
    else auxOp.Free;
  Finally
    stream.Free;  // *** NEVER CALLED IF EXIT ***
  End;
end;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions