-
Notifications
You must be signed in to change notification settings - Fork 43
Fix infinite loop in NSCFDictionary/NSCFSet fast enumeration #73
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
Open
rupertdaniel
wants to merge
2
commits into
gnustep:master
Choose a base branch
from
rupertdaniel:fix/nscf-fast-enumeration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #import <Foundation/NSDictionary.h> | ||
| #import <Foundation/NSNumber.h> | ||
| #include "CoreFoundation/CFDictionary.h" | ||
| #include "../CFTesting.h" | ||
|
|
||
| /* Build a dictionary via the CF API so it is backed by NSCFDictionary | ||
| * (not a gnustep-base dictionary), which is what exercises the bridge's | ||
| * fast-enumeration path. */ | ||
| static CFMutableDictionaryRef makeCFDict(int count) | ||
| { | ||
| CFMutableDictionaryRef d = CFDictionaryCreateMutable(NULL, 0, | ||
| &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| id key = [[NSNumber alloc] initWithInt: i]; | ||
| id val = [[NSNumber alloc] initWithInt: i * 10]; | ||
| CFDictionarySetValue(d, (const void *)key, (const void *)val); | ||
| [key release]; | ||
| [val release]; | ||
| } | ||
| return d; | ||
| } | ||
|
|
||
| static void testFastEnumeration(int count) | ||
| { | ||
| CFMutableDictionaryRef cfdict = makeCFDict(count); | ||
| NSDictionary *dict = (NSDictionary *)cfdict; | ||
|
|
||
| int *seen = calloc(count > 0 ? count : 1, sizeof(int)); | ||
| NSUInteger visited = 0, guard = 0, limit = (NSUInteger)count * 4 + 16; | ||
| Boolean duplicate = false, invalid = false; | ||
|
|
||
| for (NSNumber *key in dict) | ||
| { | ||
| int k = [key intValue]; | ||
| if (k < 0 || k >= count) invalid = true; | ||
| else if (seen[k]++) duplicate = true; | ||
|
|
||
| NSNumber *val = [dict objectForKey: key]; /* usable mid-enumeration */ | ||
| if (!val || [val intValue] != k * 10) invalid = true; | ||
|
|
||
| visited++; | ||
| if (++guard > limit) break; /* regression: never terminates */ | ||
| } | ||
|
|
||
| PASS_CF(guard <= limit, | ||
| "fast enumeration of a %d-key CFDictionary terminates", count); | ||
| PASS_CF(visited == (NSUInteger)count, | ||
| "fast enumeration visits every key exactly once (%d keys, visited %lu)", | ||
| count, (unsigned long)visited); | ||
| PASS_CF(!duplicate, "fast enumeration does not repeat keys (%d keys)", count); | ||
| PASS_CF(!invalid, "fast enumeration yields valid keys/values (%d keys)", count); | ||
|
|
||
| free(seen); | ||
| CFRelease(cfdict); | ||
| } | ||
|
|
||
| /* A fresh for/in must restart cleanly (state->state resets to 0). */ | ||
| static void testReEnumeration(void) | ||
| { | ||
| CFMutableDictionaryRef cfdict = makeCFDict(10); | ||
| NSDictionary *dict = (NSDictionary *)cfdict; | ||
| NSUInteger a = 0, b = 0, guard = 0; | ||
|
|
||
| for (id k in dict) { (void)k; if (++guard > 100) break; a++; } | ||
| guard = 0; | ||
| for (id k in dict) { (void)k; if (++guard > 100) break; b++; } | ||
|
|
||
| PASS_CF(a == 10 && b == 10, | ||
| "a CFDictionary can be fast-enumerated repeatedly (%lu, then %lu)", | ||
| (unsigned long)a, (unsigned long)b); | ||
|
|
||
| CFRelease(cfdict); | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| testFastEnumeration(0); /* empty: loop body never runs */ | ||
| testFastEnumeration(1); /* the originally reported case */ | ||
| testFastEnumeration(20); /* > 16-element batch: cursor resume across calls */ | ||
| testFastEnumeration(500); /* many batches + hash collisions */ | ||
| testReEnumeration(); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #import <Foundation/NSSet.h> | ||
| #import <Foundation/NSNumber.h> | ||
| #include "CoreFoundation/CFSet.h" | ||
| #include "../CFTesting.h" | ||
|
|
||
| /* Build a set via the CF API so it is backed by NSCFSet, exercising the | ||
| * bridge's fast-enumeration path. */ | ||
| static CFMutableSetRef makeCFSet(int count) | ||
| { | ||
| CFMutableSetRef s = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| id v = [[NSNumber alloc] initWithInt: i]; | ||
| CFSetAddValue(s, (const void *)v); | ||
| [v release]; | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| static void testFastEnumeration(int count) | ||
| { | ||
| CFMutableSetRef cfset = makeCFSet(count); | ||
| NSSet *set = (NSSet *)cfset; | ||
|
|
||
| int *seen = calloc(count > 0 ? count : 1, sizeof(int)); | ||
| NSUInteger visited = 0, guard = 0, limit = (NSUInteger)count * 4 + 16; | ||
| Boolean duplicate = false, invalid = false; | ||
|
|
||
| for (NSNumber *v in set) | ||
| { | ||
| int k = [v intValue]; | ||
| if (k < 0 || k >= count) invalid = true; | ||
| else if (seen[k]++) duplicate = true; | ||
| visited++; | ||
| if (++guard > limit) break; /* regression: never terminates */ | ||
| } | ||
|
|
||
| PASS_CF(guard <= limit, | ||
| "fast enumeration of a %d-element CFSet terminates", count); | ||
| PASS_CF(visited == (NSUInteger)count, | ||
| "fast enumeration visits every element once (%d, visited %lu)", | ||
| count, (unsigned long)visited); | ||
| PASS_CF(!duplicate, "fast enumeration does not repeat elements (%d)", count); | ||
| PASS_CF(!invalid, "fast enumeration yields valid elements (%d)", count); | ||
|
|
||
| free(seen); | ||
| CFRelease(cfset); | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| testFastEnumeration(0); | ||
| testFastEnumeration(1); | ||
| testFastEnumeration(20); | ||
| testFastEnumeration(500); | ||
| return 0; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.