GSHashTable: do not count duplicate keys when creating from an array#74
Open
DTW-Thalion wants to merge 1 commit into
Open
GSHashTable: do not count duplicate keys when creating from an array#74DTW-Thalion wants to merge 1 commit into
DTW-Thalion wants to merge 1 commit into
Conversation
GSHashTableCreate added every element of the input array and incremented _count each time without checking whether the key was already present, so a repeated key inflated the count past the number of stored buckets. That made CFSetGetCount / CFDictionaryGetCount over-report, and left CFSetGetValues reading uninitialised slots. Guard the insert with the same bucket->count check the mutable GSHashTableAddValue path already uses. Adds Tests/CFSet/create_duplicates.m and Tests/CFDictionary/create_duplicates.m.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
GSHashTableCreatebuilds the table from the input array by callingGSHashTableAddKeyValuePairand incrementing_countfor every element, with no check for a key that is already present:For a duplicate key,
GSHashTableFindBucketreturns the existing bucket, but_countis still incremented, so it ends up larger than the number of stored buckets. SinceGSHashTableGetCountreturns_countwhileGSHashTableGetKeysAndValuesemits one entry per occupied bucket, the two disagree:CFSetGetCount/CFDictionaryGetCountover-report (e.g.CFSetCreateof{a, b, a}reports 3).CFSetGetValues(and the dictionary/bag equivalents) then read past the filled buckets into uninitialised memory.This affects
CFSetCreate,CFDictionaryCreateandCFBagCreate, which all share this path.Fix
Guard the insert with the same
bucket->count <= 0check the mutableGSHashTableAddValuepath already uses, so a key that is already present is not added or counted again. Creation now behaves consistently with incremental insertion.Tests
Tests/CFSet/create_duplicates.mandTests/CFDictionary/create_duplicates.mcreate a container from an array containing a repeated key and check the count. The dictionary test fails before this change (count 2) and passes after (count 1); both pass after the fix.