Skip to content

Access Violation When First Matcher is Not Defined #150

@IgorKaplya

Description

@IgorKaplya

An access violation occurs in the TBehavior.Match function when the first matcher is not defined. This issue arises because the MatchWithMatchers function attempts to access an undefined matcher, leading to a runtime error.

Steps to Reproduce:

  1. Create a mock object using TMock<IInterfaceToTest>.
  2. Set up the mock to return a value when specific parameters are matched.
  3. Call the method with the specified parameters.

Example Code:

procedure TExample_MatchersTests.Match_parameter_partial();
var
  mockCredit: TMock<IInterfaceToTest>;
begin
  mockCredit := TMock<IInterfaceToTest>.Create;

  mockCredit.Setup.WillReturn(6).When.TakesTwoParams(1, It(1).IsEqualTo<boolean>(true));

  Assert.AreEqual(6, mockCredit.Instance.TakesTwoParams(1, true));
end;

Presumed Bug Location:
The bug is likely in the TBehavior.Match function, specifically within the MatchWithMatchers function:

function TBehavior.Match(const Args: TArray<TValue>): Boolean;

  function MatchWithMatchers: Boolean;
  var
    i : integer;
  begin
    result := False;
    for i := 0 to High(FMatchers) do
    begin
      // Access violation when FMatchers[i] is nil
      if not FMatchers[i].Match(Args[i+1]) then
        exit;
    end;
    result := True;
  end;
image

Expected Behavior:
The function should handle cases where matchers are not defined without causing an access violation.

Actual Behavior:
An access violation occurs when the first matcher is not defined.

Environment:

  • Delphi version: 11.3
  • Operating System: Windows11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions