couchbase/fleece

Dict::iterator::keyString() does not correctly handle an empty dictionary

bmeike opened this issue · 1 comments

When the dictionary is empty, _key is NULL. Attempting to de-reference it will cause a failure. I believe that both keyString and keyt do this.

snej commented

If the dictionary is empty, the iterator starts out past the end, and you shouldn't be accessing its current key when it's past the end. It sounds like your loop isn't written correctly. It should look like:

for (Dict::iterator i(dict); i; ++i) { ... }

If you're using the C API, the equivalent of the boolean check is FLDictIterator_GetValue(); it returns NULL when past the end. So an iteration looks like

FLDictIterator iter;
FLDictIterator_Begin(theDict, &iter);
FLValue value;
while (NULL != (value = FLDictIterator_GetValue(&iter))) {
    FLString key = FLDictIterator_GetKeyString(&iter);
    // ...
    FLDictIterator_Next(&iter);
}