]> git.saurik.com Git - redis.git/blobdiff - redis.c
in rdbLoadDoubleValue now the buffer is nul terminated correctly. Thanks valgrind.
[redis.git] / redis.c
diff --git a/redis.c b/redis.c
index 627c4cf131a3ae574e83359278c7ee5fab12019d..302a2129d660101c61e425703c31faa2a4cc0499 100644 (file)
--- a/redis.c
+++ b/redis.c
@@ -27,7 +27,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#define REDIS_VERSION "1.100"
+#define REDIS_VERSION "1.1.91"
 
 #include "fmacros.h"
 #include "config.h"
@@ -826,8 +826,7 @@ static dictType hashDictType = {
  * is based on heap allocation for send buffers, so we simply abort.
  * At least the code will be simpler to read... */
 static void oom(const char *msg) {
-    fprintf(stderr, "%s: Out of memory\n",msg);
-    fflush(stderr);
+    redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
     sleep(1);
     abort();
 }
@@ -1802,8 +1801,8 @@ static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int di
             robj *lenobj;
 
             lenobj = createObject(REDIS_STRING,
-                sdscatprintf(sdsempty(),"%d\r\n",
-                    stringObjectLen(argv[j])));
+                sdscatprintf(sdsempty(),"%lu\r\n",
+                    (unsigned long) stringObjectLen(argv[j])));
             lenobj->refcount = 0;
             outv[outc++] = lenobj;
         }
@@ -2019,8 +2018,8 @@ static void addReplyDouble(redisClient *c, double d) {
     char buf[128];
 
     snprintf(buf,sizeof(buf),"%.17g",d);
-    addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n%s\r\n",
-        strlen(buf),buf));
+    addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
+        (unsigned long) strlen(buf),buf));
 }
 
 static void addReplyBulkLen(redisClient *c, robj *obj) {
@@ -2040,7 +2039,7 @@ static void addReplyBulkLen(redisClient *c, robj *obj) {
             len++;
         }
     }
-    addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",len));
+    addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",(unsigned long)len));
 }
 
 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
@@ -2824,6 +2823,7 @@ static int rdbLoadDoubleValue(FILE *fp, double *val) {
     case 253: *val = R_Nan; return 0;
     default:
         if (fread(buf,len,1,fp) == 0) return -1;
+        buf[len] = '\0';
         sscanf(buf, "%lg", val);
         return 0;
     }
@@ -2979,6 +2979,7 @@ static void echoCommand(redisClient *c) {
 static void setGenericCommand(redisClient *c, int nx) {
     int retval;
 
+    deleteIfVolatile(c->db,c->argv[1]);
     retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
     if (retval == DICT_ERR) {
         if (!nx) {
@@ -3054,7 +3055,7 @@ static void mgetCommand(redisClient *c) {
 }
 
 static void msetGenericCommand(redisClient *c, int nx) {
-    int j;
+    int j, busykeys = 0;
 
     if ((c->argc % 2) == 0) {
         addReplySds(c,sdsnew("-ERR wrong number of arguments\r\n"));
@@ -3064,12 +3065,15 @@ static void msetGenericCommand(redisClient *c, int nx) {
      * set nothing at all if at least one already key exists. */
     if (nx) {
         for (j = 1; j < c->argc; j += 2) {
-            if (dictFind(c->db->dict,c->argv[j]) != NULL) {
-                addReply(c, shared.czero);
-                return;
+            if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
+                busykeys++;
             }
         }
     }
+    if (busykeys) {
+        addReply(c, shared.czero);
+        return;
+    }
 
     for (j = 1; j < c->argc; j += 2) {
         int retval;
@@ -3214,7 +3218,7 @@ static void keysCommand(redisClient *c) {
     dictEntry *de;
     sds pattern = c->argv[1]->ptr;
     int plen = sdslen(pattern);
-    int numkeys = 0, keyslen = 0;
+    unsigned long numkeys = 0, keyslen = 0;
     robj *lenobj = createObject(REDIS_STRING,NULL);
 
     di = dictGetIterator(c->db->dict);
@@ -3871,7 +3875,7 @@ static void scardCommand(redisClient *c) {
             addReply(c,shared.wrongtypeerr);
         } else {
             s = o->ptr;
-            addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
+            addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
                 dictSize(s)));
         }
     }
@@ -3936,12 +3940,12 @@ static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
     return dictSize(*d1)-dictSize(*d2);
 }
 
-static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey) {
+static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
     dict **dv = zmalloc(sizeof(dict*)*setsnum);
     dictIterator *di;
     dictEntry *de;
     robj *lenobj = NULL, *dstset = NULL;
-    int j, cardinality = 0;
+    unsigned long j, cardinality = 0;
 
     for (j = 0; j < setsnum; j++) {
         robj *setobj;
@@ -4018,9 +4022,9 @@ static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, r
     }
 
     if (!dstkey) {
-        lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",cardinality);
+        lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
     } else {
-        addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
+        addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
             dictSize((dict*)dstset->ptr)));
         server.dirty++;
     }
@@ -4122,7 +4126,7 @@ static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnu
     if (!dstkey) {
         decrRefCount(dstset);
     } else {
-        addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
+        addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
             dictSize((dict*)dstset->ptr)));
         server.dirty++;
     }
@@ -4650,7 +4654,7 @@ static void zcardCommand(redisClient *c) {
             addReply(c,shared.wrongtypeerr);
         } else {
             zs = o->ptr;
-            addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",zs->zsl->length));
+            addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",zs->zsl->length));
         }
     }
 }
