Store param value length. Fix off-by-one.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 17 Sep 2008 14:38:06 +0000 (16:38 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 17 Sep 2008 14:39:39 +0000 (16:39 +0200)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
postlicyd/config.c
postlicyd/filter.c
postlicyd/filter.h
postlicyd/rbl.c
postlicyd/strlist.c

index 20716b2..b563ef3 100644 (file)
@@ -304,7 +304,8 @@ read_param_value:
         filter_param_t param;
         param.type  = param_tokenize(key, key_len);
         if (param.type != ATK_UNKNOWN) {
-            param.value = m_strdup(value);
+            param.value     = p_dupstr(value, value_len);
+            param.value_len = value_len;
             array_add(config->params, param);
         }
     }
index a067ef3..dea4436 100644 (file)
@@ -209,9 +209,7 @@ const filter_hook_t *filter_run(const filter_t *filter, const query_t *query)
 
 void filter_set_name(filter_t *filter, const char *name, ssize_t len)
 {
-    filter->name = p_new(char, len + 1);
-    memcpy(filter->name, name, len);
-    filter->name[len] = '\0';
+    filter->name = p_dupstr(name, len);
 }
 
 bool filter_set_type(filter_t *filter, const char *type, ssize_t len)
@@ -234,7 +232,8 @@ bool filter_add_param(filter_t *filter, const char *name, ssize_t name_len,
                atokens[param.type], ftokens[filter->type]);
         return false;
     }
-    param.value = m_strdup(value);
+    param.value     = p_dupstr(value, value_len);
+    param.value_len = value_len;
     array_add(filter->params, param);
     return true;
 }
index e8319d3..f53b917 100644 (file)
@@ -58,7 +58,8 @@ ARRAY(filter_hook_t)
 
 typedef struct filter_param_t {
     filter_param_id_t type;
-    char *value;
+    char    *value;
+    ssize_t value_len;
 } filter_param_t;
 ARRAY(filter_param_t)
 
@@ -186,30 +187,30 @@ const filter_hook_t *filter_run(const filter_t *filter, const query_t *query);
     case ATK_ ## Param: {                                                      \
         char *next;                                                            \
         (Dest) = strtol(param->value, &next, 10);                              \
-        PARSE_CHECK(!*next, "invalid %s value %s", atokens[ATK_ ## Param],     \
-                    param->value);                                             \
+        PARSE_CHECK(!*next, "invalid %s value %.*s", atokens[ATK_ ## Param],   \
+                    param->value_len, param->value);                           \
      } break
 
 #define FILTER_PARAM_PARSE_BOOLEAN(Param, Dest)                                \
     case ATK_ ## Param: {                                                      \
-        if (param->value[0] == '1' && param->value[1] == '\0') {               \
+        if (param->value_len == 1 && param->value[0] == '1') {                 \
             (Dest) = true;                                                     \
-        } else if (param->value[0] == '0' && param->value[1] == '\0') {        \
+        } else if (param->value_len == 1 && param->value[0] == '0') {          \
             (Dest) = false;                                                    \
-        } else if (ascii_tolower(param->value[0]) == 't') {                    \
+        } else if (param->value_len == 4                                       \
+                   && ascii_tolower(param->value[0]) == 't') {                 \
             (Dest) = ascii_tolower(param->value[1]) == 'r'                     \
                   && ascii_tolower(param->value[2]) == 'u'                     \
-                  && ascii_tolower(param->value[3]) == 'e'                     \
-                  && !param->value[4];                                         \
-        } else if (ascii_tolower(param->value[0]) == 'f') {                    \
+                  && ascii_tolower(param->value[3]) == 'e';                    \
+        } else if (param->value_len == 5                                       \
+                   && ascii_tolower(param->value[0]) == 'f') {                 \
             (Dest) = ascii_tolower(param->value[1]) == 'a'                     \
                   && ascii_tolower(param->value[2]) == 'l'                     \
                   && ascii_tolower(param->value[3]) == 's'                     \
-                  && ascii_tolower(param->value[4]) == 'e'                     \
-                  && !param->value[5];                                         \
+                  && ascii_tolower(param->value[4]) == 'e';                    \
         } else {                                                               \
-            PARSE_CHECK(false, "invalid %s value %s", atokens[ATK_ ## Param],  \
-                        param->value);                                         \
+            PARSE_CHECK(false, "invalid %s value %.*s", atokens[ATK_ ## Param],\
+                        param->value_len, param->value);                       \
         }                                                                      \
     } break
 
index 748d40a..1bae4d8 100644 (file)
@@ -305,8 +305,10 @@ static bool rbl_filter_constructor(filter_t *filter)
                     array_add(data->weights, weight);
                     break;
                 }
-                current = p + 1;
-                p = m_strchrnul(current, ':');
+                if (i != 2) {
+                    current = p + 1;
+                    p = m_strchrnul(current, ':');
+                }
             }
           } break;
 
index 25ed069..dee9269 100644 (file)
@@ -219,13 +219,16 @@ static bool strlist_filter_constructor(filter_t *filter)
                     trie = strlist_create(current, reverse, lock);
                     PARSE_CHECK(trie != NULL,
                                 "cannot load string list from %s", current);
+                    trie_inspect(trie, false);
                     array_add(config->tries, trie);
                     array_add(config->weights, weight);
                     array_add(config->reverses, reverse);
                     break;
                 }
-                current = p + 1;
-                p = m_strchrnul(current, ':');
+                if (i != 3) {
+                    current = p + 1;
+                    p = m_strchrnul(current, ':');
+                }
             }
           } break;