From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4E45F44643A for ; Thu, 29 Oct 2020 11:37:22 +0300 (MSK) Received: by mail-lf1-f67.google.com with SMTP id h6so2248167lfj.3 for ; Thu, 29 Oct 2020 01:37:22 -0700 (PDT) From: Cyrill Gorcunov Date: Thu, 29 Oct 2020 11:37:06 +0300 Message-Id: <20201029083707.309206-2-gorcunov@gmail.com> In-Reply-To: <20201029083707.309206-1-gorcunov@gmail.com> References: <20201029083707.309206-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v2 1/2] raft: raft_request_to_string -- don't hardcode size List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tml Cc: Vladislav Shpilevoy The size should be matched to the real size of a buffer, otherwise it is a room for mistake. Same time make sure we're not overriding the buffer. Signed-off-by: Cyrill Gorcunov --- src/box/raft.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/box/raft.c b/src/box/raft.c index 4a8e54cac..7c546de8c 100644 --- a/src/box/raft.c +++ b/src/box/raft.c @@ -275,36 +275,36 @@ static const char * raft_request_to_string(const struct raft_request *req) { assert(req->term != 0); - int size = 1024; char buf[1024]; + int size = sizeof(buf); char *pos = buf; int rc = snprintf(pos, size, "{term: %llu", (unsigned long long)req->term); - assert(rc >= 0); + assert(rc >= 0 && rc < size); pos += rc; size -= rc; if (req->vote != 0) { rc = snprintf(pos, size, ", vote: %u", req->vote); - assert(rc >= 0); + assert(rc >= 0 && rc < size); pos += rc; size -= rc; } if (req->state != 0) { rc = snprintf(pos, size, ", state: %s", raft_state_strs[req->state]); - assert(rc >= 0); + assert(rc >= 0 && rc < size); pos += rc; size -= rc; } if (req->vclock != NULL) { rc = snprintf(pos, size, ", vclock: %s", vclock_to_string(req->vclock)); - assert(rc >= 0); + assert(rc >= 0 && rc < size); pos += rc; size -= rc; } rc = snprintf(pos, size, "}"); - assert(rc >= 0); + assert(rc >= 0 && rc < size); pos += rc; return tt_cstr(buf, pos - buf); } -- 2.26.2