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 2865A6FFBB; Fri, 12 Nov 2021 02:58:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2865A6FFBB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636675089; bh=seeeLk9tnvdgs12FuzUJBds6oOhgN5egnCWFG5Ry4cM=; 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=cI2H96qx7EcOjvFpP5gpPRG6GBts3TJpDRuS2ktucefwSXhYgLQz6IbG1BsIq/C5q Z3Pp+qPHC9wZ8xJ7jxdgklzopInDxXMsaW2d115/5ZbiNz2O8ot3NLXV3GvdDR22pE VM42I2e/izhZlH+mkbfsh39RMuWR6m21COvvjc4E= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 8643B70655 for ; Fri, 12 Nov 2021 02:54:40 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8643B70655 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mlJtv-0003tY-E6; Fri, 12 Nov 2021 02:54:39 +0300 To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org Date: Fri, 12 Nov 2021 00:54:26 +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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9731B3922EC063979CAB00300CDA98DC51CCA794E1CC6B18A00894C459B0CD1B9F17981B8798AE693280C7A9AFA7422F47F76FE7EF67E38FFD6CDE72A09B3180C X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE73B44982FA5E78411EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006377F69ABDCCC31D2058638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D851B67921A4B156101EE75B6F93507F13117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18C26CFBAC0749D213D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6300D3B61E77C8D3B089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B50537BF44C8E1EC058A2436C49CE05BB7D1 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C4173B97CBE165B82D912AB7649887A206BC0994F947283B77A9D420A4CFB5DD3EC0B23F6C27EA5B1EE9B4A59BAA07B7DF8665D501CE775C20D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7536C62C4FBC402878410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34FA5A4D623E7343BBBE9C6421076A27873D86434CBFF3655406F3723ECA99680D94488F29A04537321D7E09C32AA3244CFB9007156BAAF6D853CED673108FC346CE0B41342B755BCD729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bJycQEUcQzRRg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D4DF9C993F9A495C1D3A8ECA1B6AD330E3841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 05/11] 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 | 82 +++++++++++++++++++++++++++++++++++++++++ src/lua/error.lua | 12 ++++++ test/box/error.result | 4 +- test/box/error.test.lua | 2 +- 7 files changed, 112 insertions(+), 35 deletions(-) diff --git a/src/box/error.cc b/src/box/error.cc index bc0d46601..34e807eb3 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_set_str(this, "object_type", object_type); + error_set_str(this, "object_name", object_name); + error_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_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..cc37412d7 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_get_str(this, "object_type"); } const char * - object_name() + object_name() const { - return m_object_name?:"(nil)"; + return error_get_str(this, "object_name"); } const char * - access_type() + access_type() const { - return m_access_type; + return error_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_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..c3e7470e8 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,84 @@ error_ref(struct error *e); void error_unref(struct error *e); +static inline const char * +error_get_str(const struct error *e, const char *name) +{ + return error_payload_get_str(&e->payload, name); +} + +static inline void +error_set_str(struct error *e, const char *name, const char *value) +{ + error_payload_set_str(&e->payload, name, value); +} + +static inline bool +error_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_set_uint(struct error *e, const char *name, uint64_t value) +{ + error_payload_set_uint(&e->payload, name, value); +} + +static inline bool +error_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_set_int(struct error *e, const char *name, int64_t value) +{ + error_payload_set_int(&e->payload, name, value); +} + +static inline bool +error_get_double(const struct error *e, const char *name, double *value) +{ + return error_payload_get_double(&e->payload, name, value); +} + +static inline void +error_set_double(struct error *e, const char *name, double value) +{ + error_payload_set_double(&e->payload, name, value); +} + +static inline bool +error_get_bool(const struct error *e, const char *name, bool *value) +{ + return error_payload_get_bool(&e->payload, name, value); +} + +static inline void +error_set_bool(struct error *e, const char *name, bool value) +{ + error_payload_set_bool(&e->payload, name, value); +} + +static inline bool +error_get_uuid(const struct error *e, const char *name, struct tt_uuid *value) +{ + return error_payload_get_uuid(&e->payload, name, value); +} + +static inline void +error_set_uuid(struct error *e, const char *name, const struct tt_uuid *value) +{ + error_payload_set_uuid(&e->payload, name, value); +} + +static inline void +error_clear_field(struct error *e, const char *name) +{ + error_payload_clear(&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/lua/error.lua b/src/lua/error.lua index 9fb227df4..fb48a9b18 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 { + char *_data; + uint32_t _size; + char _name[1]; +}; + +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)