[Tarantool-patches] [PATCH v2 1/2] raft: raft_request_to_string -- don't hardcode size

Cyrill Gorcunov gorcunov at gmail.com
Thu Oct 29 11:37:06 MSK 2020


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 <gorcunov at gmail.com>
---
 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



More information about the Tarantool-patches mailing list