Skip to content

A serious logic error that frees BRPeer twice in BRPeerManagerConnect #54

@w20089527

Description

@w20089527

This error will cause app crash

I will give some illustations to show it.

Step 1

In BRPeerManagerConnect, we do BRPeerConnect
image

Step 2

We assume that everything is ok in BRPeerConnect. Then, we will go into _peerThreadRoutine
image

Step 3

In _peerThreadRoutine we run to 1090 line. Here we assume we got the lock, then we run to 1091 line. OK, let's look back at BRPeerManagerConnect. Which line does it run ? Go to Step 4
image

Step 4

In BRPeerManagerConnect, we now at BRPeerConnectStatus. Let's look at BRPeerConnectStatus.
image

Step 5

When we want to get BRPeer status, we firstly need to get the peer's lock. You know, currently, the lock has been got in _peerThreadRoutine which at 1090 line. On the other hand, in _peerThreadRoutine we run at 1091 line. So BRPeerConnectStatus will be blocked until get the lock.
image

Step 6

OK, it's time for _peerThreadRoutine releasing the lock. BRPeerConnectStatus returns the BRPeerStatusDisconnected. Then we get into a situation that _peerDisconnected will be called. Switch to Step 7 to see what does it do.
image

Step 7

Ignore others, just look at BRPeerFree(peer); that frees BRPeer.
image

Step 8

Finally, let's switch back to _peerThreadRoutine. At line 1109, disconnected which is _peerDisconnected will be called again. So everyone will know its result(crash).
image

Solution

Actually, we can use another Status to make a difference from BRPeerStatusDisconnected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions