Fix bug in writeToClient (#834)

Fix bug in writeToClient
In https://github.com/valkey-io/valkey/pull/758, a major refactor was
done to `networking.c`.

As part of this refactor, a new bug was introduced: we don't advance the
`c->buf` pointer in repeated writes.

This bug should be very unlikely to manifest, as it requires the
client's TCP buffer to be filled in the first try and then released
immediately after in the second try.

Despite all my efforts to reproduce this scenario, I was unable to do
so.

Signed-off-by: Uri Yagelnik <uriy@amazon.com>
This commit is contained in:
uriyage 2024-07-31 07:54:18 +03:00 committed by GitHub
parent fa238dc049
commit 1d18842074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2092,7 +2092,7 @@ int _writeToClient(client *c) {
ssize_t tot_written = 0;
while (tot_written < bytes_to_write) {
int nwritten = connWrite(c->conn, c->buf + c->sentlen, bytes_to_write - tot_written);
int nwritten = connWrite(c->conn, c->buf + c->sentlen + tot_written, bytes_to_write - tot_written);
if (nwritten <= 0) {
c->write_flags |= WRITE_FLAGS_WRITE_ERROR;
tot_written = tot_written > 0 ? tot_written : nwritten;