This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
Fix: KVO Observation blocks can get called with garbage parameters #276
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Short version: In the KVO addObserver methods that take an
options
parameter, if the caller passes 0, the observation block can get called with fewer parameters than expected, which in ARC, causes the compiler to try to retain what is effectively garbage, causing a hard crash.Longer version: For instance, if I create an observation like this:
that call will get passed down to
-[NSObject bk_addObserverForKeyPaths:identifier:options:task:]
which determines what context to use with this line:except that in
-[_BKObserver observeValueForKeyPath:ofObject:change:context:]
, the notification having a context ofBKObserverContextManyKeys
(instead ofBKObserverContextManyKeysWithChange
) will cause the task block to get casted tovoid (^task)(id, NSString *)
, when the block that was passed in was avoid (^task)(id, NSString *, NSDictionary *)
. The result is that when task is called, the third parameter is garbage (whatever happens to be at that position in the stack.) ARC emits calls that assumes that all parameters are valid or nil, so a lot of the time (but not always, if the garbage value happens to be a valid ObjC object) this will cause a hard crash.To avoid this, it would be advisable to always call blocks with the signature that they expect, regardless of options (which this patch does.) Alternatively, you could assert when someone calls the
options:
-taking methods with 0 foroptions
, but I'm partial to the solution here.I filed an Issue related to this pull request: #277