net/mlx5: make tunnel hub list thread safe
[dpdk.git] / lib / librte_pipeline / rte_swx_pipeline_spec.c
index d72badd..f788449 100644 (file)
@@ -10,9 +10,8 @@
 #include "rte_swx_pipeline.h"
 #include "rte_swx_ctl.h"
 
-#define MAX_LINE_LENGTH 256
-#define MAX_TOKENS 16
-#define MAX_INSTRUCTION_LENGTH 256
+#define MAX_LINE_LENGTH RTE_SWX_INSTRUCTION_SIZE
+#define MAX_TOKENS RTE_SWX_INSTRUCTION_TOKENS_MAX
 
 #define STRUCT_BLOCK 0
 #define ACTION_BLOCK 1
@@ -35,9 +34,17 @@ struct extobj_spec {
 static void
 extobj_spec_free(struct extobj_spec *s)
 {
+       if (!s)
+               return;
+
        free(s->name);
+       s->name = NULL;
+
        free(s->extern_type_name);
+       s->extern_type_name = NULL;
+
        free(s->pragma);
+       s->pragma = NULL;
 }
 
 static int
@@ -213,9 +220,8 @@ struct_block_parse(struct struct_spec *s,
                return -ENOMEM;
        }
 
-       new_fields = reallocarray(s->fields,
-                                 s->n_fields + 1,
-                                 sizeof(struct rte_swx_field_params));
+       new_fields = realloc(s->fields,
+                            (s->n_fields + 1) * sizeof(struct rte_swx_field_params));
        if (!new_fields) {
                free(name);
 
@@ -247,8 +253,14 @@ struct header_spec {
 static void
 header_spec_free(struct header_spec *s)
 {
+       if (!s)
+               return;
+
        free(s->name);
+       s->name = NULL;
+
        free(s->struct_type_name);
+       s->struct_type_name = NULL;
 }
 
 static int
@@ -298,7 +310,11 @@ struct metadata_spec {
 static void
 metadata_spec_free(struct metadata_spec *s)
 {
+       if (!s)
+               return;
+
        free(s->struct_type_name);
+       s->struct_type_name = NULL;
 }
 
 static int
@@ -425,7 +441,7 @@ action_block_parse(struct action_spec *s,
                   uint32_t *err_line,
                   const char **err_msg)
 {
-       char buffer[MAX_INSTRUCTION_LENGTH], *instr;
+       char buffer[RTE_SWX_INSTRUCTION_SIZE], *instr;
        const char **new_instructions;
        uint32_t i;
 
@@ -452,9 +468,8 @@ action_block_parse(struct action_spec *s,
                return -ENOMEM;
        }
 
-       new_instructions = reallocarray(s->instructions,
-                                       s->n_instructions + 1,
-                                       sizeof(char *));
+       new_instructions = realloc(s->instructions,
+                                  (s->n_instructions + 1) * sizeof(char *));
        if (!new_instructions) {
                free(instr);
 
@@ -620,9 +635,8 @@ table_key_block_parse(struct table_spec *s,
                return -ENOMEM;
        }
 
-       new_fields = reallocarray(s->params.fields,
-                                 s->params.n_fields + 1,
-                                 sizeof(struct rte_swx_match_field_params));
+       new_fields = realloc(s->params.fields,
+                            (s->params.n_fields + 1) * sizeof(struct rte_swx_match_field_params));
        if (!new_fields) {
                free(name);
 
@@ -700,9 +714,8 @@ table_actions_block_parse(struct table_spec *s,
                return -ENOMEM;
        }
 
-       new_action_names = reallocarray(s->params.action_names,
-                                       s->params.n_actions + 1,
-                                       sizeof(char *));
+       new_action_names = realloc(s->params.action_names,
+                                  (s->params.n_actions + 1) * sizeof(char *));
        if (!new_action_names) {
                free(name);
 
@@ -992,7 +1005,7 @@ apply_block_parse(struct apply_spec *s,
                  uint32_t *err_line,
                  const char **err_msg)
 {
-       char buffer[MAX_INSTRUCTION_LENGTH], *instr;
+       char buffer[RTE_SWX_INSTRUCTION_SIZE], *instr;
        const char **new_instructions;
        uint32_t i;
 
@@ -1019,9 +1032,8 @@ apply_block_parse(struct apply_spec *s,
                return -ENOMEM;
        }
 
-       new_instructions = reallocarray(s->instructions,
-                                       s->n_instructions + 1,
-                                       sizeof(char *));
+       new_instructions = realloc(s->instructions,
+                                  (s->n_instructions + 1) * sizeof(char *));
        if (!new_instructions) {
                free(instr);
 
@@ -1069,7 +1081,7 @@ rte_swx_pipeline_build_from_spec(struct rte_swx_pipeline *p,
                goto error;
        }
 
-       if (!p) {
+       if (!spec) {
                if (err_line)
                        *err_line = 0;
                if (err_msg)
@@ -1113,6 +1125,17 @@ rte_swx_pipeline_build_from_spec(struct rte_swx_pipeline *p,
                                goto error;
                        }
 
+                       /* Handle excessively long tokens. */
+                       if (strnlen(token, RTE_SWX_NAME_SIZE) >=
+                           RTE_SWX_NAME_SIZE) {
+                               if (err_line)
+                                       *err_line = n_lines;
+                               if (err_msg)
+                                       *err_msg = "Token too big.";
+                               status = -EINVAL;
+                               goto error;
+                       }
+
                        /* Save token. */
                        tokens[n_tokens] = token;
                        n_tokens++;