@@ -5068,14 +5072,14 @@ static sds genRedisInfoString(void) {
         "redis_version:%s\r\n"
         "arch_bits:%s\r\n"
         "multiplexing_api:%s\r\n"
-        "uptime_in_seconds:%d\r\n"
-        "uptime_in_days:%d\r\n"
+        "uptime_in_seconds:%ld\r\n"
+        "uptime_in_days:%ld\r\n"
         "connected_clients:%d\r\n"
         "connected_slaves:%d\r\n"
         "used_memory:%zu\r\n"
         "changes_since_last_save:%lld\r\n"
         "bgsave_in_progress:%d\r\n"
-        "last_save_time:%d\r\n"
+        "last_save_time:%ld\r\n"
         "total_connections_received:%lld\r\n"
         "total_commands_processed:%lld\r\n"
         "role:%s\r\n"
@@ -5122,7 +5126,8 @@ static sds genRedisInfoString(void) {
 
 static void infoCommand(redisClient *c) {
     sds info = genRedisInfoString();
-    addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info)));
+    addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
+        (unsigned long)sdslen(info)));
     addReplySds(c,info);
     addReply(c,shared.crlf);
 }
@@ -5664,8 +5669,8 @@ static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv
         char seldb[64];
 
         snprintf(seldb,sizeof(seldb),"%d",dictid);
-        buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
-            strlen(seldb),seldb);
+        buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
+            (unsigned long)strlen(seldb),seldb);
         server.appendseldb = dictid;
     }
 
@@ -5689,7 +5694,7 @@ static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv
         robj *o = argv[j];
 
         o = getDecodedObject(o);
-        buf = sdscatprintf(buf,"$%d\r\n",sdslen(o->ptr));
+        buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
         buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
         buf = sdscatlen(buf,"\r\n",2);
         decrRefCount(o);
@@ -6112,7 +6117,8 @@ static void debugCommand(redisClient *c) {
         val = dictGetEntryVal(de);
         addReplySds(c,sdscatprintf(sdsempty(),
             "+Key at:%p refcount:%d, value at:%p refcount:%d encoding:%d\r\n",
-                key, key->refcount, val, val->refcount, val->encoding));
+                (void*)key, key->refcount, (void*)val, val->refcount,
+                val->encoding));
     } else {
         addReplySds(c,sdsnew(
             "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|RELOAD]\r\n"));
@@ -6157,6 +6163,7 @@ static void daemonize(void) {
     FILE *fp;
 
     if (fork() != 0) exit(0); /* parent exits */
+    printf("New pid: %d\n", getpid());
     setsid(); /* create a new session */
 
     /* Every output goes to /dev/null. If Redis is daemonized but
@@ -6187,8 +6194,8 @@ int main(int argc, char **argv) {
     } else {
         redisLog(REDIS_WARNING,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
     }
-    initServer();
     if (server.daemonize) daemonize();
+    initServer();
     redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
 #ifdef __linux__
     linuxOvercommitMemoryWarning();