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

Fix comparing nullslice and empty slice #170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Fleece/API_Impl/FLSlice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,28 @@ FL_ASSUME_NONNULL_BEGIN

__hot
bool FLSlice_Equal(FLSlice a, FLSlice b) noexcept {
return a.size == b.size && FLMemCmp(a.buf, b.buf, a.size) == 0;
if (a.size == b.size) {
if (a.size == 0)
// Check whether both slices are the nullslice or empty slices.
return (a.buf == nullptr) == (b.buf == nullptr);
else
return FLMemCmp(a.buf, b.buf, a.size) == 0;
}
return false;
}


__hot
int FLSlice_Compare(FLSlice a, FLSlice b) noexcept {
// Optimized for speed, not simplicity!
if (a.size == b.size)
return FLMemCmp(a.buf, b.buf, a.size);
if (a.size == 0) {
// Sort the nullslice before an empty slice.
if ((a.buf == nullptr) == (b.buf == nullptr))
return 0;
else
return a.buf == nullptr ? -1 : 1;
} else
return FLMemCmp(a.buf, b.buf, a.size);
else if (a.size < b.size) {
int result = FLMemCmp(a.buf, b.buf, a.size);
return result ? result : -1;
Expand Down
13 changes: 13 additions & 0 deletions Tests/API_ValueTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,16 @@ TEST_CASE("API MutableDict item bool conversion", "[API]") {
}
CHECK(dict.toJSONString() == "{\"a_key\":6}");
}

TEST_CASE("API Mutable copy of Dict with empty string shared key", "[API]") {
auto sk = fleece::SharedKeys::create();
auto enc = fleece::Encoder(sk);
enc.beginDict();
enc.writeKey("");
enc.writeNull();
enc.endDict();
auto doc = enc.finishDoc();
auto copy = doc.root().asDict().mutableCopy(kFLCopyImmutables);
CHECK(copy.count() == 1);
CHECK(copy[""].type() == kFLNull);
}