[3.x] Core: Add recursion level check for Array and Dictionary hashing

This commit is contained in:
Danil Alexeev 2023-08-22 15:07:53 +03:00
parent 17b403af81
commit 68e8c1bb8f
No known key found for this signature in database
GPG Key ID: 124453E157DA8DC7
6 changed files with 29 additions and 8 deletions

View File

@ -118,16 +118,25 @@ bool Array::operator==(const Array &p_array) const {
}
uint32_t Array::hash() const {
return recursive_hash(0);
}
uint32_t Array::recursive_hash(int p_recursion_count) const {
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, 0, "Max recursion reached");
p_recursion_count++;
uint32_t h = hash_djb2_one_32(0);
for (int i = 0; i < _p->array.size(); i++) {
h = hash_djb2_one_32(_p->array[i].hash(), h);
h = hash_djb2_one_32(_p->array[i].recursive_hash(p_recursion_count), h);
}
return h;
}
void Array::operator=(const Array &p_array) {
_ref(p_array);
}
void Array::push_back(const Variant &p_value) {
_p->array.push_back(p_value);
}

View File

@ -60,6 +60,7 @@ public:
bool operator==(const Array &p_array) const;
uint32_t hash() const;
uint32_t recursive_hash(int p_recursion_count) const;
void operator=(const Array &p_array);
void push_back(const Variant &p_value);

View File

@ -221,12 +221,20 @@ void Dictionary::_unref() const {
}
_p = nullptr;
}
uint32_t Dictionary::hash() const {
return recursive_hash(0);
}
uint32_t Dictionary::recursive_hash(int p_recursion_count) const {
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, 0, "Max recursion reached");
p_recursion_count++;
uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
h = hash_djb2_one_32(E.key().hash(), h);
h = hash_djb2_one_32(E.value().hash(), h);
h = hash_djb2_one_32(E.key().recursive_hash(p_recursion_count), h);
h = hash_djb2_one_32(E.value().recursive_hash(p_recursion_count), h);
}
return h;

View File

@ -75,6 +75,7 @@ public:
bool operator!=(const Dictionary &p_dictionary) const;
uint32_t hash() const;
uint32_t recursive_hash(int p_recursion_count) const;
void operator=(const Dictionary &p_dictionary);
const Variant *next(const Variant *p_key = nullptr) const;

View File

@ -2508,6 +2508,10 @@ Variant::~Variant() {
}*/
uint32_t Variant::hash() const {
return recursive_hash(0);
}
uint32_t Variant::recursive_hash(int p_recursion_count) const {
switch (type) {
case NIL: {
return 0;
@ -2622,13 +2626,10 @@ uint32_t Variant::hash() const {
return reinterpret_cast<const NodePath *>(_data._mem)->hash();
} break;
case DICTIONARY: {
return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(p_recursion_count);
} break;
case ARRAY: {
const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
return arr.hash();
return reinterpret_cast<const Array *>(_data._mem)->recursive_hash(p_recursion_count);
} break;
case POOL_BYTE_ARRAY: {
const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);

View File

@ -413,6 +413,7 @@ public:
bool operator!=(const Variant &p_variant) const;
bool operator<(const Variant &p_variant) const;
uint32_t hash() const;
uint32_t recursive_hash(int p_recursion_count) const;
bool hash_compare(const Variant &p_variant) const;
bool booleanize() const;