Skip to content

Commit 53c082e

Browse files
committed
Improve networking type correctness
read() and write() return ssize_t (signed long), not int. For other offsets, we can use the unsigned size_t type instead of a signed offset (since our replication offsets and buffer positions are never negative).
1 parent f704360 commit 53c082e

File tree

5 files changed

+13
-12
lines changed

5 files changed

+13
-12
lines changed

src/anet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ int anetUnixNonBlockConnect(char *err, char *path)
391391
* (unless error or EOF condition is encountered) */
392392
int anetRead(int fd, char *buf, int count)
393393
{
394-
int nread, totlen = 0;
394+
ssize_t nread, totlen = 0;
395395
while(totlen != count) {
396396
nread = read(fd,buf,count-totlen);
397397
if (nread == 0) return totlen;
@@ -406,7 +406,7 @@ int anetRead(int fd, char *buf, int count)
406406
* (unless error is encountered) */
407407
int anetWrite(int fd, char *buf, int count)
408408
{
409-
int nwritten, totlen = 0;
409+
ssize_t nwritten, totlen = 0;
410410
while(totlen != count) {
411411
nwritten = write(fd,buf,count-totlen);
412412
if (nwritten == 0) return totlen;

src/cluster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4462,7 +4462,7 @@ void migrateCommand(redisClient *c) {
44624462
{
44634463
sds buf = cmd.io.buffer.ptr;
44644464
size_t pos = 0, towrite;
4465-
int nwritten = 0;
4465+
ssize_t nwritten = 0;
44664466

44674467
while ((towrite = sdslen(buf)-pos) > 0) {
44684468
towrite = (towrite > (64*1024) ? (64*1024) : towrite);

src/networking.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@ void freeClientsInAsyncFreeQueue(void) {
797797

798798
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
799799
redisClient *c = privdata;
800-
int nwritten = 0, totwritten = 0, objlen;
800+
ssize_t nwritten = 0, totwritten = 0;
801+
size_t objlen;
801802
size_t objmem;
802803
robj *o;
803804
REDIS_NOTUSED(el);
@@ -1621,7 +1622,7 @@ int checkClientOutputBufferLimits(redisClient *c) {
16211622
* called from contexts where the client can't be freed safely, i.e. from the
16221623
* lower level functions pushing data inside the client output buffers. */
16231624
void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) {
1624-
redisAssert(c->reply_bytes < ULONG_MAX-(1024*64));
1625+
redisAssert(c->reply_bytes < SIZE_MAX-(1024*64));
16251626
if (c->reply_bytes == 0 || c->flags & REDIS_CLOSE_ASAP) return;
16261627
if (checkClientOutputBufferLimits(c)) {
16271628
sds client = catClientInfoString(sdsempty(),c);

src/redis-benchmark.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typedef struct _client {
8686
char **randptr; /* Pointers to :rand: strings inside the command buf */
8787
size_t randlen; /* Number of pointers in client->randptr */
8888
size_t randfree; /* Number of unused pointers in client->randptr */
89-
unsigned int written; /* Bytes of 'obuf' already written */
89+
size_t written; /* Bytes of 'obuf' already written */
9090
long long start; /* Start time of a request */
9191
long long latency; /* Request latency */
9292
int pending; /* Number of pending requests (replies to consume) */
@@ -266,7 +266,7 @@ static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
266266

267267
if (sdslen(c->obuf) > c->written) {
268268
void *ptr = c->obuf+c->written;
269-
int nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written);
269+
ssize_t nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written);
270270
if (nwritten == -1) {
271271
if (errno != EPIPE)
272272
fprintf(stderr, "Writing to socket: %s\n", strerror(errno));

src/redis.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ typedef struct redisClient {
542542
int multibulklen; /* number of multi bulk arguments left to read */
543543
long bulklen; /* length of bulk argument in multi bulk request */
544544
list *reply;
545-
unsigned long reply_bytes; /* Tot bytes of objects in reply list */
546-
int sentlen; /* Amount of bytes already sent in the current
545+
size_t reply_bytes; /* Tot bytes of objects in reply list */
546+
size_t sentlen; /* Amount of bytes already sent in the current
547547
buffer or object being sent. */
548548
time_t ctime; /* Client creation time */
549549
time_t lastinteraction; /* time of the last interaction, used for timeout */
@@ -553,8 +553,8 @@ typedef struct redisClient {
553553
int replstate; /* replication state if this is a slave */
554554
int repl_put_online_on_ack; /* Install slave write handler on ACK. */
555555
int repldbfd; /* replication DB file descriptor */
556-
off_t repldboff; /* replication DB file offset */
557-
off_t repldbsize; /* replication DB file size */
556+
size_t repldboff; /* replication DB file offset */
557+
size_t repldbsize; /* replication DB file size */
558558
sds replpreamble; /* replication DB preamble. */
559559
long long reploff; /* replication offset if this is our master */
560560
long long repl_ack_off; /* replication ack offset, if this is a slave */
@@ -571,7 +571,7 @@ typedef struct redisClient {
571571
sds peerid; /* Cached peer ID. */
572572

573573
/* Response buffer */
574-
int bufpos;
574+
size_t bufpos;
575575
char buf[REDIS_REPLY_CHUNK_BYTES];
576576
} redisClient;
577577

0 commit comments

Comments
 (0)