summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-04-22 19:00:23 +0200
committerantirez <antirez@gmail.com>2014-04-23 10:16:33 +0200
commit09bb817d1a801f35894172395e07ce5965cde98f (patch)
tree6734e37e0fee89b2458c7dccd8be00b6a68c5f9a
parent20c040d364e2af83a55e70af01663f46f22e1c28 (diff)
downloadredis-onepass-bulk.tar.gz
Read bulk reply length in one pass.onepass-bulk
See issue #1699.
-rw-r--r--src/networking.c63
1 files changed, 43 insertions, 20 deletions
diff --git a/src/networking.c b/src/networking.c
index 9e3e4a21f..17a7286ce 100644
--- a/src/networking.c
+++ b/src/networking.c
@@ -997,35 +997,58 @@ int processMultibulkBuffer(redisClient *c) {
while(c->multibulklen) {
/* Read bulk length if unknown */
if (c->bulklen == -1) {
- newline = strchr(c->querybuf+pos,'\r');
- if (newline == NULL) {
- if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
- addReplyError(c,"Protocol error: too big bulk count string");
- setProtocolError(c,0);
+ char *p = c->querybuf+pos;
+
+ /* Parse $<num>\r\n. Note that we can be certain that
+ * there is either a "\r" if the protcol is correct and
+ * the bulk header is complete, or "\0" (because of sds.c API
+ * that gurantees null-term) if no "\r" is encountered. */
+ if (*p != '$') {
+ if (*p == '\0') {
+ break;
+ } else {
+ addReplyErrorFormat(c,
+ "Protocol error: expected '$', got '%c'", *p);
+ setProtocolError(c, p - c->querybuf);
+ return REDIS_ERR;
}
- break;
+ }
+ p++; /* Skip $ */
+
+ /* Parse the number. */
+ ll = 0;
+ while(*p >= '0' && *p <= '9') {
+ ll *= 10;
+ ll += *p - '0';
+ p++;
}
- /* 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;
+ if (*p != '\r') {
+ if (*p == '\0') { /* No complete bulk header found. */
+ if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
+ addReplyError(c,
+ "Protocol error: too big bulk count string");
+ setProtocolError(c, p - c->querybuf);
+ return REDIS_ERR;
+ }
+ break;
+ } else {
+ addReplyError(c,"Protocol error: invalid bulk length");
+ setProtocolError(c, p - c->querybuf);
+ return REDIS_ERR;
+ }
}
- ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
- if (!ok || ll < 0 || ll > 512*1024*1024) {
+ if (*(p+1) == '\0') break; /* We need a "\n" as well. */
+ p += 2; /* Skip \r\n. */
+
+ if (ll < 0 || ll > 512*1024*1024) {
addReplyError(c,"Protocol error: invalid bulk length");
- setProtocolError(c,pos);
+ setProtocolError(c, p - c->querybuf);
return REDIS_ERR;
}
- pos += newline-(c->querybuf+pos)+2;
+ pos = p - c->querybuf;
if (ll >= REDIS_MBULK_BIG_ARG) {
size_t qblen;