From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 628EA6F3E5; Sat, 6 Nov 2021 02:58:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 628EA6F3E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636156726; bh=QPROqIh63wwCzz5uEnJ/TR4dAjZYD+3Ry8Rxpl+HdlM=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=YfP6yDYNbOtZb+Bp7/sViAmcAXmVwvsc06qLwZvPj1jx+k+35Xk3XI2c70JrNiF3e pPF5QnyxHfZltkKeXW5/iWDUK81KQELIKuO5pcz4kQ/YCX5OUN4aPZe1B4gltaO6M5 RY+6fMglg4VPz6sE8pE9h7HpLwDzvF/D3zLLFE1M= Received: from smtpng3.i.mail.ru (smtpng3.i.mail.ru [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id E44E56DB24 for ; Sat, 6 Nov 2021 02:56:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E44E56DB24 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1mj94f-0006s6-KM; Sat, 06 Nov 2021 02:56:46 +0300 To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org, vdavydov@tarantool.org Date: Sat, 6 Nov 2021 00:56:35 +0100 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9F9D976862A30D4FE63DAF5621974E6B15B9B849281F8F673182A05F538085040983FFF2023A9D26C8C2C26216E724212881D629572A737A63A4EE5AF827F669F X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77BF46084C0059042EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B43005D9966BC2348638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D82C57435EA65B56A7FF8277A548E19110117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735209ECD01F8117BC8BEA471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7CD1D040B6C1ECEA3F43847C11F186F3C59DAA53EE0834AAEE X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B505F04FA6A337665710E60E8C3206D1B6E6 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C4173B97CBE165B82D912AB7649887A206BC0994F947283B77A9D420A4CFB5DD3E6C2AABBA1B15488263171F5B0540C207F4F744C73144E8B1D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75C69C5C0DDE134364410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34047E322BFAFD69BDB763CA2A17281560AA9DC34B7FA800AD7A1B49808518A2C801A15389B9C094751D7E09C32AA3244C190E49EAD11C52CE59CACEF84E6EFF62853296C06374E602FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj75A/c671lqWIJnGdzhquxA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D86591A0C6996E01C3042699505667CFA3841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH 4/9] error: use error_payload to store optional members X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" error_payload is a part of struct error now. All the fields stored in C++ classes on top of struct error are moved into the payload. Part of #5568 --- src/box/error.cc | 11 +++---- src/box/error.h | 34 +++++--------------- src/lib/core/diag.c | 2 ++ src/lib/core/diag.h | 70 +++++++++++++++++++++++++++++++++++++++++ src/lib/uuid/mp_uuid.c | 16 +++++++++- src/lib/uuid/tt_uuid.h | 9 ++++++ src/lua/error.lua | 12 +++++++ test/box/error.result | 4 +-- test/box/error.test.lua | 2 +- 9 files changed, 124 insertions(+), 36 deletions(-) diff --git a/src/box/error.cc b/src/box/error.cc index bc0d46601..629ec703f 100644 --- a/src/box/error.cc +++ b/src/box/error.cc @@ -305,9 +305,9 @@ AccessDeniedError::AccessDeniedError(const char *file, unsigned int line, */ if (run_trigers) trigger_run(&on_access_denied, (void *) &ctx); - m_object_type = strdup(object_type); - m_access_type = strdup(access_type); - m_object_name = strdup(object_name); + error_field_set_str(this, "object_type", object_type); + error_field_set_str(this, "object_name", object_name); + error_field_set_str(this, "access_type", access_type); } struct error * @@ -337,15 +337,14 @@ CustomError::CustomError(const char *file, unsigned int line, const char *custom_type, uint32_t errcode) :ClientError(&type_CustomError, file, line, errcode) { - strncpy(m_custom_type, custom_type, sizeof(m_custom_type) - 1); - m_custom_type[sizeof(m_custom_type) - 1] = '\0'; + error_field_set_str(this, "custom_type", custom_type); } void CustomError::log() const { say_file_line(S_ERROR, file, line, errmsg, - "Custom type %s", m_custom_type); + "Custom type %s", custom_type()); } struct error * diff --git a/src/box/error.h b/src/box/error.h index 30ab36a97..43faaea70 100644 --- a/src/box/error.h +++ b/src/box/error.h @@ -243,38 +243,23 @@ public: const char *object_name, const char *user_name, bool run_trigers = true); - ~AccessDeniedError() - { - free(m_object_name); - free(m_object_type); - free(m_access_type); - } - const char * - object_type() + object_type() const { - return m_object_type; + return error_field_get_str(this, "object_type"); } const char * - object_name() + object_name() const { - return m_object_name?:"(nil)"; + return error_field_get_str(this, "object_name"); } const char * - access_type() + access_type() const { - return m_access_type; + return error_field_get_str(this, "access_type"); } - -private: - /** Type of object the required access was denied to */ - char *m_object_type; - /** Name of object the required access was denied to */ - char *m_object_name; - /** Type of declined access */ - char *m_access_type; }; /** @@ -319,13 +304,10 @@ public: virtual void log() const; const char* - custom_type() + custom_type() const { - return m_custom_type; + return error_field_get_str(this, "custom_type"); } -private: - /** Custom type name. */ - char m_custom_type[64]; }; #endif /* defined(__cplusplus) */ diff --git a/src/lib/core/diag.c b/src/lib/core/diag.c index b557ca667..217c3ae7e 100644 --- a/src/lib/core/diag.c +++ b/src/lib/core/diag.c @@ -53,6 +53,7 @@ error_unref(struct error *e) to_delete->cause->effect = NULL; to_delete->cause = NULL; } + error_payload_destroy(&to_delete->payload); to_delete->destroy(to_delete); if (cause == NULL) return; @@ -117,6 +118,7 @@ error_create(struct error *e, e->refs = 0; e->saved_errno = 0; e->code = 0; + error_payload_create(&e->payload); if (file != NULL) { snprintf(e->file, sizeof(e->file), "%s", file); e->line = line; diff --git a/src/lib/core/diag.h b/src/lib/core/diag.h index 30b8d2f5d..03bd223e1 100644 --- a/src/lib/core/diag.h +++ b/src/lib/core/diag.h @@ -36,6 +36,8 @@ #include #include #include + +#include "error_payload.h" #include "say.h" #if defined(__cplusplus) @@ -89,6 +91,8 @@ struct error { int saved_errno; /** Error code. Shortest possible description of error's reason. */ int code; + /** Key-value storage with error's dynamic fields. */ + struct error_payload payload; /** Line number. */ unsigned line; /* Source file name. */ @@ -120,6 +124,72 @@ error_ref(struct error *e); void error_unref(struct error *e); +static inline const char * +error_field_get_str(const struct error *e, const char *name) +{ + return error_payload_get_str(&e->payload, name); +} + +static inline void +error_field_set_str(struct error *e, const char *name, const char *value) +{ + error_payload_set_str(&e->payload, name, value); +} + +static inline bool +error_field_get_uint(const struct error *e, const char *name, uint64_t *value) +{ + return error_payload_get_uint(&e->payload, name, value); +} + +static inline void +error_field_set_uint(struct error *e, const char *name, uint64_t value) +{ + error_payload_set_uint(&e->payload, name, value); +} + +static inline bool +error_field_get_int(const struct error *e, const char *name, int64_t *value) +{ + return error_payload_get_int(&e->payload, name, value); +} + +static inline void +error_field_set_int(struct error *e, const char *name, int64_t value) +{ + error_payload_set_int(&e->payload, name, value); +} + +static inline bool +error_field_get_double(const struct error *e, const char *name, double *value) +{ + return error_payload_get_double(&e->payload, name, value); +} + +static inline void +error_field_set_double(struct error *e, const char *name, double value) +{ + error_payload_set_double(&e->payload, name, value); +} + +static inline bool +error_field_get_bool(const struct error *e, const char *name, bool *value) +{ + return error_payload_get_bool(&e->payload, name, value); +} + +static inline void +error_field_set_bool(struct error *e, const char *name, bool value) +{ + error_payload_set_bool(&e->payload, name, value); +} + +static inline void +error_field_unset(struct error *e, const char *name) +{ + error_payload_unset(&e->payload, name); +} + /** * Unlink error from its effect. For instance: * e1 -> e2 -> e3 -> e4 (e1:set_prev(e2); e2:set_prev(e3) ...) diff --git a/src/lib/uuid/mp_uuid.c b/src/lib/uuid/mp_uuid.c index a60716eb5..51f0465ac 100644 --- a/src/lib/uuid/mp_uuid.c +++ b/src/lib/uuid/mp_uuid.c @@ -32,7 +32,7 @@ #include "mp_uuid.h" #include "msgpuck.h" #include "mp_extension_types.h" -#include "error_payload.h" +#include "diag.h" inline uint32_t mp_sizeof_uuid(void) @@ -139,3 +139,17 @@ error_payload_set_uuid(struct error_payload *e, const char *name, char *data = error_payload_prepare(e, name, mp_sizeof_uuid(), 0)->data; mp_encode_uuid(data, uuid); } + +bool +error_field_get_uuid(const struct error *e, const char *name, + struct tt_uuid *uuid) +{ + return error_payload_get_uuid(&e->payload, name, uuid); +} + +void +error_field_set_uuid(struct error *e, const char *name, + const struct tt_uuid *uuid) +{ + error_payload_set_uuid(&e->payload, name, uuid); +} diff --git a/src/lib/uuid/tt_uuid.h b/src/lib/uuid/tt_uuid.h index 866764e77..eb457c1f1 100644 --- a/src/lib/uuid/tt_uuid.h +++ b/src/lib/uuid/tt_uuid.h @@ -41,6 +41,7 @@ extern "C" { #endif +struct error; struct error_payload; enum { UUID_LEN = 16, UUID_STR_LEN = 36 }; @@ -206,6 +207,14 @@ void error_payload_set_uuid(struct error_payload *e, const char *name, const struct tt_uuid *uuid); +bool +error_field_get_uuid(const struct error *e, const char *name, + struct tt_uuid *uuid); + +void +error_field_set_uuid(struct error *e, const char *name, + const struct tt_uuid *uuid); + #if defined(__cplusplus) } /* extern "C" */ #endif diff --git a/src/lua/error.lua b/src/lua/error.lua index 9fb227df4..a5d1d2a43 100644 --- a/src/lua/error.lua +++ b/src/lua/error.lua @@ -11,6 +11,17 @@ enum { typedef void (*error_f)(struct error *e); +struct error_field { + uint32_t _size; + const char *_data; + char _name[0]; +}; + +struct error_payload { + int _count; + struct error_field **_fields; +}; + struct error { error_f _destroy; error_f _raise; @@ -19,6 +30,7 @@ struct error { int64_t _refs; int _saved_errno; int code; + struct error_payload _payload; /** Line number. */ unsigned _line; /* Source file name. */ diff --git a/test/box/error.result b/test/box/error.result index 19ea53873..7583306a6 100644 --- a/test/box/error.result +++ b/test/box/error.result @@ -899,13 +899,13 @@ e:unpack() | - file: '[string "e = box.error.new({type = ''TestType''}) "]' | line: 1 | ... --- Try too long type name. +-- Try long type name. e = box.error.new({type = string.rep('a', 128)}) | --- | ... #e.type | --- - | - 63 + | - 128 | ... -- gh-4887: accessing 'prev' member also refs it so that after diff --git a/test/box/error.test.lua b/test/box/error.test.lua index ed95d1d41..068212d96 100644 --- a/test/box/error.test.lua +++ b/test/box/error.test.lua @@ -241,7 +241,7 @@ e:unpack() -- Try to omit message. e = box.error.new({type = 'TestType'}) e:unpack() --- Try too long type name. +-- Try long type name. e = box.error.new({type = string.rep('a', 128)}) #e.type -- 2.24.3 (Apple Git-128)