From 01db11ce8d2325f00c394def6cc9d265dc85eb4e Mon Sep 17 00:00:00 2001 From: Qu Chen Date: Thu, 17 Dec 2020 11:58:58 -0800 Subject: [PATCH] Not over-allocate client query buffer when reading large objects. (#5954) In response to large client query buffer optimization introduced in 1898e6c. The calculation of the amount of remaining bytes we need to write to the query buffer was calculated wrong, as a result we are unnecessarily growing the client query buffer by sdslen(c->querybuf) always. This fix corrects that behavior. Please note the previous behavior prior to the before-mentioned change was correctly calculating the remaining additional bytes, and this change makes that calculate to be consistent. Useful context, the argument of size `ll` starts at qb_pos (which is now the beginning of the sds), but much of it may have already been read from the socket, so we only need to grow the sds for the remainder of it. (cherry picked from commit b8c98337cd8f90064085a7af4b416f6fb3e80e1c) --- src/networking.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/networking.c b/src/networking.c index 9150426a5..2c3a1d067 100644 --- a/src/networking.c +++ b/src/networking.c @@ -1793,7 +1793,7 @@ int processMultibulkBuffer(client *c) { c->qb_pos = 0; /* Hint the sds library about the amount of bytes this string is * going to contain. */ - c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2); + c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2-sdslen(c->querybuf)); } } c->bulklen = ll;