couchbase/fleece

MutableDict's keyref boolean conversion does unintended key allocations

AndrewLipscomb opened this issue · 1 comments

From 59ff995

The following code will create a MutableDict with some odd behaviour and an invalid state

	fleece::MutableDict dict { fleece::MutableDict::newDict() };
	dict["a_key"] = 6;
	if (dict["a_non_existent_key"])
	{
		std::cout << "This probably shouldn't print!" << std::endl;
	}
	std::cout << dict.toJSONString() << std::endl;

In this case - the boolean conversions convert the keyref value to a Slot type rather than using the Value::operator bool conversion - resulting in the bool conversion being true despite a const MutableDict or Dict returning false - and more importantly, the toJSONString() call will terminate due to the keyref::set call being made without a value to write into it.

Gutfeel is that this behaviour is pretty unintuitive and that a pattern like this is pretty common given that is what you'd use for the const types - it can be workarounded if this is intended but that should really be documented.

snej commented

Ouch. C++ operator bool can be really frustrating. I'll take a look ASAP.