redisClient *c = zmalloc(sizeof(redisClient));
c->bufpos = 0;
- anetNonBlock(NULL,fd);
- anetTcpNoDelay(NULL,fd);
- if (aeCreateFileEvent(server.el,fd,AE_READABLE,
- readQueryFromClient, c) == AE_ERR)
- {
- close(fd);
- zfree(c);
- return NULL;
+ /* passing -1 as fd it is possible to create a non connected client.
+ * This is useful since all the Redis commands needs to be executed
+ * in the context of a client. When commands are executed in other
+ * contexts (for instance a Lua script) we need a non connected client. */
+ if (fd != -1) {
+ anetNonBlock(NULL,fd);
+ anetTcpNoDelay(NULL,fd);
+ if (aeCreateFileEvent(server.el,fd,AE_READABLE,
+ readQueryFromClient, c) == AE_ERR)
+ {
+ close(fd);
+ zfree(c);
+ return NULL;
+ }
}
selectDb(c,0);
/* Set the event loop to listen for write events on the client's socket.
* Typically gets called every time a reply is built. */
int _installWriteEvent(redisClient *c) {
+ if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK;
if (c->fd <= 0) return REDIS_ERR;
if (c->bufpos == 0 && listLength(c->reply) == 0 &&
(c->replstate == REDIS_REPL_NONE ||
cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
if (cfd == AE_ERR) {
- redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
+ redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
return;
}
redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
cfd = anetUnixAccept(server.neterr, fd);
if (cfd == AE_ERR) {
- redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
+ redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
return;
}
redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
int processMultibulkBuffer(redisClient *c) {
char *newline = NULL;
- char *eptr;
- int pos = 0, tolerr;
- long bulklen;
+ int pos = 0, ok;
+ long long ll;
if (c->multibulklen == 0) {
/* The client should have been reset */
redisAssert(c->argc == 0);
/* Multi bulk length cannot be read without a \r\n */
- newline = strstr(c->querybuf,"\r\n");
+ newline = strchr(c->querybuf,'\r');
if (newline == NULL)
return REDIS_ERR;
+ /* Buffer should also contain \n */
+ if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
+ return REDIS_ERR;
+
/* We know for sure there is a whole line since newline != NULL,
* so go ahead and find out the multi bulk length. */
redisAssert(c->querybuf[0] == '*');
- c->multibulklen = strtol(c->querybuf+1,&eptr,10);
- pos = (newline-c->querybuf)+2;
- if (c->multibulklen <= 0) {
- c->querybuf = sdsrange(c->querybuf,pos,-1);
- return REDIS_OK;
- } else if (c->multibulklen > 1024*1024) {
+ ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
+ if (!ok || ll > 1024*1024) {
addReplyError(c,"Protocol error: invalid multibulk length");
setProtocolError(c,pos);
return REDIS_ERR;
}
+ pos = (newline-c->querybuf)+2;
+ if (ll <= 0) {
+ c->querybuf = sdsrange(c->querybuf,pos,-1);
+ return REDIS_OK;
+ }
+
+ c->multibulklen = ll;
+
/* Setup argv array on client structure */
if (c->argv) zfree(c->argv);
c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
-
- /* Search new newline */
- newline = strstr(c->querybuf+pos,"\r\n");
}
redisAssert(c->multibulklen > 0);
while(c->multibulklen) {
/* Read bulk length if unknown */
if (c->bulklen == -1) {
- newline = strstr(c->querybuf+pos,"\r\n");
- if (newline != NULL) {
- if (c->querybuf[pos] != '$') {
- addReplyErrorFormat(c,
- "Protocol error: expected '$', got '%c'",
- c->querybuf[pos]);
- setProtocolError(c,pos);
- return REDIS_ERR;
- }
+ newline = strchr(c->querybuf+pos,'\r');
+ if (newline == NULL)
+ break;
- bulklen = strtol(c->querybuf+pos+1,&eptr,10);
- tolerr = (eptr[0] != '\r');
- if (tolerr || bulklen == LONG_MIN || bulklen == LONG_MAX ||
- bulklen < 0 || bulklen > 512*1024*1024)
- {
- addReplyError(c,"Protocol error: invalid bulk length");
- setProtocolError(c,pos);
- return REDIS_ERR;
- }
- pos += eptr-(c->querybuf+pos)+2;
- c->bulklen = bulklen;
- } else {
- /* No newline in current buffer, so wait for more data */
+ /* Buffer should also contain \n */
+ if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
break;
+
+ if (c->querybuf[pos] != '$') {
+ addReplyErrorFormat(c,
+ "Protocol error: expected '$', got '%c'",
+ c->querybuf[pos]);
+ setProtocolError(c,pos);
+ return REDIS_ERR;
}
+
+ ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
+ if (!ok || ll < 0 || ll > 512*1024*1024) {
+ addReplyError(c,"Protocol error: invalid bulk length");
+ setProtocolError(c,pos);
+ return REDIS_ERR;
+ }
+
+ pos += newline-(c->querybuf+pos)+2;
+ c->bulklen = ll;
}
/* Read bulk argument */