mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-19 17:55:43 +00:00
Fix handling non null-terminated string_views in LookupByKey (#8203)
* Reproduce the error in a unit test Reproduces #8200 * Overload KeyCompareWithValue to work for string-like objects This fixes #8200. * Extra tests --------- Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -1436,6 +1436,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
int KeyCompareWithValue(const char *_name) const {
|
||||
return strcmp(name()->c_str(), _name);
|
||||
}
|
||||
template<typename StringType>
|
||||
int KeyCompareWithValue(const StringType& _name) const {
|
||||
if (name()->c_str() < _name) return -1;
|
||||
if (_name < name()->c_str()) return 1;
|
||||
return 0;
|
||||
}
|
||||
const ::flatbuffers::Vector<uint8_t> *inventory() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<uint8_t> *>(VT_INVENTORY);
|
||||
}
|
||||
|
||||
@@ -598,6 +598,12 @@ struct FooTable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
int KeyCompareWithValue(const char *_c) const {
|
||||
return strcmp(c()->c_str(), _c);
|
||||
}
|
||||
template<typename StringType>
|
||||
int KeyCompareWithValue(const StringType& _c) const {
|
||||
if (c()->c_str() < _c) return -1;
|
||||
if (_c < c()->c_str()) return 1;
|
||||
return 0;
|
||||
}
|
||||
const ::flatbuffers::Vector<const keyfield::sample::Baz *> *d() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<const keyfield::sample::Baz *> *>(VT_D);
|
||||
}
|
||||
|
||||
@@ -313,6 +313,33 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
|
||||
|
||||
// Verify the same objects are returned for char*-based and string-based
|
||||
// lookups.
|
||||
TEST_EQ(vecoftables->LookupByKey("Barney"),
|
||||
vecoftables->LookupByKey(std::string("Barney")));
|
||||
TEST_EQ(vecoftables->LookupByKey("Fred"),
|
||||
vecoftables->LookupByKey(std::string("Fred")));
|
||||
TEST_EQ(vecoftables->LookupByKey("Wilma"),
|
||||
vecoftables->LookupByKey(std::string("Wilma")));
|
||||
|
||||
#ifdef FLATBUFFERS_HAS_STRING_VIEW
|
||||
// Tests for LookupByKey with a key that is a truncated
|
||||
// version of a longer, invalid key.
|
||||
const std::string invalid_key = "Barney123";
|
||||
std::string_view valid_truncated_key = invalid_key;
|
||||
valid_truncated_key.remove_suffix(3); // "Barney"
|
||||
TEST_NOTNULL(vecoftables->LookupByKey(valid_truncated_key));
|
||||
TEST_EQ(vecoftables->LookupByKey("Barney"),
|
||||
vecoftables->LookupByKey(valid_truncated_key));
|
||||
|
||||
// Tests for LookupByKey with a key that is a truncated
|
||||
// version of a longer, valid key.
|
||||
const std::string valid_key = "Barney";
|
||||
std::string_view invalid_truncated_key = valid_key;
|
||||
invalid_truncated_key.remove_suffix(3); // "Bar"
|
||||
TEST_NULL(vecoftables->LookupByKey(invalid_truncated_key));
|
||||
#endif // FLATBUFFERS_HAS_STRING_VIEW
|
||||
|
||||
// Test accessing a vector of sorted structs
|
||||
auto vecofstructs = monster->testarrayofsortedstruct();
|
||||
if (vecofstructs) { // not filled in monster_test.bfbs
|
||||
|
||||
@@ -1432,6 +1432,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
int KeyCompareWithValue(const char *_name) const {
|
||||
return strcmp(name()->c_str(), _name);
|
||||
}
|
||||
template<typename StringType>
|
||||
int KeyCompareWithValue(const StringType& _name) const {
|
||||
if (name()->c_str() < _name) return -1;
|
||||
if (_name < name()->c_str()) return 1;
|
||||
return 0;
|
||||
}
|
||||
const ::flatbuffers::Vector<uint8_t> *inventory() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<uint8_t> *>(VT_INVENTORY);
|
||||
}
|
||||
|
||||
@@ -1423,6 +1423,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
int KeyCompareWithValue(const char *_name) const {
|
||||
return strcmp(name()->c_str(), _name);
|
||||
}
|
||||
template<typename StringType>
|
||||
int KeyCompareWithValue(const StringType& _name) const {
|
||||
if (name()->c_str() < _name) return -1;
|
||||
if (_name < name()->c_str()) return 1;
|
||||
return 0;
|
||||
}
|
||||
const ::flatbuffers::Vector<uint8_t> *inventory() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<uint8_t> *>(VT_INVENTORY);
|
||||
}
|
||||
|
||||
@@ -1423,6 +1423,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
int KeyCompareWithValue(const char *_name) const {
|
||||
return strcmp(name()->c_str(), _name);
|
||||
}
|
||||
template<typename StringType>
|
||||
int KeyCompareWithValue(const StringType& _name) const {
|
||||
if (name()->c_str() < _name) return -1;
|
||||
if (_name < name()->c_str()) return 1;
|
||||
return 0;
|
||||
}
|
||||
const ::flatbuffers::Vector<uint8_t> *inventory() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<uint8_t> *>(VT_INVENTORY);
|
||||
}
|
||||
|
||||
@@ -1423,6 +1423,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
int KeyCompareWithValue(const char *_name) const {
|
||||
return strcmp(name()->c_str(), _name);
|
||||
}
|
||||
template<typename StringType>
|
||||
int KeyCompareWithValue(const StringType& _name) const {
|
||||
if (name()->c_str() < _name) return -1;
|
||||
if (_name < name()->c_str()) return 1;
|
||||
return 0;
|
||||
}
|
||||
const ::flatbuffers::Vector<uint8_t> *inventory() const {
|
||||
return GetPointer<const ::flatbuffers::Vector<uint8_t> *>(VT_INVENTORY);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user