]> git.saurik.com Git - redis.git/blobdiff - src/sds.c
[issue 620] don't segfault if number of arguments is less than 1
[redis.git] / src / sds.c
index 343ad1fa7fc662ae9641b6da832e9e386199db65..2ec7c3cb76b3a2d6f5afdb3be8bd6aa793329a1c 100644 (file)
--- a/src/sds.c
+++ b/src/sds.c
@@ -298,15 +298,17 @@ int sdscmp(sds s1, sds s2) {
  */
 sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
     int elements = 0, slots = 5, start = 0, j;
+    sds *tokens;
 
-    sds *tokens = zmalloc(sizeof(sds)*slots);
+    if (seplen < 1 || len < 0) return NULL;
+
+    tokens = zmalloc(sizeof(sds)*slots);
 #ifdef SDS_ABORT_ON_OOM
     if (tokens == NULL) sdsOomAbort();
+#else
+    if (tokens == NULL) return NULL;
 #endif
-    if (seplen < 1 || len < 0 || tokens == NULL) {
-        *count = 0;
-        return NULL;
-    }
+
     if (len == 0) {
         *count = 0;
         return tokens;
@@ -551,6 +553,29 @@ void sdssplitargs_free(sds *argv, int argc) {
     zfree(argv);
 }
 
+/* Modify the string substituting all the occurrences of the set of
+ * characters specifed in the 'from' string to the corresponding character
+ * in the 'to' array.
+ *
+ * For instance: sdsmapchars(mystring, "ho", "01", 2)
+ * will have the effect of turning the string "hello" into "0ell1".
+ *
+ * The function returns the sds string pointer, that is always the same
+ * as the input pointer since no resize is needed. */
+sds sdsmapchars(sds s, char *from, char *to, size_t setlen) {
+    size_t j, i, l = sdslen(s);
+
+    for (j = 0; j < l; j++) {
+        for (i = 0; i < setlen; i++) {
+            if (s[j] == from[i]) {
+                s[j] = to[i];
+                break;
+            }
+        }
+    }
+    return s;
+}
+
 #ifdef SDS_TEST_MAIN
 #include <stdio.h>
 #include "testhelp.h"