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

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

Open
w20089527 opened this issue Jun 11, 2019 · 0 comments
Open

Comments

@w20089527
Copy link

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

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