From 6aede992a9db749d6e420b645416d9a6f5fe356b Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 14 Feb 2022 23:04:05 +0300 Subject: [PATCH] Fixed variant decoding Segmentation Fault --- core/io/marshalls.cpp | 11 ++++++----- core/io/marshalls.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index a363cc36946..5c39b2fa1b3 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -94,7 +94,8 @@ static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r return OK; } -Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects) { +Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects, int p_depth) { + ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Variant is too deep. Bailing."); const uint8_t *buf = p_buffer; int len = p_len; @@ -585,7 +586,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Variant value; int used; - err = decode_variant(value, buf, len, &used, p_allow_objects); + err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1); if (err) { return err; } @@ -635,7 +636,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Variant key, value; int used; - Error err = decode_variant(key, buf, len, &used, p_allow_objects); + Error err = decode_variant(key, buf, len, &used, p_allow_objects, p_depth + 1); ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; @@ -644,7 +645,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int (*r_len) += used; } - err = decode_variant(value, buf, len, &used, p_allow_objects); + err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1); ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; @@ -677,7 +678,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int for (int i = 0; i < count; i++) { int used = 0; Variant v; - Error err = decode_variant(v, buf, len, &used, p_allow_objects); + Error err = decode_variant(v, buf, len, &used, p_allow_objects, p_depth + 1); ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; len -= used; diff --git a/core/io/marshalls.h b/core/io/marshalls.h index 4d7b98b7491..fef3a1c2c1b 100644 --- a/core/io/marshalls.h +++ b/core/io/marshalls.h @@ -212,7 +212,7 @@ public: EncodedObjectAsID() {} }; -Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false); +Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false, int p_depth = 0); Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0); #endif // MARSHALLS_H