Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SkybuckFlying opened this issue Jan 27, 2022 · 0 comments

Comments

@SkybuckFlying
Copy link
Contributor

SkybuckFlying commented Jan 27, 2022

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant